From 83f4e21b2e315c365c2041aefd2366435267de37 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 21 Dec 2025 23:10:37 +0100 Subject: [PATCH] Better cli (added help) --- src/cli.rs | 36 ++++++++++++++++++++++++++---------- src/server.rs | 5 ++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 55f9347..1dea2aa 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,6 +7,21 @@ use crate::{logger, plugin::loader::PluginLoader, server::Server}; logger!(LOGGER "CLI"); +macro_rules! commands { + (($cmdi:expr) $($cmd:literal $desc:literal => $body:block)*) => { + match $cmdi { + "help" => { + LOGGER.info("List of commands:"); + $( + println!("\x1b[34m{}\x1b[0m: {}", $cmd, format!($desc)); + )* + }, + $($cmd => $body)* + _ => LOGGER.error(format!("Command not found: {}", $cmdi)), + } + }; +} + pub fn parse_args(input: &str) -> Vec { let mut args = Vec::new(); let mut current = String::new(); @@ -69,7 +84,7 @@ pub fn start_cli(server: Arc, plugin_loader: PluginLoader) { let mut rl = DefaultEditor::new().unwrap(); loop { - let line = match rl.readline("> ") { + let line = match rl.readline("\n> ") { Ok(line) => { rl.add_history_entry(line.as_str()).ok(); line @@ -87,8 +102,9 @@ pub fn start_cli(server: Arc, plugin_loader: PluginLoader) { continue; } - match args[0].as_str() { - "install" => { + commands! { + (args[0].as_str()) + "install" "Installs the specified plugin .vxp package" => { if require_args(&args, &[""]) { let path = PathBuf::from_str(&args[1]).unwrap(); if path.extension().and_then(|s| s.to_str()) == Some("vxp") { @@ -105,20 +121,20 @@ pub fn start_cli(server: Arc, plugin_loader: PluginLoader) { } } } - "load" => { + "load" "loads a plugin that has been installed" => { if require_args(&args, &[""]) { plugin_loader.load(&server.root.join("plugins").join(&args[1])); } } - "ping" => { - LOGGER.info("pong"); - } - "shutdown" => { + "shutdown" "Softly shuts the server down, may not fully shut everything down" => { server.shutdown(); break; } - _ => LOGGER.error(format!("Command not found: {}", args[0])), - } + + "ping" "replies with pong" => { + LOGGER.info("pong"); + } + }; } }); } diff --git a/src/server.rs b/src/server.rs index 49dbcea..505a8f1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -118,7 +118,10 @@ impl Server { let listener = TcpListener::bind(format!("0.0.0.0:{}", self.config.port))?; Self::LOGGER.info(format!("Server listening at 0.0.0.0:{}", self.config.port)); - println!("{WELCOME}\nversion {}\n", env!("CARGO_PKG_VERSION")); + println!( + "{WELCOME}\nversion {}\nType 'help' to see available commands.", + env!("CARGO_PKG_VERSION") + ); for stream in listener.incoming() { match stream {