From 964458007e08819d5236a0576919e8e16d0e98ce Mon Sep 17 00:00:00 2001 From: Nikolas Schmidt-Voigt Date: Fri, 30 Jul 2021 22:49:02 +0200 Subject: [PATCH] make grouped table output more readable --- Cargo.lock | 45 +++++++++++++++++++++++-------- Cargo.toml | 2 +- src/output.rs | 27 +++++++++++++------ src/table.rs | 75 +++++++++++++++++++++++++++++++++++---------------- 4 files changed, 106 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8dc7bd0..539d7eb 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,15 +11,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anyhow" version = "1.0.42" @@ -47,10 +38,10 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" name = "bartib" version = "0.1.0" dependencies = [ - "ansi_term 0.12.1", "anyhow", "chrono", "clap", + "nu-ansi-term", "thiserror", ] @@ -79,7 +70,7 @@ version = "2.33.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" dependencies = [ - "ansi_term 0.11.0", + "ansi_term", "atty", "bitflags", "strsim", @@ -88,6 +79,12 @@ dependencies = [ "vec_map", ] +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "hermit-abi" version = "0.1.18" @@ -97,12 +94,32 @@ dependencies = [ "libc", ] +[[package]] +name = "itertools" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" +dependencies = [ + "either", +] + [[package]] name = "libc" version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" +[[package]] +name = "nu-ansi-term" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccf5ce0705f83c8afb776d00fe071ab994148efdd8e060909c6614b0fe740af" +dependencies = [ + "itertools", + "overload", + "winapi", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -122,6 +139,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "proc-macro2" version = "1.0.24" diff --git a/Cargo.toml b/Cargo.toml index 52f8c25..8548136 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,4 @@ chrono = "0.4" clap = "~2.33" thiserror = "1.0" anyhow = "1.0.42" -ansi_term = "0.12.1" \ No newline at end of file +nu-ansi-term = "0.34.0" \ No newline at end of file diff --git a/src/output.rs b/src/output.rs index 0850c5d..2d6e316 100755 --- a/src/output.rs +++ b/src/output.rs @@ -1,4 +1,4 @@ -use ansi_term::Colour; +use nu_ansi_term::Color; use chrono::NaiveDate; use std::collections::BTreeMap; @@ -37,13 +37,24 @@ pub fn list_activities_grouped_by_date(activities: &[&activity::Activity]) { return; } - let activities_by_date = group_activities_by_date(activities); + let mut activity_table = table::Table::new(vec![ + "Started".to_string(), + "Stopped".to_string(), + "Description".to_string(), + "Project".to_string(), + "Duration".to_string(), + ]); - for (date, activity_list) in activities_by_date { - println!("{}", date); - list_activities(&activity_list, false); - println!(); - } + group_activities_by_date(activities).iter() + .map(|(date, activity_list)| create_activites_group(&format!("{}", date), activity_list.as_slice())) + .for_each(|g| activity_table.add_group(g)); + + println!("\n{}", activity_table); +} + +fn create_activites_group(title: &str, activities: &[&activity::Activity]) -> table::Group { + let rows = activities.iter().map(|a| get_activity_table_row(&a, false)).collect(); + table::Group::new(Some(title.to_string()), rows) } // displays a table with running activities (no end time) @@ -118,7 +129,7 @@ fn get_activity_table_row(activity: &&activity::Activity, with_start_dates: bool ]); if !activity.is_stopped() { - new_row.set_color(Colour::Green); + new_row.set_color(Color::Green.normal()); } new_row diff --git a/src/table.rs b/src/table.rs index d30670f..b468fd1 100755 --- a/src/table.rs +++ b/src/table.rs @@ -1,13 +1,12 @@ use std::cmp; use std::fmt; -use std::fmt::Formatter; use std::str; -use ansi_term::{Colour, Style}; +use nu_ansi_term::Style; pub struct Row { content: Vec, - color: Option, + style: Option