dysk_cli/
help.rs

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