table struct

This commit is contained in:
Love 2025-01-04 20:19:54 +01:00
parent 95504990b2
commit 4a83bb2b39
4 changed files with 95 additions and 0 deletions

17
Cargo.lock generated
View File

@ -184,6 +184,16 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "colored"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
dependencies = [
"lazy_static",
"windows-sys 0.59.0",
]
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -217,6 +227,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"colored",
"quick-xml",
"reqwest",
"serde",
@ -661,6 +672,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.169"

View File

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.95"
clap = { version = "4.5.23", features = ["derive"] }
colored = "2.2.0"
quick-xml = { version = "0.37.2", features = ["async-tokio", "tokio"] }
reqwest = "0.12.12"
serde = { version = "1.0.217", features = ["derive", "rc"] }

View File

@ -1,6 +1,7 @@
pub mod cli;
pub mod parsing;
pub mod models;
pub mod table;
pub mod ecb_url {
pub const TODAY: &'static str = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";

76
src/table.rs Normal file
View File

@ -0,0 +1,76 @@
use colored::*;
use std::fmt::Display;
struct Table {
header: Option<String>,
column_left: String,
column_right: String,
rows: Vec<String>,
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,
}
}
fn disable_header(&mut self) {
self.header = None
}
fn set_header(&mut self, header: String) {
self.header = Some(header);
}
fn add_row(&mut self, row_left: String, row_right: String) {
self.rows.push(row_left);
self.rows.push(row_right);
}
}
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,
"{}{}{}\n",
middle_padding,
header.bold().cyan(),
middle_padding
)?;
}
let right_padding_amount = self.width - self.column_left.len();
let right_padding = " ".repeat(right_padding_amount);
writeln!(
f,
"{}{}{}\n",
self.column_left.bold().yellow(),
right_padding,
self.column_right.bold().yellow()
)?;
writeln!(f, "{}\n", "-".repeat(self.width))?;
for i in 1..(self.rows.len() - 2) {
let left = &self.rows[i];
let right = &self.rows[i + 1];
let padding_amount = (self.width - left.len() - right.len()) / 2;
let padding = " ".repeat(padding_amount);
writeln!(f, "{}{}{}\n", left.bold().green(), padding, right)?;
}
todo!()
}
}