From 45ac9b41329b3e64a8df29a7acfae97fe330f334 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 13 Jan 2018 03:19:41 +0100 Subject: [PATCH] Change list command to use a table for output --- bin/domain/imag-timetrack/Cargo.toml | 1 + bin/domain/imag-timetrack/src/list.rs | 47 ++++++++++++++++++++++----- bin/domain/imag-timetrack/src/main.rs | 3 +- doc/src/09020-changelog.md | 1 + 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/bin/domain/imag-timetrack/Cargo.toml b/bin/domain/imag-timetrack/Cargo.toml index 462d16ac..84bf4fab 100644 --- a/bin/domain/imag-timetrack/Cargo.toml +++ b/bin/domain/imag-timetrack/Cargo.toml @@ -25,6 +25,7 @@ log = "0.3" chrono = "0.4" filters = "0.2" itertools = "0.7" +prettytable-rs = "0.6" libimagstore = { version = "0.6.0", path = "../../../lib/core/libimagstore" } libimagrt = { version = "0.6.0", path = "../../../lib/core/libimagrt" } diff --git a/bin/domain/imag-timetrack/src/list.rs b/bin/domain/imag-timetrack/src/list.rs index 88c61504..2b025093 100644 --- a/bin/domain/imag-timetrack/src/list.rs +++ b/bin/domain/imag-timetrack/src/list.rs @@ -21,6 +21,9 @@ use std::str::FromStr; use chrono::NaiveDateTime; use filters::filter::Filter; +use prettytable::Table; +use prettytable::row::Row; +use prettytable::cell::Cell; use libimagerror::trace::trace_error; use libimagerror::trace::MapErrTrace; @@ -28,6 +31,7 @@ use libimagerror::iter::TraceIterator; use libimagstore::store::FileLockEntry; use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetracking::TimeTracking; +use libimagtimetrack::error::Result; use libimagrt::runtime::Runtime; @@ -93,13 +97,18 @@ pub fn list_impl(rt: &Runtime, let filter = start_time_filter.and(end_time_filter); + let mut table = Table::new(); + table.set_titles(Row::new(["Tag", "Start", "End"].into_iter().map(|s| Cell::new(s)).collect())); + + let mut stdout = ::std::io::stdout(); + rt.store() .get_timetrackings() .and_then(|iter| { iter.trace_unwrap() .filter(|e| filter.filter(e)) - .fold(Ok(()), |acc, e| { - acc.and_then(|_| { + .fold(Ok(table), |acc: Result<_>, e| { + acc.and_then(|mut tab: Table| { debug!("Processing {:?}", e.get_location()); let tag = e.get_timetrack_tag()?; @@ -111,15 +120,35 @@ pub fn list_impl(rt: &Runtime, let end = e.get_end_datetime()?; debug!(" -> end = {:?}", end); - match (start, end) { - (None, _) => println!("{} has no start time.", tag), - (Some(s), None) => println!("{} | {} - ...", tag, s), - (Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), - } + let v = match (start, end) { + (None, _) => vec![String::from(tag.as_str()), String::from(""), String::from("")], + (Some(s), None) => { + vec![ + String::from(tag.as_str()), + format!("{}", s), + String::from(""), + ] + }, + (Some(s), Some(e)) => { + vec![ + String::from(tag.as_str()), + format!("{}", s), + format!("{}", e), + ] + }, + }; - Ok(()) + let cells : Vec = v + .into_iter() + .map(|s| Cell::new(&s)) + .collect(); + tab.add_row(Row::new(cells)); + + Ok(tab) }) - }) + })? + .print(&mut stdout) + .map_err(|_| String::from("Failed printing table").into()) }) .map(|_| 0) .map_err_trace() diff --git a/bin/domain/imag-timetrack/src/main.rs b/bin/domain/imag-timetrack/src/main.rs index 55a8d01f..6d736cb0 100644 --- a/bin/domain/imag-timetrack/src/main.rs +++ b/bin/domain/imag-timetrack/src/main.rs @@ -20,12 +20,11 @@ #[macro_use] extern crate log; -#[macro_use] - extern crate clap; extern crate chrono; extern crate filters; extern crate itertools; +extern crate prettytable; extern crate libimagerror; extern crate libimagstore; diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md index f9e37d0d..bbce7b85 100644 --- a/doc/src/09020-changelog.md +++ b/doc/src/09020-changelog.md @@ -37,6 +37,7 @@ This section contains the changelog from the last release to the next release. chaining * `libimagbookmark` does not longer wrap types from the store. * The `toml-query` dependency was updated to 0.5.0 + * `imag-timetrack list` lists with a table now * Bugfixes ## 0.5.0