From 3386e940957e31c5800b0a8d4bd21fda6dd2a4b1 Mon Sep 17 00:00:00 2001 From: Nikolas Schmidt-Voigt Date: Thu, 30 Sep 2021 23:35:43 +0200 Subject: [PATCH 1/4] calculate wrapped column width in tables --- Cargo.lock | 44 ++++++++-- Cargo.toml | 3 +- src/view/list.rs | 40 +++++---- src/view/table.rs | 206 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 250 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 539d7eb..2247899 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,7 +8,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" dependencies = [ - "winapi", + "winapi 0.3.9", ] [[package]] @@ -25,7 +25,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -42,6 +42,7 @@ dependencies = [ "chrono", "clap", "nu-ansi-term", + "term_size", "thiserror", ] @@ -61,7 +62,7 @@ dependencies = [ "num-integer", "num-traits", "time", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -103,6 +104,16 @@ dependencies = [ "either", ] +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + [[package]] name = "libc" version = "0.2.86" @@ -117,7 +128,7 @@ checksum = "cccf5ce0705f83c8afb776d00fe071ab994148efdd8e060909c6614b0fe740af" dependencies = [ "itertools", "overload", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -180,6 +191,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "term_size" +version = "1.0.0-beta1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8a17d8699e154863becdf18e4fd28bd0be27ca72856f54daf75c00f2566898f" +dependencies = [ + "kernel32-sys", + "libc", + "winapi 0.2.8", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -217,7 +239,7 @@ checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", "wasi", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -244,6 +266,12 @@ version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" + [[package]] name = "winapi" version = "0.3.9" @@ -254,6 +282,12 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 8548136..b55d9cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ chrono = "0.4" clap = "~2.33" thiserror = "1.0" anyhow = "1.0.42" -nu-ansi-term = "0.34.0" \ No newline at end of file +nu-ansi-term = "0.34.0" +term_size = "1.0.0-beta1" \ No newline at end of file diff --git a/src/view/list.rs b/src/view/list.rs index 0e69bb9..48dbd35 100644 --- a/src/view/list.rs +++ b/src/view/list.rs @@ -14,13 +14,7 @@ pub fn list_activities(activities: &[&activity::Activity], with_start_dates: boo return; } - let mut activity_table = table::Table::new(vec![ - "Started".to_string(), - "Stopped".to_string(), - "Description".to_string(), - "Project".to_string(), - "Duration".to_string(), - ]); + let mut activity_table = create_activity_table(); activities .iter() @@ -37,13 +31,7 @@ pub fn list_activities_grouped_by_date(activities: &[&activity::Activity]) { return; } - let mut activity_table = table::Table::new(vec![ - "Started".to_string(), - "Stopped".to_string(), - "Description".to_string(), - "Project".to_string(), - "Duration".to_string(), - ]); + let mut activity_table = create_activity_table(); group_activities_by_date(activities) .iter() @@ -55,6 +43,16 @@ pub fn list_activities_grouped_by_date(activities: &[&activity::Activity]) { println!("\n{}", activity_table); } +fn create_activity_table() -> table::Table { + table::Table::new(vec![ + table::Column{ label: "Started".to_string(), wrap: table::Wrap::NoWrap }, + table::Column{ label: "Stopped".to_string(), wrap: table::Wrap::NoWrap }, + table::Column{ label: "Description".to_string(), wrap: table::Wrap::Wrap }, + table::Column{ label: "Project".to_string(), wrap: table::Wrap::Wrap }, + table::Column{ label: "Duration".to_string(), wrap: table::Wrap::NoWrap }, + ]) +} + fn create_activites_group(title: &str, activities: &[&activity::Activity]) -> table::Group { let rows = activities .iter() @@ -69,10 +67,10 @@ pub fn list_running_activities(running_activities: &[&activity::Activity]) { println!("No Activity is currently running"); } else { let mut activity_table = table::Table::new(vec![ - "Started At".to_string(), - "Description".to_string(), - "Project".to_string(), - "Duration".to_string(), + table::Column{ label: "Started At".to_string(), wrap: table::Wrap::NoWrap }, + table::Column{ label: "Description".to_string(), wrap: table::Wrap::Wrap }, + table::Column{ label: "Project".to_string(), wrap: table::Wrap::Wrap }, + table::Column{ label: "Duration".to_string(), wrap: table::Wrap::NoWrap }, ]); running_activities @@ -97,9 +95,9 @@ pub fn list_descriptions_and_projects(descriptions_and_projects : &[(&String, &S println!("No activities have been tracked yet"); } else { let mut descriptions_and_projects_table = table::Table::new(vec![ - " # ".to_string(), - "Description".to_string(), - "Project".to_string() + table::Column{ label: " # ".to_string(), wrap: table::Wrap::NoWrap }, + table::Column{ label: "Description".to_string(), wrap: table::Wrap::Wrap }, + table::Column{ label: "Project".to_string(), wrap: table::Wrap::Wrap }, ]); let mut i = descriptions_and_projects.len(); diff --git a/src/view/table.rs b/src/view/table.rs index 754c2b2..c166815 100644 --- a/src/view/table.rs +++ b/src/view/table.rs @@ -4,6 +4,16 @@ use std::str; use nu_ansi_term::Style; +pub enum Wrap { + Wrap, + NoWrap +} + +pub struct Column { + pub label: String, + pub wrap: Wrap +} + pub struct Row { content: Vec, style: Option