tap/tooling/formatting/
help.rs1use crate::{
2 engine::structs::{CmdInfo, Engine},
3 tooling::{
4 colors::set::colorize,
5 formatting::text::{add_space, frame},
6 },
7};
8
9#[derive(PartialEq)]
10pub struct HelpCmdInfo<'a, H> {
11 pub commands: Vec<String>,
12 pub info: &'a CmdInfo<'a, H>,
13}
14
15impl<'a, H> HelpCmdInfo<'a, H> {
16 pub fn new(command: String, info: &'a CmdInfo<'a, H>) -> Self {
17 Self {
18 commands: vec![command],
19 info: info,
20 }
21 }
22
23 pub fn is(&mut self, name: String, info: &CmdInfo<'a, H>) -> bool {
24 let same = self.info.required == info.required
25 && self.info.optional == info.optional
26 && self.info.unlimited == info.unlimited
27 && self.info.docs == info.docs;
28 if same {
29 self.commands.push(name);
30 }
31 same
32 }
33}
34
35pub fn group_commands<'a, C, H>(commands: &'a C) -> Vec<HelpCmdInfo<'a, H>>
36where
37 &'a C: IntoIterator<Item = (&'a &'a str, &'a CmdInfo<'a, H>)>,
38{
39 let mut grouped: Vec<HelpCmdInfo<H>> = Vec::new();
40 for (name, info) in commands {
41 let mut added = false;
42 for cmd in &mut grouped {
43 if cmd.is(name.to_string(), info) {
44 added = true;
45 break;
46 }
47 }
48 if !added {
49 grouped.push(HelpCmdInfo::new(name.to_string(), info));
50 }
51 }
52 grouped
53}
54
55pub fn generate_help(eng: &Engine) -> String {
56 let mut res = String::from("");
57 if eng.about.trim() != "" {
59 res.push_str(&format!("{}", colorize(&eng.about)));
60 res.push_str("\n\n");
61 }
62 match eng.basic_handler {
64 Some(ref i) => {
65 let docs = i.docs;
66 if docs.trim() != "" {
67 res.push_str(docs);
68 res.push_str("\n\n");
69 }
70 }
71 None => {}
72 }
73 let mut cmds_keys: Vec<&str> = eng.cmds.keys().copied().collect();
75 cmds_keys.sort_unstable();
76 let mut commands: Vec<HelpCmdInfo<'_, _>> = Vec::new();
77 for name in cmds_keys.iter().copied() {
78 let info = eng.cmds.get(name).unwrap();
79 let mut added = false;
80 for cmd in &mut commands {
81 if cmd.is(name.to_string(), info) {
82 added = true;
83 break;
84 }
85 }
86 if !added {
87 commands.push(HelpCmdInfo::new(name.to_string(), info));
88 }
89 }
90 for cmd in commands {
91 let mut argsv: Vec<String> = vec![];
92 for req in &cmd.info.required {
93 let r = format!("[?RD]<{}>[?RT]", req.to_string());
94 argsv.push(r);
95 }
96 for opt in &cmd.info.optional {
97 let r = format!("[?BE][{}][?RT]", opt.to_string());
98 argsv.push(r);
99 }
100 if cmd.info.unlimited {
101 argsv.push("[?CN](arg...)[?RT]".to_string());
102 }
103 if argsv.len() == 0 {
104 argsv.push("(none)".to_string());
105 }
106 let args = colorize(&argsv.join(", "));
107 let block = &format!(
108 "- [?YW]Args[?RT]: {}\n- [?YW]Docs[?RT]:\n{}",
109 args,
110 add_space(&cmd.info.docs, " ", 1)
111 );
112 let cmd = &colorize(&format!(
113 "[?BGN] Command [?BKBK][?BBK][[?BKBK][?YW]{}[?BBK]][?RT]",
114 cmd.commands.join(" [?GN]OR[?YW] ")
115 ));
116 res.push_str(&frame(block, cmd, "", "GN"));
117 res.push('\n');
118 }
119 let mut subcmds_keys: Vec<&str> = eng.subcmds.keys().copied().collect();
121 subcmds_keys.sort_unstable();
122 let mut subcommands: Vec<HelpCmdInfo<'_, _>> = Vec::new();
123 for name in subcmds_keys.iter().copied() {
124 let info = eng.subcmds.get(name).unwrap();
125 let mut added = false;
126 for cmd in &mut subcommands {
127 if cmd.is(name.to_string(), info) {
128 added = true;
129 break;
130 }
131 }
132 if !added {
133 subcommands.push(HelpCmdInfo::new(name.to_string(), info));
134 }
135 }
136 for cmd in subcommands {
137 let block = &format!("- [?YW]Docs[?RT]:\n{}", add_space(&cmd.info.docs, " ", 1));
138 let cmd = &colorize(&format!(
139 "[?BGN] Command [?BKBK][?BBK][[?BKBK][?YW]{}[?BBK]][?RT]",
140 cmd.commands.join(" [?GN]OR[?YW] ")
141 ));
142 res.push_str(&frame(block, cmd, "", "GN"));
143 res.push('\n');
144 }
145 res
146}