Strategy formatting

This commit is contained in:
2026-07-25 18:20:22 +02:00
parent 009f8b76b3
commit 7290154025
3 changed files with 97 additions and 15 deletions
+52 -14
View File
@@ -110,16 +110,16 @@ impl Formatted for Position {
}
}
struct Property(&'static str, String);
struct Pair<'a>(&'a str, &'a str);
impl Formatted for MarketOverview {
fn get_formatted(&self) -> Vec<String> {
let mut o = vec![
Property("TREND", format!("{}", self.trend)),
Property("VOLATILITY", format!("{}", self.volatility)),
Property(
Pair("TREND", &format!("{}", self.trend)),
Pair("VOLATILITY", &format!("{}", self.volatility)),
Pair(
"PRESSURE",
if self.pressure.is_sign_positive() {
&if self.pressure.is_sign_positive() {
format!("\x1b[32m{:.3}\x1b[0m", self.pressure)
} else {
format!("\x1b[31m{:.3}\x1b[0m", self.pressure)
@@ -140,21 +140,44 @@ impl Formatted for MarketOverview {
impl Formatted for Status {
fn get_formatted(&self) -> Vec<String> {
vec![
Property("Feed", format!("{:?}", self.feed)),
Property("Exchange", self.exchange.clone()),
Property("DEX", self.dex.clone()),
Property("Latency", format!("{} ms", self.latency)),
Pair("Feed", &format!("{:?}", self.feed)),
Pair("Exchange", &self.exchange.clone()),
Pair("DEX", &self.dex.clone()),
Pair("Latency", &format!("{} ms", self.latency)),
]
.get_formatted()
}
}
impl Formatted for Property {
impl<'a> Formatted for Pair<'a> {
fn get_formatted(&self) -> Vec<String> {
vec![self.0.to_string(), self.1.clone()]
vec![self.0.to_string(), self.1.to_string()]
}
}
fn visible_len(s: &str) -> usize {
let mut len = 0;
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '\x1b' {
if chars.peek() == Some(&'[') {
chars.next();
while let Some(c) = chars.next() {
if c.is_ascii_alphabetic() {
break;
}
}
}
} else {
len += c.len_utf8();
}
}
len
}
impl<T: Formatted> Formatted for Vec<T> {
fn get_formatted(&self) -> Vec<String> {
let mut output = Vec::new();
@@ -171,7 +194,7 @@ impl<T: Formatted> Formatted for Vec<T> {
for row in &rows {
for (i, col) in row.iter().enumerate() {
widths[i] = widths[i].max(col.len());
widths[i] = widths[i].max(visible_len(col));
}
}
@@ -181,7 +204,8 @@ impl<T: Formatted> Formatted for Vec<T> {
for (col_idx, col) in row.iter().enumerate() {
let width = widths[col_idx];
row_out.push_str(&format!("{:<width$}", col, width = width));
row_out.push_str(col);
row_out.push_str(&" ".repeat(width.saturating_sub(visible_len(col))));
if col_idx + 1 != row.len() {
row_out.push_str(" ");
@@ -197,7 +221,21 @@ impl<T: Formatted> Formatted for Vec<T> {
impl Formatted for Strategy {
fn get_formatted(&self) -> Vec<String> {
vec![format!("1"), format!("2")]
vec![
Pair("", ""),
Pair("\x1b[2mName\x1b[0m", "\x1b[2mRisk\x1b[0m"),
Pair(&self.strategy.name.to_string(), &self.risk.name.to_string()),
Pair("", ""),
Pair("\x1b[2mMode\x1b[0m", "\x1b[2mState\x1b[0m"),
Pair(&self.mode.to_string(), &self.state.to_string()),
Pair("", ""),
Pair("\x1b[2mCooldown\x1b[0m", "\x1b[2mTimeframes\x1b[0m"),
Pair(
&format!("{:?} ({:?} rec)", self.cooldown, self.risk.cooldown),
&format!("{:?}", self.strategy.timeframes),
),
]
.get_formatted()
}
}