From 9c7b492ac9aa7ff0e1b7862b1caa061bdc4b1710 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 15 Jul 2026 22:53:18 +0200 Subject: [PATCH] Tidy up --- src/command.rs | 4 +- src/formatting.rs | 101 +++++++++++++++++-------------------- src/main.rs | 34 ++++++------- src/types.rs | 125 ++++++++++++++++++++++++++++++---------------- 4 files changed, 146 insertions(+), 118 deletions(-) diff --git a/src/command.rs b/src/command.rs index 29ce0a3..352f1a1 100644 --- a/src/command.rs +++ b/src/command.rs @@ -6,7 +6,7 @@ impl PulseTradeApp { return; } - let (command, args) = if let Some((command, args)) = command.split_once(" ") { + let (command, _args) = if let Some((command, args)) = command.split_once(" ") { (command, args.split(" ").collect()) } else { (command, Vec::new()) @@ -19,7 +19,7 @@ impl PulseTradeApp { _ => { self.logs.lock().await.push(EventLog { - kind: crate::types::LogKind::ERR, + kind: crate::types::LogKind::Err, name: "cmd", message: format!("Command '{}' not found", command), }); diff --git a/src/formatting.rs b/src/formatting.rs index fa8aded..d8b1557 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -12,52 +12,41 @@ impl Formatted for InspectTarget { match self { Self::None => vec!["\x1b[2mnothing to inspect\x1b[0m".to_string()], - Self::Symbol { - symbol, - price, - trend, - market_trend, - volatility, - pressure, - alerts, - } => vec![ - Property("Symbol", format!("\x1b[35m{}\x1b[0m", symbol)), - Property("Price", format!("\x1b[96m{}\x1b[0m", format_f64(*price))), - Property("Trend", format!("{}", format_f64(*trend))), - Property("Market", format!("{:?}", market_trend)), - Property("Volatility", format!("{:?}", volatility)), - Property("Pressure", format!("{:+.2}%", pressure * 100.0)), - Property("Alerts", format!("{}", alerts.len())), + Self::Symbol(boxed) => { + let (watch, overview) = &**boxed; + vec![ + Property("Symbol", format!("\x1b[35m{}\x1b[0m", watch.symbol)), + Property("Price", format!("\x1b[96m{}\x1b[0m", format_f64(watch.price))), + Property("Trend", format!("{}", format_f64(watch.trend))), + Property("Market", format!("{}", overview.trend)), + Property("Volatility", format!("{}", overview.volatility)), + Property( + "Pressure", + format!("{:+.2}%", overview.pressure * 100.0), + ), + Property("Alerts", format!("{}", overview.alerts.len())), + ] + .get_formatted() + } + + Self::Position(pos) => vec![ + Property("Symbol", format!("\x1b[35m{}\x1b[0m", pos.symbol)), + Property("Profit", format!("{:+.2}", pos.profit)), + Property("Amount", format!("{}", pos.amount)), ] .get_formatted(), - Self::Position { - symbol, - profit, - amount, - } => vec![ - Property("Symbol", format!("\x1b[35m{}\x1b[0m", symbol)), - Property("Profit", format!("{:+.2}", profit)), - Property("Amount", format!("{}", amount)), + Self::Signal(sig) => vec![ + Property("Type", format!("{}", sig.kind)), + Property("Symbol", format!("\x1b[35m{}\x1b[0m", sig.symbol)), + Property("Parameter", format!("{}", sig.param)), + Property("Price", format!("\x1b[96m{}\x1b[0m", format_f64(sig.price))), ] .get_formatted(), - Self::Signal { - kind, - symbol, - param, - price, - } => vec![ - Property("Type", format!("{:?}", kind)), - Property("Symbol", format!("\x1b[35m{}\x1b[0m", symbol)), - Property("Parameter", format!("{:?}", param)), - Property("Price", format!("\x1b[96m{}\x1b[0m", format_f64(*price))), - ] - .get_formatted(), - - Self::Alert { level, message } => vec![ - Property("Level", format!("{:?}", level)), - Property("Message", message.clone()), + Self::Alert(alert) => vec![ + Property("Level", format!("{}", alert.level)), + Property("Message", alert.message.clone()), ] .get_formatted(), } @@ -68,11 +57,11 @@ impl Formatted for Alert { fn get_formatted(&self) -> Vec { vec![ format!( - "{}{:?}\x1b[0m", + "{}{}\x1b[0m", match &self.level { - AlertLevel::L => "\x1b[34m", - AlertLevel::M => "\x1b[33m", - AlertLevel::H => "\x1b[31m", + AlertLevel::Low => "\x1b[34m", + AlertLevel::Medium => "\x1b[33m", + AlertLevel::High => "\x1b[31m", }, self.level ), @@ -85,12 +74,12 @@ impl Formatted for EventLog { fn get_formatted(&self) -> Vec { vec![ format!( - "{}{:?}\x1b[0m", + "{}{}\x1b[0m", match &self.kind { - LogKind::INFO => "\x1b[34m", - LogKind::DEBUG => "\x1b[36m", - LogKind::ERR => "\x1b[31m", - LogKind::WARN => "\x1b[33m", + LogKind::Info => "\x1b[34m", + LogKind::Debug => "\x1b[36m", + LogKind::Err => "\x1b[31m", + LogKind::Warn => "\x1b[33m", }, self.kind ), @@ -103,13 +92,13 @@ impl Formatted for EventLog { impl Formatted for Signal { fn get_formatted(&self) -> Vec { vec![ - if matches!(self.kind, SignalKind::BUY) { - format!("\x1b[32m{:?}\x1b[0m", self.kind) + if matches!(self.kind, SignalKind::Buy) { + format!("\x1b[32m{}\x1b[0m", self.kind) } else { - format!("\x1b[31m{:?}\x1b[0m", self.kind) + format!("\x1b[31m{}\x1b[0m", self.kind) }, format!("\x1b[35m{}\x1b[0m", self.symbol), - format!("\x1b[34m{:?}\x1b[0m", self.param), + format!("\x1b[34m{}\x1b[0m", self.param), format_f64(self.price), ] } @@ -151,13 +140,13 @@ impl Formatted for ActivePosition { } } -pub struct Property(&'static str, String); +struct Property(&'static str, String); impl Formatted for MarketOverview { fn get_formatted(&self) -> Vec { vec![ - Property("TREND", format!("{:?}", self.trend)), - Property("VOLATILITY", format!("{:?}", self.volatility)), + Property("TREND", format!("{}", self.trend)), + Property("VOLATILITY", format!("{}", self.volatility)), Property( "PRESSURE", if self.pressure.is_sign_positive() { diff --git a/src/main.rs b/src/main.rs index 35882a4..1fd204c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,30 +88,30 @@ impl App for PulseTradeApp { let mut signals = self.signals.lock().await; signals.push(Signal { - kind: types::SignalKind::BUY, + kind: types::SignalKind::Buy, symbol: "BTC".to_string(), - param: types::SignalParameter::LIM, + param: types::SignalParameter::Lim, price: 118_800.0, }); signals.push(Signal { - kind: types::SignalKind::BUY, + kind: types::SignalKind::Buy, symbol: "BTC".to_string(), - param: types::SignalParameter::TAP, + param: types::SignalParameter::Tap, price: 120_000.0, }); signals.push(Signal { - kind: types::SignalKind::BUY, + kind: types::SignalKind::Buy, symbol: "BTC".to_string(), - param: types::SignalParameter::STL, + param: types::SignalParameter::Stl, price: 118_000.0, }); let mut logs = self.logs.lock().await; logs.push(EventLog { - kind: types::LogKind::WARN, + kind: types::LogKind::Warn, name: "pulse.init", message: "We're still not done yet ;)".to_string(), }); @@ -119,17 +119,17 @@ impl App for PulseTradeApp { let mut market_overview = self.market_overview.lock().await; market_overview.alerts.push(Alert { - level: types::AlertLevel::H, + level: types::AlertLevel::High, message: "BTC funding rate elevated".to_string(), }); market_overview.alerts.push(Alert { - level: types::AlertLevel::M, + level: types::AlertLevel::Medium, message: "Market volatility increasing".to_string(), }); market_overview.alerts.push(Alert { - level: types::AlertLevel::L, + level: types::AlertLevel::Low, message: "ETH volatility returning to normal".to_string(), }); } @@ -224,11 +224,11 @@ impl App for PulseTradeApp { ), (LayoutItem::Widget(Size::Flex(1)), { let mo = self.market_overview.lock().await; - Box::new(format![ + Box::new(format!( " MARKET OVERVIEW\n{}\n\n{}", apply_padding(mo.get_formatted()).join("\n"), - apply_padding(mo.alerts.get_formatted(),).join("\n") - ]) + apply_padding(mo.alerts.get_formatted()).join("\n") + )) }), ]), ); @@ -252,20 +252,20 @@ impl App for PulseTradeApp { ), ( LayoutItem::Widget(Size::Flex(1)), - Box::new(format![ + Box::new(format!( " STATUS\n{}", apply_padding(self.status.lock().await.get_formatted()).join("\n") - ]), + )), ), ]), ); layout.draw( 5, - format![ + format!( " EVENT LOGS\n{}", apply_padding(self.logs.lock().await.get_formatted()).join("\n") - ], + ), ); layout.draw(6, Input(" > ", &*self.command.lock().await)); diff --git a/src/types.rs b/src/types.rs index f060b54..2cde269 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,9 +1,11 @@ +#[derive(Debug, Clone)] pub struct WatchListItem { pub symbol: String, pub price: f64, pub trend: f64, } +#[derive(Debug, Clone)] pub struct ActivePosition { pub symbol: String, pub profit: f64, @@ -17,6 +19,16 @@ pub enum MarketTrend { Neutral, } +impl std::fmt::Display for MarketTrend { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Bullish => write!(f, "Bullish"), + Self::Bearish => write!(f, "Bearish"), + Self::Neutral => write!(f, "Neutral"), + } + } +} + #[derive(Debug, Clone, Copy)] pub enum Volatility { Low, @@ -24,6 +36,17 @@ pub enum Volatility { High, } +impl std::fmt::Display for Volatility { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Low => write!(f, "Low"), + Self::Medium => write!(f, "Medium"), + Self::High => write!(f, "High"), + } + } +} + +#[derive(Debug, Clone)] pub struct MarketOverview { pub trend: MarketTrend, pub volatility: Volatility, @@ -49,18 +72,39 @@ pub struct Status { #[derive(Debug, Clone, Copy)] pub enum SignalKind { - BUY, - SELL, + Buy, + Sell, +} + +impl std::fmt::Display for SignalKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Buy => write!(f, "BUY"), + Self::Sell => write!(f, "SELL"), + } + } } #[derive(Debug, Clone, Copy)] pub enum SignalParameter { - LIM, - STL, - TAP, - CHK, + Lim, + Stl, + Tap, + Chk, } +impl std::fmt::Display for SignalParameter { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Lim => write!(f, "LIM"), + Self::Stl => write!(f, "STL"), + Self::Tap => write!(f, "TAP"), + Self::Chk => write!(f, "CHK"), + } + } +} + +#[derive(Debug, Clone)] pub struct Signal { pub kind: SignalKind, pub symbol: String, @@ -70,10 +114,21 @@ pub struct Signal { #[derive(Debug, Clone, Copy)] pub enum LogKind { - INFO, - WARN, - ERR, - DEBUG, + Info, + Warn, + Err, + Debug, +} + +impl std::fmt::Display for LogKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Info => write!(f, "INFO"), + Self::Warn => write!(f, "WARN"), + Self::Err => write!(f, "ERR"), + Self::Debug => write!(f, "DEBUG"), + } + } } #[derive(Debug, Clone)] @@ -85,9 +140,19 @@ pub struct EventLog { #[derive(Debug, Clone, Copy)] pub enum AlertLevel { - H, - M, - L, + High, + Medium, + Low, +} + +impl std::fmt::Display for AlertLevel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::High => write!(f, "H"), + Self::Medium => write!(f, "M"), + Self::Low => write!(f, "L"), + } + } } #[derive(Debug, Clone)] @@ -99,34 +164,8 @@ pub struct Alert { #[derive(Debug, Clone)] pub enum InspectTarget { None, - - Symbol { - symbol: String, - price: f64, - trend: f64, - - market_trend: MarketTrend, - volatility: Volatility, - pressure: f64, - - alerts: Vec, - }, - - Position { - symbol: String, - profit: f64, - amount: f64, - }, - - Signal { - kind: SignalKind, - symbol: String, - param: SignalParameter, - price: f64, - }, - - Alert { - level: AlertLevel, - message: String, - }, + Symbol(Box<(WatchListItem, MarketOverview)>), + Position(ActivePosition), + Signal(Signal), + Alert(Alert), }