tiempo-rs/src/commands/display.rs

229 lines
6.5 KiB
Rust
Raw Normal View History

2021-06-18 11:27:19 -05:00
use std::convert::TryFrom;
use std::io::Write;
use std::str::FromStr;
2021-06-18 11:27:19 -05:00
use clap::ArgMatches;
use chrono::{DateTime, Utc, Local, TimeZone, LocalResult};
2021-06-28 23:40:41 -05:00
use terminal_size::{Width, terminal_size};
use ansi_term::Color::Yellow;
2021-06-18 11:27:19 -05:00
2021-06-21 17:38:51 -05:00
use crate::error;
use crate::database::{Database, DBVersion};
2021-06-21 17:38:51 -05:00
use crate::formatters::Formatter;
use crate::config::Config;
use crate::models::Entry;
2021-07-01 23:42:59 -05:00
use crate::timeparse::parse_time;
2021-06-18 11:27:19 -05:00
use super::Command;
fn local_to_utc(t: DateTime<Utc>) -> error::Result<DateTime<Utc>> {
let local_time = match Local.from_local_datetime(&t.naive_utc()) {
2021-07-01 23:42:59 -05:00
LocalResult::None => return Err(error::Error::NoneLocalTime(t.naive_utc().to_string())),
LocalResult::Single(t) => t,
LocalResult::Ambiguous(t1, t2) => return Err(error::Error::AmbiguousLocalTime {
2021-07-01 23:42:59 -05:00
orig: t.naive_utc().to_string(),
2021-07-02 10:07:07 -05:00
t1: t1.naive_local(),
t2: t2.naive_local(),
}),
};
Ok(Utc.from_utc_datetime(&local_time.naive_utc()))
}
fn local_to_utc_vec(entries: Vec<Entry>) -> error::Result<Vec<Entry>> {
entries
.into_iter()
.map(|e| {
Ok(Entry {
start: local_to_utc(e.start)?,
end: e.end.map(|t| local_to_utc(t)).transpose()?,
..e
})
})
.collect()
}
2021-06-28 23:40:41 -05:00
fn term_width() -> usize {
if let Some((Width(w), _)) = terminal_size() {
w as usize
} else {
80
}
}
enum Sheet {
All,
Full,
Sheet(String),
}
impl FromStr for Sheet {
type Err = error::Error;
fn from_str(name: &str) -> error::Result<Sheet> {
Ok(match name {
"all" => Sheet::All,
"full" => Sheet::Full,
name => Sheet::Sheet(name.into()),
})
}
}
2021-06-29 12:04:54 -05:00
#[derive(Default)]
2021-06-18 11:27:19 -05:00
pub struct Args {
2021-06-21 17:38:51 -05:00
ids: bool,
start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>,
2021-06-21 17:38:51 -05:00
format: Formatter,
grep: Option<String>,
sheet: Option<Sheet>,
2021-06-18 11:27:19 -05:00
}
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
type Error = error::Error;
fn try_from(matches: &'a ArgMatches) -> error::Result<Args> {
2021-06-21 17:38:51 -05:00
Ok(Args {
ids: matches.is_present("ids"),
2021-07-01 23:42:59 -05:00
start: matches.value_of("start").map(|s| parse_time(s)).transpose()?,
end: matches.value_of("end").map(|s| parse_time(s)).transpose()?,
2021-06-21 17:38:51 -05:00
format: matches.value_of("format").unwrap().parse()?,
grep: matches.value_of("grep").map(|s| s.into()),
sheet: matches.value_of("sheet").map(|s| s.parse()).transpose()?,
2021-06-21 17:38:51 -05:00
})
2021-06-18 11:27:19 -05:00
}
}
pub struct DisplayCommand { }
2021-06-18 11:27:19 -05:00
impl<'a> Command<'a> for DisplayCommand {
type Args = Args;
2021-06-30 19:20:10 -05:00
fn handle<D, O, E>(args: Self::Args, db: &mut D, out: &mut O, err: &mut E, _config: &Config) -> error::Result<()>
2021-06-18 11:27:19 -05:00
where
D: Database,
2021-06-29 12:02:33 -05:00
O: Write,
E: Write,
2021-06-18 11:27:19 -05:00
{
let entries = match args.sheet {
2021-06-30 19:20:10 -05:00
Some(Sheet::All) => db.entries_all_visible(args.start, args.end)?,
Some(Sheet::Full) => db.entries_full(args.start, args.end)?,
Some(Sheet::Sheet(name)) => db.entries_by_sheet(&name, args.start, args.end)?,
None => {
let current_sheet = db.current_sheet()?.unwrap_or("default".into());
2021-06-30 19:20:10 -05:00
db.entries_by_sheet(&current_sheet, args.start, args.end)?
}
};
2021-06-21 17:38:51 -05:00
2021-06-30 19:20:10 -05:00
let (entries, needs_warning) = if let DBVersion::Timetrap = db.version()? {
// this indicates that times in the database are specified in the
// local time and need to be converted to utc before displaying
2021-06-30 19:20:10 -05:00
(local_to_utc_vec(entries)?, true)
} else {
2021-06-30 19:20:10 -05:00
(entries, false)
};
2021-06-28 23:40:41 -05:00
args.format.print_formatted(
entries,
2021-06-28 23:40:41 -05:00
out,
Utc::now(),
args.ids,
term_width(),
2021-06-30 19:20:10 -05:00
)?;
if needs_warning {
writeln!(
err,
"{} You are using the old timetrap format, it is advised that \
you update your database using t migrate",
Yellow.bold().paint("[WARNING]"),
)?;
}
Ok(())
2021-06-18 11:27:19 -05:00
}
}
2021-06-29 12:04:54 -05:00
#[cfg(test)]
mod tests {
use super::*;
use crate::database::SqliteDatabase;
use crate::test_utils::PrettyString;
#[test]
fn display_as_local_time_if_previous_version() {
let args = Default::default();
let mut db = SqliteDatabase::from_path("assets/test_old_db.db").unwrap();
let mut out = Vec::new();
let mut err = Vec::new();
let config = Default::default();
2021-06-30 19:20:10 -05:00
std::env::set_var("TZ", "UTC");
2021-06-29 12:04:54 -05:00
DisplayCommand::handle(args, &mut db, &mut out, &mut err, &config).unwrap();
assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString("Timesheet: default
Day Start End Duration Notes
Tue Jun 29, 2021 06:26:49 - 07:26:52 1:00:03 lets do some rust
1:00:03
-------------------------------------------------------------------
2021-06-29 12:04:54 -05:00
Total 1:00:03
"));
assert_eq!(
String::from_utf8_lossy(&err),
format!("{} You are using the old timetrap format, it is advised that you update your database using t migrate\n", Yellow.bold().paint("[WARNING]")),
2021-06-29 12:04:54 -05:00
);
}
#[test]
2021-06-30 13:31:38 -05:00
#[ignore]
2021-06-29 12:04:54 -05:00
fn correctly_use_utc_if_new_version() {
assert!(false, "start with a newly created database");
assert!(false, "correctly display times in local timezone");
}
2021-06-30 19:20:10 -05:00
#[test]
fn filter_by_start() {
let args = Args {
format: Formatter::Csv,
start: Some(Utc.ymd(2021, 6, 30).and_hms(10, 5, 0)),
..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, 6, 30).and_hms(10, 0, 0), None, Some("hola".into()), "default".into()).unwrap();
db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 10, 0), None, Some("hola".into()), "default".into()).unwrap();
DisplayCommand::handle(args, &mut db, &mut out, &mut err, &config).unwrap();
assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString("start,end,note,sheet
2021-06-30T10:10:00Z,,hola,default
"));
assert_eq!(
String::from_utf8_lossy(&err),
String::new(),
);
}
#[test]
fn filter_by_end() {
}
#[test]
fn filter_by_start_and_end() {
}
#[test]
fn filter_by_match() {
}
2021-06-29 12:04:54 -05:00
}