tiempo-rs/src/commands/today.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

2021-07-06 22:52:20 -05:00
use std::convert::TryFrom;
use std::io::Write;
use clap::ArgMatches;
use chrono::{DateTime, Utc, Local};
2021-07-07 10:49:29 -05:00
use regex::Regex;
2021-07-06 22:52:20 -05:00
2021-07-07 10:49:29 -05:00
use crate::error::{Result, Error};
2021-07-06 22:52:20 -05:00
use crate::database::Database;
use crate::formatters::Formatter;
use crate::config::Config;
use crate::timeparse::parse_time;
2021-07-07 10:49:29 -05:00
use crate::regex::parse_regex;
2021-07-06 22:52:20 -05:00
use super::{Command, display::{Sheet, entries_for_display}};
2021-07-06 22:52:20 -05:00
#[derive(Default)]
pub struct Args {
ids: bool,
end: Option<DateTime<Utc>>,
format: Formatter,
2021-07-07 10:49:29 -05:00
grep: Option<Regex>,
2021-07-06 22:52:20 -05:00
sheet: Option<Sheet>,
}
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
2021-07-07 10:49:29 -05:00
type Error = Error;
2021-07-06 22:52:20 -05:00
2021-07-07 10:49:29 -05:00
fn try_from(matches: &'a ArgMatches) -> Result<Args> {
2021-07-06 22:52:20 -05:00
Ok(Args {
ids: matches.is_present("ids"),
end: matches.value_of("end").map(|s| parse_time(s)).transpose()?,
format: matches.value_of("format").unwrap().parse()?,
2021-07-07 10:49:29 -05:00
grep: matches.value_of("grep").map(parse_regex).transpose()?,
2021-07-06 22:52:20 -05:00
sheet: matches.value_of("sheet").map(|s| s.parse()).transpose()?,
})
}
}
pub struct TodayCommand { }
impl<'a> Command<'a> for TodayCommand {
type Args = Args;
fn handle<D, O, E>(args: Self::Args, db: &mut D, out: &mut O, err: &mut E, _config: &Config, now: DateTime<Utc>) -> Result<()>
2021-07-06 22:52:20 -05:00
where
D: Database,
O: Write,
E: Write,
{
let start = Some(now.with_timezone(&Local).date().and_hms(0, 0, 0).with_timezone(&Utc));
2021-07-06 22:52:20 -05:00
entries_for_display(start, args.end, args.sheet, db, out, err, args.format, args.ids, args.grep, now)
2021-07-06 22:52:20 -05:00
}
}