From e61aa231ea90fa486e3fd12766e715acb7b4ce96 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 2 Aug 2026 02:36:23 +0200 Subject: [PATCH] better commands --- src/engine/engine/command.rs | 158 ++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 69 deletions(-) diff --git a/src/engine/engine/command.rs b/src/engine/engine/command.rs index 8fa4f9d..3d18823 100644 --- a/src/engine/engine/command.rs +++ b/src/engine/engine/command.rs @@ -1,7 +1,6 @@ use crate::{engine::Engine, store::config::ConfigManager}; use pulse_sdk::prelude::*; -use toml::Value; impl Engine { pub async fn invalid_command_usage(&self, name: &str) -> tokio::io::Result<()> { @@ -13,7 +12,43 @@ impl Engine { command: &str, mut args: Vec<&str>, ) -> tokio::io::Result<()> { + macro_rules! set_cfg { + ($n:ident) => { + let Ok(Ok($n)) = toml::value::Value::try_from(args[2]).map(|v| v.try_into()) else { + return self + .terminal_server + .error("config::set", "Unable to parse value") + .await; + }; + }; + } + match command { + "help" | "?" => { + self.terminal_server + .info("engine::help", "AVAILABLE COMMANDS") + .await?; + + let commands = [ + ("help", "Show this help message"), + ("config reload", "Reload configuration from disk"), + ("config save", "Save current configuration"), + ("account list", "List available accounts"), + ("account use ", "Switch active account"), + ("preset list", "List available presets"), + ("preset use ", "Switch active preset"), + ("strategy start", "Start strategy runtime"), + ("strategy set ", "Change active strategy"), + ("strategy ", "Send command to strategy runtime"), + ]; + + for (command, description) in commands { + self.terminal_server + .info("engine::help", &format!("{:<30} {}", command, description)) + .await?; + } + } + "config" | "cfg" => { if args.len() == 0 { return self.invalid_command_usage("config").await; @@ -21,10 +56,23 @@ impl Engine { match args[0] { "reload" => { - *self.config.lock().await = ConfigManager::new().await?; + let mut config = self.config.lock().await; + + let new_config = ConfigManager::new().await?; + let strategy_changed = + new_config.get_ref()?.strategy != config.get_ref()?.strategy; + + *config = new_config; + self.terminal_server .info("engine::config", "successfully reloaded") .await?; + + if strategy_changed { + self.strategy_engine + .reload(&config.get_ref()?.strategy) + .await?; + } } "save" => { @@ -34,72 +82,6 @@ impl Engine { .await?; } - "set" => { - macro_rules! set_cfg { - ($n:ident, $v:expr) => { - if let Ok(Ok($n)) = Value::try_from(args[2]).map(|v| v.try_into()) { - $v; - } else { - self.terminal_server - .error("config::set", "Unable to parse value") - .await?; - } - }; - } - - if args.len() == 3 { - match args[1] { - "watchlist" | "watch" | "wl" => { - set_cfg!(v, self.config.lock().await.get_mut()?.watchlist = v); - - self.terminal_server - .info("config::set", "watchlist set successfully, use `config save` to persist changes") - .await?; - } - - "strategy" | "strat" | "sg" => { - set_cfg!(id, { - let id: String = id; - - if !crate::store::pulse_strategy(&id)? - .join("strategy.toml") - .exists() - { - return self - .terminal_server - .error( - "config::set[strategy]", - &format!("Non existent strategy `{id}`"), - ) - .await; - } - - self.strategy_engine.reload(id.as_str()).await?; - self.config.lock().await.get_mut()?.strategy = id; - }); - - self.terminal_server - .info("config::set", "strategy set successfully, use `config save` to persist changes") - .await?; - } - - _ => { - self.terminal_server - .error("config::set", "Invalid usage, available options: watchlist, strategy, cooldown") - .await?; - } - } - - self.terminal_server - .broadcast(TerminalServerMessage::ConfigUpdated( - self.get_config_status().await?, - )) - .await?; - } else { - self.invalid_command_usage("engine::config").await?; - } - } - _ => { self.invalid_command_usage("engine::config").await?; } @@ -254,6 +236,35 @@ impl Engine { match strategy_command { "start" => {} + "set" => { + set_cfg!(id); + + let id: String = id; + + if !crate::store::pulse_strategy(&id)? + .join("strategy.toml") + .exists() + { + return self + .terminal_server + .error( + "config::set[strategy]", + &format!("Non existent strategy `{id}`"), + ) + .await; + } + + self.strategy_engine.reload(id.as_str()).await?; + self.config.lock().await.get_mut()?.strategy = id; + + self.terminal_server + .info( + "config::set", + "strategy set successfully, use `config save` to persist changes", + ) + .await?; + } + _ => { self.strategy_engine .send(&StrategyEngineMessage::Command { @@ -267,11 +278,20 @@ impl Engine { _ => { self.terminal_server - .error("engine::cmd", &format!("command '{}' not found", command)) + .error( + "engine::cmd", + &format!("command '{}' not found, use help or ?", command), + ) .await?; } } + self.terminal_server + .broadcast(TerminalServerMessage::ConfigUpdated( + self.get_config_status().await?, + )) + .await?; + Ok(()) } }