dysk_cli/
help.rs

1use {
2    crate::args::*,
3    clap::CommandFactory,
4};
5
6static INTRO_TEMPLATE: &str = "
7**dysk** displays filesystem information in a pretty table.
8Complete documentation at https://dystroy.org/dysk
9
10";
11
12static EXAMPLES_TEMPLATE: &str = "
13**Examples:**
14
15${examples
16**${example-number})** ${example-title}: `${example-cmd}`
17${example-comments}
18}
19";
20
21static EXAMPLES: &[Example] = &[
22    Example::new("Standard overview of your usual disks", "dysk", ""),
23    Example::new("List all filesystems", "dysk -a", ""),
24    Example::new("Display inodes information", "dysk -c +inodes", ""),
25    Example::new(
26        "Add columns of your choice",
27        "dysk -c label+dev+",
28        "You may add columns before, after, or at any other place. \
29        You can change the column order too. \
30        See https://dystroy.org/dysk/table#columns\n",
31    ),
32    Example::new("See the disk of the current directory", "dysk .", ""),
33    Example::new(
34        "Filter for low space",
35        "dysk -f 'use > 65% | free < 50G'",
36        "",
37    ),
38    Example::new("Filter to exclude SSD disks", "dysk -f 'disk <> SSD'", ""),
39    Example::new(
40        "Complex filter",
41        "dysk -f '(type=xfs & remote=no) | size > 5T'",
42        "",
43    ),
44    Example::new("Export as JSON", "dysk -j", ""),
45    Example::new(
46        "Sort by free size",
47        "dysk -s free",
48        "Add `-desc` to the column name to sort in reverse.",
49    ),
50];
51
52pub fn print(ascii: bool) {
53    let mut printer = clap_help::Printer::new(Args::command())
54        .with("introduction", INTRO_TEMPLATE)
55        .without("author");
56    printer.template_keys_mut().push("examples");
57    printer.set_template("examples", EXAMPLES_TEMPLATE);
58    if ascii {
59        printer.skin_mut().limit_to_ascii();
60    }
61    for (i, example) in EXAMPLES.iter().enumerate() {
62        printer
63            .expander_mut()
64            .sub("examples")
65            .set("example-number", i + 1)
66            .set("example-title", example.title)
67            .set("example-cmd", example.cmd)
68            .set_md("example-comments", example.comments);
69    }
70    printer.print_help();
71}
72
73struct Example {
74    title: &'static str,
75    cmd: &'static str,
76    comments: &'static str,
77}
78
79impl Example {
80    pub const fn new(
81        title: &'static str,
82        cmd: &'static str,
83        comments: &'static str,
84    ) -> Self {
85        Self {
86            title,
87            cmd,
88            comments,
89        }
90    }
91}