This commit is contained in:
2026-07-30 04:44:21 +02:00
parent 8dcf6b2a41
commit 87e3c2b964
8 changed files with 95 additions and 236 deletions
+61 -47
View File
@@ -1,3 +1,4 @@
use hypersdk::hypercore::Side;
use pulse_sdk::prelude::*;
pub trait Formatted {
@@ -22,23 +23,6 @@ impl Formatted for InspectTarget {
}
}
impl Formatted for Alert {
fn get_formatted(&self) -> Vec<String> {
vec![
format!(
"{}{}\x1b[0m",
match &self.level {
AlertLevel::Low => "\x1b[34m",
AlertLevel::Medium => "\x1b[33m",
AlertLevel::High => "\x1b[31m",
},
self.level
),
self.message.clone(),
]
}
}
impl Formatted for EventLog {
fn get_formatted(&self) -> Vec<String> {
vec![
@@ -64,7 +48,7 @@ impl Formatted for SignalStatus {
Ok(signal) => {
vec![
format!("\x1b[33mOK\x1b[0m"),
if matches!(signal.kind, Direction::Buy) {
if matches!(signal.side, Side::Ask) {
format!("\x1b[32mBUY\x1b[0m")
} else {
format!("\x1b[31mSELL\x1b[0m")
@@ -78,7 +62,7 @@ impl Formatted for SignalStatus {
Err(signal) => {
vec![
format!("\x1b[31mERR\x1b[0m"),
if matches!(signal.kind, Direction::Buy) {
if matches!(signal.side, Side::Ask) {
format!("\x1b[32mBUY\x1b[0m")
} else {
format!("\x1b[31mSELL\x1b[0m")
@@ -115,16 +99,16 @@ impl Formatted for Position {
fn get_formatted(&self) -> Vec<String> {
vec![
self.symbol.to_string(),
format_f64(self.size),
format_f64(self.size.as_f64()),
self.entry_price.to_string(),
format!(
"{}{}\x1b[0m",
if self.profit.0.is_sign_positive() {
if self.pnl.is_sign_positive() {
"\x1b[32m"
} else {
"\x1b[31m"
},
self.profit,
self.pnl,
),
]
}
@@ -134,31 +118,6 @@ struct Pair<'a>(&'a str, &'a str);
struct Triple<'a>(&'a str, &'a str, &'a str);
impl Formatted for MarketOverview {
fn get_formatted(&self) -> Vec<String> {
let mut o = vec![
Pair("TREND", &format!("{}", self.trend)),
Pair("VOLATILITY", &format!("{}", self.volatility)),
Pair(
"PRESSURE",
&if self.pressure.is_sign_positive() {
format!("\x1b[32m{:.3}\x1b[0m", self.pressure)
} else {
format!("\x1b[31m{:.3}\x1b[0m", self.pressure)
},
),
]
.get_formatted();
o.push(format!(
"\n{}",
apply_padding(self.alerts.get_formatted()).join("\n")
));
o
}
}
impl Formatted for Status {
fn get_formatted(&self) -> Vec<String> {
vec![
@@ -272,3 +231,58 @@ pub fn apply_padding(mut items: Vec<String>) -> Vec<String> {
items
}
pub fn format_f64(value: f64) -> String {
let abs = value.abs();
let (divisor, suffix) = if abs >= 1_000_000_000.0 {
(1_000_000_000.0, "B")
} else if abs >= 1_000_000.0 {
(1_000_000.0, "M")
} else if abs >= 1_000.0 {
(1_000.0, "K")
} else {
(1.0, "")
};
if divisor != 1.0 {
let formatted = value / divisor;
// Remove unnecessary trailing zeros
let s = format!("{:.2}", formatted)
.trim_end_matches('0')
.trim_end_matches('.')
.to_string();
return format!("{}{}", s, suffix);
}
let val = format!("{:.3}", value);
let parts: Vec<&str> = val.split('.').collect();
let int = parts[0].to_string();
let negative = int.starts_with('-');
let start = if negative { 1 } else { 0 };
let mut result = String::new();
for (i, c) in int[start..].chars().rev().enumerate() {
if i > 0 && i % 3 == 0 {
result.push(',');
}
result.push(c);
}
let mut formatted: String = result.chars().rev().collect();
if negative {
formatted.insert(0, '-');
}
if parts.len() > 1 {
formatted.push('.');
formatted.push_str(parts[1]);
}
formatted
}