From c45f9a59ca6af245724f9a63c686755ea37d489e Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 21 Dec 2025 14:55:21 +0100 Subject: [PATCH] Improved CLI --- Cargo.toml | 1 + src/cli.rs | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d9da2c4..6a36b56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ chrono = "0.4.42" once_cell = "1.21.3" rand = "0.9.2" rusqlite = "0.37.0" +rustyline = "17.0.2" serde = { version = "1.0.219", features = ["serde_derive"] } serde_json = "1.0.143" sha1 = "0.10.6" diff --git a/src/cli.rs b/src/cli.rs index 7095040..cbeb33f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,6 @@ -use std::{fs::File, io, path::PathBuf, str::FromStr, sync::Arc}; +use std::{fs::File, path::PathBuf, str::FromStr, sync::Arc}; +use rustyline::{DefaultEditor, error::ReadlineError}; use zip::ZipArchive; use crate::{logger, plugin::loader::PluginLoader, server::Server}; @@ -65,11 +66,24 @@ pub fn require_args(args: &[String], required: &[&str]) -> bool { pub fn start_cli(server: Arc, plugin_loader: PluginLoader) { std::thread::spawn(move || { + let mut rl = DefaultEditor::new().unwrap(); + loop { - let mut buf = String::new(); - io::stdin().read_line(&mut buf).unwrap(); - let args = parse_args(buf.trim()); - if args.len() == 0 { + let line = match rl.readline("> ") { + Ok(line) => { + rl.add_history_entry(line.as_str()).ok(); + line + } + Err(ReadlineError::Interrupted) => continue, + Err(ReadlineError::Eof) => break, + Err(err) => { + LOGGER.error(format!("CLI error: {:?}", err)); + break; + } + }; + + let args = parse_args(line.trim()); + if args.is_empty() { continue; } @@ -85,6 +99,7 @@ pub fn start_cli(server: Arc, plugin_loader: PluginLoader) { let mut archive = ZipArchive::new(file).unwrap(); archive.extract(&d).unwrap(); } + LOGGER.info("Installed"); } else { LOGGER.error("File must be .vxp"); } @@ -95,9 +110,10 @@ pub fn start_cli(server: Arc, plugin_loader: PluginLoader) { plugin_loader.load(&server.root.join("plugins").join(&args[1])); } } - _ => { - LOGGER.error(format!("Command not found ({})", args[0])); + "ping" => { + LOGGER.info("pong"); } + _ => LOGGER.error(format!("Command not found: {}", args[0])), } } });