diff --git a/lib/src/commands.rs b/lib/src/commands.rs index 3d47b9f..73e9cc9 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::sync::Mutex; use lazy_static::lazy_static; +use crate::log; pub struct Command { pub name: &'static str, @@ -27,4 +28,25 @@ macro_rules! register_command { usage: $usage, }); }; +} + +pub fn init() { + register_command!("help", help_command, "Prints help information", "help [command]"); +} + +fn help_command(args: Vec) { + let registry = COMMAND_REGISTRY.lock().unwrap(); + if args.is_empty() { + for command in registry.values() { + log(format!("{}: {} | {}", command.name, command.help, command.usage)); + } + } else { + for arg in args { + if let Some(command) = registry.get(&arg[..]) { + log(format!("{}: {} | {}", command.name, command.help, command.usage)); + } else { + log(format!("Command not found: {}", arg)); + } + } + } } \ No newline at end of file diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 4853da1..48915ba 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,7 +1,6 @@ #![allow(special_module_name)] pub mod commands; -use crate::commands::Command; use wasm_bindgen::prelude::*; @@ -18,8 +17,6 @@ lazy_static! { #[cfg(target_arch = "wasm32")] #[wasm_bindgen(start)] pub fn start() -> Result<(), JsValue> { - use commands::register_command; - console_log::init_with_level(log::Level::Debug).unwrap(); info!("Logging works!"); std::panic::set_hook(Box::new(console_error_panic_hook::hook)); @@ -150,7 +147,7 @@ pub fn log(message: String) { pub fn register_commands() { register_command!("reset_camera", reset_camera_command, "Reset camera to default settings", "reset_camera"); register_command!("fov", update_camera_fov_command, "Update camera field of view (FOV)", "fov "); - + commands::init(); } fn reset_camera_command(args: Vec) {