moros/usr/
help.rs

1use crate::api::console::Style;
2use crate::api::process::ExitCode;
3
4pub fn main(args: &[&str]) -> Result<(), ExitCode> {
5    match args.len() {
6        1 => help_summary(),
7        2 => help_command(args[1]),
8        _ => {
9            help();
10            Err(ExitCode::UsageError)
11        }
12    }
13}
14
15fn help_command(cmd: &str) -> Result<(), ExitCode> {
16    match cmd {
17        "-h" | "--help" => {
18            help();
19            Ok(())
20        }
21        "date" => help_date(),
22        "edit" => help_edit(),
23        _ => help_unknown(cmd),
24    }
25}
26
27fn help_unknown(cmd: &str) -> Result<(), ExitCode> {
28    error!("Help not found for command '{}'", cmd);
29    Err(ExitCode::Failure)
30}
31
32fn print_usage(alias: &str, command: &str, usage: &str) {
33    let csi_col1 = Style::color("lime");
34    let csi_col2 = Style::color("aqua");
35    let csi_reset = Style::reset();
36    println!(
37        "  {}{}{}{:21}{}{}",
38        csi_col1, alias, csi_col2, command, csi_reset, usage
39    );
40}
41
42fn help_summary() -> Result<(), ExitCode> {
43    let csi_color = Style::color("yellow");
44    let csi_reset = Style::reset();
45
46    println!("{}Usage:{}", csi_color, csi_reset);
47    print_usage("", "<dir>", " Change directory");
48    print_usage("", "<cmd>", " Execute command");
49    println!();
50
51    println!("{}Commands:{}", csi_color, csi_reset);
52    print_usage(
53        "c",
54        "opy <file> <file>",
55        "Copy file from source to destination",
56    );
57    print_usage("d", "rop <file>", "Drop file or empty directory");
58    print_usage("e", "dit <file>", "Edit existing or new file");
59    print_usage("f", "ind <str> <path>", "Find pattern in path");
60    print_usage("h", "elp <cmd>", "Display help about a command");
61    print_usage("l", "ist <dir>", "List entries in directory");
62    print_usage(
63        "m",
64        "ove <file> <file>",
65        "Move file from source to destination",
66    );
67    print_usage("p", "rint <str>", "Print string to screen");
68    print_usage("q", "uit", "Quit the console");
69    print_usage("r", "ead <file>", "Read file to screen");
70    print_usage("w", "rite <file>", "Write file or directory");
71    println!();
72
73    println!("{}Credits:{}", csi_color, csi_reset);
74    println!("  Made with <3 in 2019-2025 by Vincent Ollivier <v@vinc.cc>");
75    Ok(())
76}
77
78fn help_edit() -> Result<(), ExitCode> {
79    let csi_color = Style::color("yellow");
80    let csi_reset = Style::reset();
81    let description = [
82        "MOROS Editor is a minimal keyboard-driven text editor with shortcuts",
83        "for navigation, editing, and file management."
84    ];
85    for line in description {
86        println!("{}", line);
87    }
88    println!();
89    println!("{}Commands:{}", csi_color, csi_reset);
90    let commands = [
91        ("^A", "Move cursor to beginning of line"),
92        ("^B", "Move cursor to end of file"),
93        ("^C", "Quit"),
94        ("^D", "Cut line"),
95        ("^E", "Move cursor to end of line"),
96        ("^F", "Find string in file"),
97        ("^K", "Kill buffer"),
98        ("^N", "Find next string in file"),
99        ("^O", "Open buffer"),
100        ("^P", "Paste line"),
101        ("^Q", "Quit"),
102        ("^T", "Move cursor to beginning of file"),
103        ("^W", "Write to file"),
104        ("^X", "Write to file and quit"),
105        ("^Y", "Copy line"),
106    ];
107    for (command, usage) in &commands {
108        let csi_color = Style::color("aqua");
109        let csi_reset = Style::reset();
110        println!("  {}{}{}    {}", csi_color, command, csi_reset, usage);
111    }
112    Ok(())
113}
114
115fn help_date() -> Result<(), ExitCode> {
116    let csi_color = Style::color("yellow");
117    let csi_reset = Style::reset();
118    println!("The date command's formatting behavior is based on strftime.");
119    println!();
120    println!("{}Specifiers:{}", csi_color, csi_reset);
121    let specifiers = [
122        (
123            "%a",
124            "Abbreviated weekday name",
125            "Thu",
126        ),
127        (
128            "%A",
129            "Full weekday name",
130            "Thursday",
131        ),
132        (
133            "%b",
134            "Abbreviated month name",
135            "Aug",
136        ),
137        (
138            "%B",
139            "Full month name",
140            "August",
141        ),
142        (
143            "%c",
144            "Date and time, equivalent to %a %b %-d %-H:%M:%S %-Y",
145            "Thu Aug 23 14:55:02 2001",
146        ),
147        (
148            "%C",
149            "Year divided by 100 and truncated to integer (00-99)",
150            "20",
151        ),
152        (
153            "%d",
154            "Day of the month, zero-padded (01-31)",
155            "23",
156        ),
157        (
158            "%D",
159            "Short MM/DD/YY date, equivalent to %-m/%d/%y",
160            "8/23/01",
161        ),
162        (
163            "%F",
164            "Short YYYY-MM-DD date, equivalent to %-Y-%m-%d",
165            "2001-08-23",
166        ),
167        (
168            "%g",
169            "Week-based year, last two digits (00-99)",
170            "01",
171        ),
172        (
173            "%G",
174            "Week-based year",
175            "2001",
176        ),
177        (
178            "%H",
179            "Hour in 24h format (00-23)",
180            "14",
181        ),
182        (
183            "%I",
184            "Hour in 12h format (01-12)",
185            "02",
186        ),
187        (
188            "%j",
189            "Day of the year (001-366)",
190            "235",
191        ),
192        (
193            "%m",
194            "Month as a decimal number (01-12)",
195            "08",
196        ),
197        (
198            "%M",
199            "Minute (00-59)",
200            "55",
201        ),
202        (
203            "%N",
204            "Subsecond nanoseconds. Always 9 digits",
205            "012345678",
206        ),
207        (
208            "%p", 
209            "am or pm designation", "pm"),
210        (
211            "%P", 
212            "AM or PM designation", "PM"),
213        (
214            "%r",
215            "12-hour clock time, equivalent to %-I:%M:%S %p",
216            "2:55:02 pm",
217        ),
218        (
219            "%R",
220            "24-hour HH:MM time, equivalent to %-H:%M",
221            "14:55"
222        ),
223        (
224            "%S",
225            "Second (00-59)",
226            "02"
227        ),
228        (
229            "%T",
230            "24-hour clock time with seconds, equivalent to %-H:%M:%S",
231            "14:55:02",
232        ),
233        (
234            "%u",
235            "ISO 8601 weekday as number with Monday as 1 (1-7)",
236            "4",
237        ),
238        (
239            "%U",
240            "Week number with Sunday as first day of the week (00-53)",
241            "33",
242        ),
243        (
244            "%V",
245            "ISO 8601 week number (01-53)",
246            "34",
247        ),
248        (
249            "%w",
250            "Weekday as a decimal number with Sunday as 0 (0-6)",
251            "4",
252        ),
253        (
254            "%W",
255            "Week number with Monday as first day of the week (00-53)",
256            "34",
257        ),
258        (
259            "%y",
260            "Year, last two digits (00-99)",
261            "01",
262        ),
263        (
264            "%Y",
265            "Full year, including + if ≥10,000",
266            "2001",
267        ),
268        (
269            "%z",
270            "ISO 8601 offset from UTC in timezone (+HHMM)",
271            "+0100",
272        ),
273        (
274            "%%",
275            "Literal %",
276            "%"
277        ),
278    ];
279    for (specifier, usage, _exemple) in &specifiers {
280        let csi_color = Style::color("aqua");
281        let csi_reset = Style::reset();
282        println!("  {}{}{}    {}", csi_color, specifier, csi_reset, usage);
283    }
284    Ok(())
285}
286
287fn help() {
288    let csi_option = Style::color("aqua");
289    let csi_title = Style::color("yellow");
290    let csi_reset = Style::reset();
291    println!(
292        "{}Usage:{} help {}[<command>]{}",
293        csi_title, csi_reset, csi_option, csi_reset
294    );
295}