Change list command to use a table for output

This commit is contained in:
Matthias Beyer 2018-01-13 03:19:41 +01:00
parent 2b0dfec457
commit 45ac9b4132
4 changed files with 41 additions and 11 deletions

View file

@ -25,6 +25,7 @@ log = "0.3"
chrono = "0.4" chrono = "0.4"
filters = "0.2" filters = "0.2"
itertools = "0.7" itertools = "0.7"
prettytable-rs = "0.6"
libimagstore = { version = "0.6.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.6.0", path = "../../../lib/core/libimagstore" }
libimagrt = { version = "0.6.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.6.0", path = "../../../lib/core/libimagrt" }

View file

@ -21,6 +21,9 @@ use std::str::FromStr;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use filters::filter::Filter; use filters::filter::Filter;
use prettytable::Table;
use prettytable::row::Row;
use prettytable::cell::Cell;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
@ -28,6 +31,7 @@ use libimagerror::iter::TraceIterator;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetrackingstore::TimeTrackStore;
use libimagtimetrack::timetracking::TimeTracking; use libimagtimetrack::timetracking::TimeTracking;
use libimagtimetrack::error::Result;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
@ -93,13 +97,18 @@ pub fn list_impl(rt: &Runtime,
let filter = start_time_filter.and(end_time_filter); 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() rt.store()
.get_timetrackings() .get_timetrackings()
.and_then(|iter| { .and_then(|iter| {
iter.trace_unwrap() iter.trace_unwrap()
.filter(|e| filter.filter(e)) .filter(|e| filter.filter(e))
.fold(Ok(()), |acc, e| { .fold(Ok(table), |acc: Result<_>, e| {
acc.and_then(|_| { acc.and_then(|mut tab: Table| {
debug!("Processing {:?}", e.get_location()); debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
@ -111,15 +120,35 @@ pub fn list_impl(rt: &Runtime,
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { let v = match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => vec![String::from(tag.as_str()), String::from(""), String::from("")],
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => {
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), 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<Cell> = 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(|_| 0)
.map_err_trace() .map_err_trace()

View file

@ -20,12 +20,11 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use]
extern crate clap; extern crate clap;
extern crate chrono; extern crate chrono;
extern crate filters; extern crate filters;
extern crate itertools; extern crate itertools;
extern crate prettytable;
extern crate libimagerror; extern crate libimagerror;
extern crate libimagstore; extern crate libimagstore;

View file

@ -37,6 +37,7 @@ This section contains the changelog from the last release to the next release.
chaining chaining
* `libimagbookmark` does not longer wrap types from the store. * `libimagbookmark` does not longer wrap types from the store.
* The `toml-query` dependency was updated to 0.5.0 * The `toml-query` dependency was updated to 0.5.0
* `imag-timetrack list` lists with a table now
* Bugfixes * Bugfixes
## 0.5.0 ## 0.5.0