1use std::sync::{Mutex, OnceLock};
2
3#[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
14pub fn register(cmd: ShellCommand) {
16 HOOKS
17 .get_or_init(|| Mutex::new(Vec::new()))
18 .lock()
19 .unwrap()
20 .push(cmd);
21}
22
23pub 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
37pub fn list() -> Vec<ShellCommand> {
39 if let Some(cmds) = HOOKS.get() {
40 cmds.lock().unwrap().clone()
41 } else {
42 vec![]
43 }
44}