use std::convert::TryFrom; use std::io::Write; use clap::ArgMatches; use chrono::{Utc, Duration}; use itertools::Itertools; use crate::error::{Error, Result}; use crate::database::Database; use crate::config::Config; use crate::tabulate::{Tabulate, Col, Align::*}; use crate::formatters::text::format_duration; use super::Command; #[derive(Default)] pub struct Args { all: bool, } impl<'a> TryFrom<&'a ArgMatches<'a>> for Args { type Error = Error; fn try_from(matches: &ArgMatches) -> Result { Ok(Args { all: matches.is_present("all"), }) } } pub struct ListCommand {} impl<'a> Command<'a> for ListCommand { type Args = Args; fn handle(args: Args, db: &mut D, out: &mut O, _err: &mut E, _config: &Config) -> Result<()> where D: Database, O: Write, E: Write, { // no sheet, list sheets let now = Utc::now(); let mut sheets = if args.all { db.entries_all_visible(None, None)? } else { db.entries_full(None, None)? }; let current = db.current_sheet()?.unwrap_or("".into()); sheets.sort_unstable_by_key(|e| e.sheet.clone()); let sheets: Vec<_> = sheets .into_iter() .group_by(|e| e.sheet.clone()) .into_iter() .map(|(key, group)| { ( if current == key { "*".into() } else { "".into() }, key, "running".into(), "today".into(), // total format_duration(group.fold(Duration::seconds(0), |acc, e| { acc + (e.end.unwrap_or(now) - e.start) })), ) }) .collect(); let mut tabs = Tabulate::with_columns(vec![ // indicator of current of prev sheet Col::min_width(1).and_alignment(Right), // sheet name Col::min_width(9).and_alignment(Left), // running time Col::min_width(9).and_alignment(Right), // today Col::min_width(7).and_alignment(Right), // accumulated Col::min_width(12).and_alignment(Right), ]); tabs.feed(vec!["".into(), "Timesheet".into(), "Running".into(), "Today".into(), "Total Time".into()]); for sheet in sheets { tabs.feed(vec![sheet.0, sheet.1, sheet.2, sheet.3, sheet.4]); } out.write_all(tabs.print().as_bytes())?; Ok(()) } } #[cfg(test)] mod tests { use chrono::{Utc, TimeZone}; use pretty_assertions::assert_eq; use crate::database::{SqliteDatabase, Database}; use crate::test_utils::PrettyString; use super::*; #[test] fn list_sheets() { let args = Default::default(); let mut db = SqliteDatabase::from_memory().unwrap(); let mut out = Vec::new(); let mut err = Vec::new(); let config = Default::default(); db.init().unwrap(); db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(0, 0, 0), None, None, "_archived".into()).unwrap(); db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(0, 0, 0), None, None, "sheet1".into()).unwrap(); db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(0, 0, 0), None, None, "sheet2".into()).unwrap(); db.entry_insert(Utc.ymd(2021, 1, 1).and_hms(0, 0, 0), None, None, "sheet3".into()).unwrap(); ListCommand::handle(args, &mut db, &mut out, &mut err, &config).unwrap(); assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString(" Timesheet Running Today Total Time sheet1 0:00:00 0:00:00 10:13:55 * sheet2 0:00:00 0:00:00 0:00:00 sheet3 0:00:00 1:52:45 9:32:03 ")); } #[test] fn list_sheets_shows_last() { assert!(false); } #[test] fn list_sheets_shows_running() { assert!(false); } }