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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! How `ljos --help` groups commands.
//!
//! The short list leads with the commands a new seat needs.
//! A command a runner or git calls, or one that is rarely typed, is
//! hidden. It still runs. `ljos help --all` lists it in the same group.
#[cfg(test)]
use std::collections::BTreeSet;
use clap::Command;
/// Two spaces, the indent clap uses for a command in `--help`.
const INDENT: &str = " ";
/// Spaces clap leaves between the longest name and its description.
const GAP: usize = 2;
/// `(heading, command names in the order they are printed)`.
///
/// Hidden names sit at the end of their group, so the short list stays
/// the commands a person types and `--all` keeps the same headings.
const GROUPS: &[(&str, &[&str])] = &[
(
"Start",
&[
"doctor", "onboard", "protocol", "prefer", "remember", "search", "sitting", "finish",
"due", "help",
],
),
(
"Daily",
&[
"note", "file", "show", "ready", "claims", "inbox", "digest", "rule", "rules", "sync",
"hud", "seat", "cards",
],
),
(
"Tracker",
&[
"claim",
"release",
"complete",
"reap",
"options",
"block",
"deed",
"evidence",
"record",
"current",
"recall",
"timeline",
"log",
"usage",
"reopen",
"withdraw",
"merge-atoms",
"merge-org",
"bump-plan",
"findings",
],
),
(
"Memory",
&[
"memory",
"memories",
"export",
"accept",
"forget",
"proposals",
"conflicts",
"consolidate",
"hubs",
"island",
"handover",
"receive",
"graded",
"habit",
"retire-notes",
"judge-score",
],
),
(
"Coordination",
&[
"send",
"read",
"reply",
"group",
"wake",
"vote",
"consensus",
"panel",
"persona",
"personas",
"agents",
"ask",
"brief",
"playbook",
"playbooks",
"workflow",
"predict",
"trust",
"calibrate",
"learn",
"open-panel",
],
),
(
"Admin",
&[
"policy",
"offboard",
"uninstall",
"upgrade",
"git-hooks",
"person-key",
"cloud",
"mesh",
"mcp",
"hook",
"git-hook",
"statusline",
"supervise",
"slot",
],
),
];
/// The names in [`GROUPS`], in order, one heading at a time.
#[cfg(test)]
pub fn names() -> impl Iterator<Item = &'static str> {
GROUPS.iter().flat_map(|(_, names)| names.iter().copied())
}
/// `ljos --help` and `ljos help`, in clap's plain layout, with one heading
/// per group. `all` includes commands clap has marked hidden.
pub fn render(cmd: &Command, all: bool) -> String {
let shown: Vec<(&str, &Command)> = GROUPS
.iter()
.flat_map(|(heading, names)| {
names.iter().filter_map(move |name| {
let sub = cmd.find_subcommand(name)?;
if !all && sub.is_hide_set() {
return None;
}
Some((*heading, sub))
})
})
.collect();
let longest = shown
.iter()
.map(|(_, sub)| sub.get_name().len())
.max()
.unwrap_or(0);
let mut out = String::new();
if let Some(about) = cmd.get_about() {
out.push_str(&about.to_string());
out.push_str("\n\n");
}
out.push_str("Usage: ");
out.push_str(cmd.get_name());
out.push_str(" <COMMAND>\n\n");
let mut last = "";
for (heading, sub) in &shown {
if *heading != last {
if !last.is_empty() {
out.push('\n');
}
out.push_str(heading);
out.push_str(":\n");
last = heading;
}
let name = sub.get_name();
let about = sub
.get_about()
.map(|text| text.to_string().replace('\n', " "))
.unwrap_or_default();
let pad = longest + GAP - name.len();
out.push_str(INDENT);
out.push_str(name);
out.push_str(&" ".repeat(pad));
out.push_str(&about);
out.push('\n');
}
// The same two lines clap prints for this command's flags.
out.push_str("\nOptions:\n");
out.push_str(" -h, --help Print help\n");
out.push_str(" -V, --version Print version\n");
if let Some(after) = cmd.get_after_help() {
out.push('\n');
out.push_str(&after.to_string());
if !out.ends_with('\n') {
out.push('\n');
}
}
out
}
/// Every subcommand of `cmd` is in one group, and every group name is a
/// subcommand. A hidden command is one clap marks hidden.
#[cfg(test)]
pub fn groups_match(cmd: &Command) -> Result<(), String> {
let mut have: BTreeSet<&str> = cmd.get_subcommands().map(Command::get_name).collect();
let mut seen = BTreeSet::new();
for name in names() {
if !seen.insert(name) {
return Err(format!("{name} is in two groups"));
}
if !have.remove(name) {
return Err(format!("{name} is not a command"));
}
}
if !have.is_empty() {
return Err(format!(
"not grouped: {}",
have.into_iter().collect::<Vec<_>>().join(", ")
));
}
for name in names() {
let sub = cmd.find_subcommand(name).expect("just checked");
let hidden = matches!(
name,
"merge-atoms"
| "merge-org"
| "bump-plan"
| "findings"
| "retire-notes"
| "judge-score"
| "open-panel"
| "hook"
| "git-hook"
| "statusline"
| "supervise"
| "slot"
);
if sub.is_hide_set() != hidden {
return Err(format!(
"{name}: hide is {}, the group list says {hidden}",
sub.is_hide_set()
));
}
}
Ok(())
}