Add comfy tables

This commit is contained in:
2022-11-16 17:49:50 +01:00
parent 8a1b95ccba
commit 139fd6a5a9
4 changed files with 138 additions and 8 deletions

90
Cargo.lock generated
View File

@@ -78,6 +78,18 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "comfy-table"
version = "6.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1090f39f45786ec6dc6286f8ea9c75d0a7ef0a0d3cda674cef0c3af7b307fbc2"
dependencies = [
"crossterm",
"strum",
"strum_macros",
"unicode-width",
]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.3" version = "0.9.3"
@@ -94,6 +106,31 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "crossterm"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67"
dependencies = [
"bitflags",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "cxx" name = "cxx"
version = "1.0.81" version = "1.0.81"
@@ -250,6 +287,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"
@@ -686,6 +729,12 @@ dependencies = [
"winreg", "winreg",
] ]
[[package]]
name = "rustversion"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.11" version = "1.0.11"
@@ -780,6 +829,27 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "signal-hook"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
dependencies = [
"libc",
"mio",
"signal-hook",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.0" version = "1.4.0"
@@ -814,6 +884,25 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "strum"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
[[package]]
name = "strum_macros"
version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.103" version = "1.0.103"
@@ -966,6 +1055,7 @@ name = "tuemensa"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"comfy-table",
"reqwest", "reqwest",
"serde", "serde",
"serde_derive", "serde_derive",

View File

@@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.23" chrono = "0.4.23"
comfy-table = "6.1.2"
reqwest = { version = "0.11", features = ["json"] } reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1.0.147", features = ["derive"] } serde = { version = "1.0.147", features = ["derive"] }
serde_derive = "1.0.147" serde_derive = "1.0.147"

View File

@@ -1,4 +1,7 @@
use mensa::Mealplan; use mensa::Mealplan;
use comfy_table::*;
use comfy_table::presets::UTF8_FULL;
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
mod mensa; mod mensa;
@@ -6,16 +9,41 @@ mod mensa;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let shedhalle = mensa::Mensa::from(mensa::MensaName::Shedhalle).await?; let shedhalle = mensa::Mensa::from(mensa::MensaName::Shedhalle);
if let mensa::Mensa::Shedhalle(resp) = shedhalle { let morgenstelle = mensa::Mensa::from(mensa::MensaName::Morgenstelle);
//println!("{:#?}", resp); if let mensa::Mensa::Shedhalle(resp) = shedhalle.await? {
//println!("{}", resp.today()); let data = resp.today().iter().map(|&x| x.get_short_info()).collect::<Vec<(&str, String, &str)>>();
for a in resp.today().iter() { table_short(data).await;
a.print_very_short_info();
} }
if let mensa::Mensa::Morgenstelle(resp) = morgenstelle.await? {
let data = resp.today().iter().map(|&x| x.get_short_info()).collect::<Vec<(&str, String, &str)>>();
table_short(data).await;
} }
Ok(()) Ok(())
} }
async fn table_short(data: Vec<(&str, String, &str)>) {
let mut table = Table::new();
//let mut data_cells = data.iter().map(|x| vec![Cell::new(x.0), Cell::new(&x.1), Cell::new(x.2)]).collect::<Vec<Vec<Cell>>>();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
//.set_width(40)
.set_header(vec!["Art", "Beschreibung", "Preis (Student)"]);
for d in data.iter() {
table.add_row(vec![Cell::new(d.0), Cell::new(&d.1), Cell::new(d.2)]);
}
// Set the default alignment for the third column to right
let column = table.column_mut(2).expect("Our table has three columns");
column.set_cell_alignment(CellAlignment::Right);
println!("{table}");
}

View File

@@ -75,6 +75,17 @@ pub struct MensaMorgenstelle {
canteen: Canteen, canteen: Canteen,
} }
impl Mealplan for MensaMorgenstelle {
fn id(&self) -> &str {
&self.canteen.canteen_id
}
fn today(&self) -> Vec<&Menu> {
let local = format!("{}", Local::now().format("%Y-%m-%d"));
self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect()
}
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Canteen { pub struct Canteen {