Skip to content

Commit

Permalink
Added help command
Browse files Browse the repository at this point in the history
  • Loading branch information
BobDaGithubAccount committed Dec 14, 2024
1 parent 6cc4e27 commit f84b483
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 22 additions & 0 deletions lib/src/commands.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<String>) {
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));
}
}
}
}
5 changes: 1 addition & 4 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(special_module_name)]

pub mod commands;
use crate::commands::Command;

use wasm_bindgen::prelude::*;

Expand All @@ -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));
Expand Down Expand Up @@ -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 <degrees>");

commands::init();
}

fn reset_camera_command(args: Vec<String>) {
Expand Down

0 comments on commit f84b483

Please sign in to comment.