typed Syntax and improved formatting
This commit is contained in:
+12
-66
@@ -3,6 +3,7 @@ use pulse_wire::terminal::{
|
||||
};
|
||||
|
||||
use pulse_wire::general::{Direction, EventLog, LogKind, Position, Signal};
|
||||
use pulse_wire::units::format_f64;
|
||||
|
||||
pub trait Formatted {
|
||||
fn get_formatted(&self) -> Vec<String>;
|
||||
@@ -71,7 +72,7 @@ impl Formatted for Signal {
|
||||
format!("\x1b[31m{}\x1b[0m", self.kind)
|
||||
},
|
||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
||||
format_f64(self.price),
|
||||
self.price.to_string(),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -79,9 +80,9 @@ impl Formatted for Signal {
|
||||
impl Formatted for MarketItem {
|
||||
fn get_formatted(&self) -> Vec<String> {
|
||||
vec![
|
||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
||||
format!("${}", format_f64(self.price)),
|
||||
format!("${}", format_f64(self.volume_24h)),
|
||||
self.symbol.to_string(),
|
||||
self.price.to_string(),
|
||||
self.volume_24h.to_string(),
|
||||
format!(
|
||||
"{} {}",
|
||||
if self.trend.is_sign_positive() {
|
||||
@@ -98,17 +99,17 @@ impl Formatted for MarketItem {
|
||||
impl Formatted for Position {
|
||||
fn get_formatted(&self) -> Vec<String> {
|
||||
vec![
|
||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
||||
self.symbol.to_string(),
|
||||
format_f64(self.size),
|
||||
format_f64(self.entry_price),
|
||||
self.entry_price.to_string(),
|
||||
format!(
|
||||
"{} {}",
|
||||
if self.profit.is_sign_positive() {
|
||||
"\x1b[32m▲\x1b[0m"
|
||||
"{}{}\x1b[0m",
|
||||
if self.profit.0.is_sign_positive() {
|
||||
"\x1b[32m"
|
||||
} else {
|
||||
"\x1b[31m▼\x1b[0m"
|
||||
"\x1b[31m"
|
||||
},
|
||||
format_f64(self.profit.abs())
|
||||
self.profit,
|
||||
),
|
||||
]
|
||||
}
|
||||
@@ -206,58 +207,3 @@ 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user