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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use {
    crate::{
        args::*,
    },
    clap::CommandFactory,
};

static INTRO_TEMPLATE: &str = "
**dysk** displays filesystem information in a pretty table.
Complete documentation at https://dystroy.org/dysk

";

static EXAMPLES_TEMPLATE: &str = "
**Examples:**

${examples
**${example-number})** ${example-title}: `${example-cmd}`
${example-comments}
}
";

static EXAMPLES: &[Example] = &[
    Example::new(
        "Standard overview of your usual disks",
        "dysk",
        ""
    ),
    Example::new(
        "List all filesystems",
        "dysk -a",
        ""
    ),
    Example::new(
        "Display inodes information",
        "dysk -c +inodes",
        ""
    ),
    Example::new(
        "Add columns of your choice",
        "dysk -c label+dev+",
        "You may add columns before, after, or at any other place. \
        You can change the column order too. \
        See https://dystroy.org/dysk/table#columns\n"
    ),
    Example::new(
        "See the disk of the current directory",
        "dysk .",
        ""
    ),
    Example::new(
        "Filter for low space",
        "dysk -f 'use > 65% | free < 50G'",
        ""
    ),
    Example::new(
        "Filter to exclude SSD disks",
        "dysk -f 'disk <> SSD'",
        ""
    ),
    Example::new(
        "Complex filter",
        "dysk -f '(type=xfs & remote=no) | size > 5T'",
        ""
    ),
    Example::new(
        "Export as JSON",
        "dysk -j",
        ""
    ),
    Example::new(
        "Sort by free size",
        "dysk -s free",
        "Add `-desc` to the column name to sort in reverse."
    ),
];

pub fn print(ascii: bool) {
    let mut printer = clap_help::Printer::new(Args::command())
        .with("introduction", INTRO_TEMPLATE)
        .without("author");
    printer.template_keys_mut().push("examples");
    printer.set_template("examples", EXAMPLES_TEMPLATE);
    if ascii {
        printer.skin_mut().limit_to_ascii();
    }
    for (i, example) in EXAMPLES.iter().enumerate() {
        printer
            .expander_mut()
            .sub("examples")
            .set("example-number", i + 1)
            .set("example-title", example.title)
            .set("example-cmd", example.cmd)
            .set_md("example-comments", example.comments);
    }
    printer.print_help();
}

struct Example {
    title: &'static str,
    cmd: &'static str,
    comments: &'static str,
}

impl Example {
    pub const fn new(
        title: &'static str,
        cmd: &'static str,
        comments: &'static str,
    ) -> Self {
        Self { title, cmd, comments }
    }
}