Skip to main content

tap/formatting/
help.rs

1use crate::{
2    engine::structs::{CmdInfo, Engine}, 
3    formatting::{set::colorize, text::{add_space, frame}}
4};
5
6#[derive(PartialEq)]
7pub struct HelpCmdInfo<'a, H> {
8    pub commands: Vec<String>,
9    pub info: &'a CmdInfo<'a, H>
10}
11
12impl<'a, H> HelpCmdInfo<'a, H> {
13    pub fn new(command: String, info: &'a CmdInfo<'a, H>) -> Self {
14        Self { commands: vec![command], info: info }
15    }
16
17    pub fn is(&mut self, name: String, info: &CmdInfo<'a, H>) -> bool {
18        let same = self.info.required == info.required
19            && self.info.optional == info.optional
20            && self.info.unlimited == info.unlimited
21            && self.info.docs == info.docs;
22        if same {
23            self.commands.push(name);
24        }
25        same
26    }
27}
28
29
30pub fn group_commands<'a, C, H>(commands: &'a C) -> Vec<HelpCmdInfo<'a, H>>
31where
32    &'a C: IntoIterator<Item = (&'a &'a str, &'a CmdInfo<'a, H>)>,
33{
34    let mut grouped: Vec<HelpCmdInfo<H>> = Vec::new();
35    for (name, info) in commands {
36        let mut added = false;
37        for cmd in &mut grouped {
38            if cmd.is(name.to_string(), info) {
39                added = true;
40                break;
41            }
42        }
43        if !added {
44            grouped.push(HelpCmdInfo::new(name.to_string(), info));
45        }
46    }
47    grouped
48}
49
50pub fn generate_help(eng: &Engine) -> String {
51    let mut res = String::from("");
52    // === Paste about ===
53    if eng.about.trim() != "" {
54        res.push_str(&format!("{}", colorize(&eng.about)));
55        res.push_str("\n\n");
56    }
57    // === Paste basic handler docs after about ===
58    match eng.basic_handler {
59        Some(ref i) => {
60            let docs = i.docs;
61            if docs.trim() != "" {
62                res.push_str(docs);
63                res.push_str("\n\n");
64            }
65        }, None => {}
66    }
67    // === Generating commands help ===
68    let commands = group_commands(&eng.cmds);
69    for cmd in commands {
70        let mut argsv: Vec<String> = vec![];
71        for req in &cmd.info.required {
72            let r = format!("[?RD]<{}>[?RT]", req.to_string());
73            argsv.push(r);
74        }
75        for opt in &cmd.info.optional {
76            let r = format!("[?BE][{}][?RT]", opt.to_string());
77            argsv.push(r);
78        }
79        if cmd.info.unlimited {
80            argsv.push("[?CN](arg...)[?RT]".to_string());
81        }
82        if argsv.len() == 0 {
83            argsv.push("(none)".to_string());
84        }
85        let args = colorize(&argsv.join(", "));
86        let block = &format!(
87"- [?YW]Args[?RT]: {}
88- [?YW]Docs[?RT]:
89{}", 
90args, add_space(&cmd.info.docs, " ", 1));
91        let cmd = &colorize(&format!(
92            "[?BGN] Command [?BKBK][?BBK][[?BKBK][?YW]{}[?BBK]][?RT]", 
93        cmd.commands.join(" [?GN]OR[?YW] ")));
94        res.push_str(&frame(block, cmd, "", "GN"));
95        res.push('\n');
96    }
97    // === Generating subcommands help ===
98    let subcommands = group_commands(&eng.subcmds);
99    for cmd in subcommands {
100        let block = &format!("- [?YW]Docs[?RT]:
101{}", add_space(&cmd.info.docs, " ", 1));
102        let cmd = &colorize(&format!(
103            "[?BGN] Command [?BKBK][?BBK][[?BKBK][?YW]{}[?BBK]][?RT]", 
104        cmd.commands.join(" [?GN]OR[?YW] ")));
105        res.push_str(&frame(block, cmd, "", "GN"));
106        res.push('\n');
107    }
108    res
109}