1use std::io::Write;
8
9use clap::Args;
10
11use crate::exit::ExitCode;
12
13#[derive(Debug, Args)]
14pub struct CheatsheetArgs {
15 pub topic: Option<String>,
17}
18
19const SHEET: &str = include_str!("../../docs/cheatsheet.txt");
20
21#[must_use]
22pub fn run(args: &CheatsheetArgs) -> ExitCode {
23 let mut out = anstream::stdout();
24
25 let sheet = SHEET.replace("\r\n", "\n");
29
30 let Some(topic) = args.topic.as_deref() else {
31 let _ = write!(out, "{sheet}");
32 return ExitCode::Success;
33 };
34
35 let wanted = format!("## {topic}");
37 let mut found = false;
38 for block in sheet.split("\n\n") {
39 if block.starts_with(&wanted) {
40 let _ = writeln!(out, "{}", block.trim_end());
41 found = true;
42 }
43 }
44
45 if found {
46 ExitCode::Success
47 } else {
48 let mut err = anstream::stderr();
49 let _ = writeln!(
50 err,
51 "unknown topic `{topic}`; run `ytcli cheatsheet` for all"
52 );
53 ExitCode::Failure
54 }
55}