1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! One-shot compact reference.
//!
//! The skill that ships with this tool stays deliberately small; an agent that
//! wants the whole surface at once runs this instead of loading a large document
//! it mostly will not use (`docs/adr/0006-agent-surface.md`).
use std::io::Write;
use clap::Args;
use crate::exit::ExitCode;
#[derive(Debug, Args)]
pub struct CheatsheetArgs {
/// Narrow the sheet to one topic: issue, auth, queue, project, goal, attachment, format.
pub topic: Option<String>,
}
const SHEET: &str = include_str!("../../docs/cheatsheet.txt");
#[must_use]
pub fn run(args: &CheatsheetArgs) -> ExitCode {
let mut out = anstream::stdout();
// The sheet is compiled in from a text file, and a Windows checkout may have
// rewritten its line endings. Normalising here keeps the section splitting
// below platform-independent regardless of how the source was checked out.
let sheet = SHEET.replace("\r\n", "\n");
let Some(topic) = args.topic.as_deref() else {
let _ = write!(out, "{sheet}");
return ExitCode::Success;
};
// Sections are separated by a blank line and start with `## <topic>`.
let wanted = format!("## {topic}");
let mut found = false;
for block in sheet.split("\n\n") {
if block.starts_with(&wanted) {
let _ = writeln!(out, "{}", block.trim_end());
found = true;
}
}
if found {
ExitCode::Success
} else {
let mut err = anstream::stderr();
let _ = writeln!(
err,
"unknown topic `{topic}`; run `ytcli cheatsheet` for all"
);
ExitCode::Failure
}
}