modcli/
shell_commands.rs

1use std::sync::{Mutex, OnceLock};
2
3/// Shell command struct with metadata
4#[derive(Clone)]
5pub struct ShellCommand {
6    pub name: &'static str,
7    pub aliases: &'static [&'static str],
8    pub help: &'static str,
9    pub handler: fn(input: &str) -> bool,
10}
11
12static HOOKS: OnceLock<Mutex<Vec<ShellCommand>>> = OnceLock::new();
13
14/// Register a new shell-only command from a parent application
15pub fn register(cmd: ShellCommand) {
16    HOOKS
17        .get_or_init(|| Mutex::new(Vec::new()))
18        .lock()
19        .unwrap()
20        .push(cmd);
21}
22
23/// Dispatches shell input to registered handlers.
24/// Returns true if any handler accepted the input.
25pub fn dispatch(input: &str) -> bool {
26    let input = input.trim();
27    if let Some(cmds) = HOOKS.get() {
28        for cmd in cmds.lock().unwrap().iter() {
29            if cmd.name == input || cmd.aliases.contains(&input) {
30                return (cmd.handler)(input);
31            }
32        }
33    }
34    false
35}
36
37/// Returns all registered shell commands (for dynamic help)
38pub fn list() -> Vec<ShellCommand> {
39    if let Some(cmds) = HOOKS.get() {
40        cmds.lock().unwrap().clone()
41    } else {
42        vec![]
43    }
44}