2025-01-04 20:19:54 +01:00
|
|
|
use colored::*;
|
|
|
|
use std::fmt::Display;
|
|
|
|
|
2025-01-04 20:45:25 +01:00
|
|
|
use crate::models::ExchangeRateResult;
|
|
|
|
|
|
|
|
pub struct Table {
|
2025-01-04 20:19:54 +01:00
|
|
|
header: Option<String>,
|
|
|
|
column_left: String,
|
|
|
|
column_right: String,
|
2025-01-04 20:45:25 +01:00
|
|
|
rows: Vec<(String, String)>,
|
2025-01-04 20:19:54 +01:00
|
|
|
pub color: bool,
|
|
|
|
pub width: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Table {
|
|
|
|
fn new(header: Option<String>, column_left: String, column_right: String) -> Self {
|
|
|
|
Self {
|
|
|
|
header,
|
|
|
|
column_left,
|
|
|
|
column_right,
|
|
|
|
rows: Vec::new(),
|
|
|
|
color: false,
|
|
|
|
width: 21,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-07 15:55:32 +01:00
|
|
|
#[allow(dead_code)]
|
2025-01-04 20:19:54 +01:00
|
|
|
fn disable_header(&mut self) {
|
|
|
|
self.header = None
|
|
|
|
}
|
|
|
|
|
2025-01-07 15:55:32 +01:00
|
|
|
#[allow(dead_code)]
|
2025-01-04 20:19:54 +01:00
|
|
|
fn set_header(&mut self, header: String) {
|
|
|
|
self.header = Some(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_row(&mut self, row_left: String, row_right: String) {
|
2025-01-04 20:45:25 +01:00
|
|
|
self.rows.push((row_left, row_right));
|
2025-01-04 20:19:54 +01:00
|
|
|
}
|
2025-01-07 16:06:22 +01:00
|
|
|
|
|
|
|
pub fn sort(&mut self) {
|
|
|
|
self.rows.sort_by(|a, b| a.1.cmp(&b.1))
|
|
|
|
}
|
2025-01-04 20:19:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Table {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
if let Some(header) = self.header.as_ref() {
|
|
|
|
let middle_padding_amount = (self.width - header.len()) / 2;
|
|
|
|
assert!(middle_padding_amount > 0);
|
|
|
|
let middle_padding = " ".repeat(middle_padding_amount);
|
|
|
|
writeln!(
|
|
|
|
f,
|
2025-01-04 20:45:25 +01:00
|
|
|
"{}{}{}",
|
2025-01-04 20:19:54 +01:00
|
|
|
middle_padding,
|
|
|
|
header.bold().cyan(),
|
|
|
|
middle_padding
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2025-01-04 20:45:25 +01:00
|
|
|
let right_padding_amount = self.width - self.column_left.len() - self.column_right.len();
|
2025-01-04 20:19:54 +01:00
|
|
|
let right_padding = " ".repeat(right_padding_amount);
|
|
|
|
writeln!(
|
|
|
|
f,
|
2025-01-04 20:45:25 +01:00
|
|
|
"{}{}{}",
|
2025-01-04 20:19:54 +01:00
|
|
|
self.column_left.bold().yellow(),
|
|
|
|
right_padding,
|
|
|
|
self.column_right.bold().yellow()
|
|
|
|
)?;
|
2025-01-04 20:45:25 +01:00
|
|
|
writeln!(f, "{}", "-".repeat(self.width))?;
|
2025-01-04 20:19:54 +01:00
|
|
|
|
2025-01-04 20:45:25 +01:00
|
|
|
for (left, right) in self.rows.iter() {
|
|
|
|
let padding_amount = self.width - left.len() - right.len();
|
2025-01-04 20:19:54 +01:00
|
|
|
let padding = " ".repeat(padding_amount);
|
2025-01-04 20:45:25 +01:00
|
|
|
writeln!(f, "{}{}{}", left.bold().green(), padding, right)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ExchangeRateResult> for Table {
|
|
|
|
fn from(value: ExchangeRateResult) -> Self {
|
|
|
|
let mut table = Table::new(Some(value.time), "Currency".to_string(), "Rate".to_string());
|
|
|
|
for (key, val) in value.rates.into_iter() {
|
|
|
|
table.add_row(key, val.to_string());
|
2025-01-04 20:19:54 +01:00
|
|
|
}
|
|
|
|
|
2025-01-04 20:45:25 +01:00
|
|
|
table
|
2025-01-04 20:19:54 +01:00
|
|
|
}
|
|
|
|
}
|