implement week command

This commit is contained in:
Abraham Toriz 2021-07-07 13:44:19 -05:00
parent 274152932f
commit 4154b59310
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
3 changed files with 83 additions and 1 deletions

View File

@ -12,6 +12,7 @@ pub mod display;
pub mod today;
pub mod yesterday;
pub mod sheet;
pub mod week;
pub trait Command<'a> {
type Args: TryFrom<&'a ArgMatches<'a>>;

70
src/commands/week.rs Normal file
View File

@ -0,0 +1,70 @@
use std::convert::TryFrom;
use std::io::Write;
use clap::ArgMatches;
use chrono::{DateTime, Utc, Local, Duration, Weekday, Datelike};
use regex::Regex;
use crate::error::{Result, Error};
use crate::database::Database;
use crate::formatters::Formatter;
use crate::config::Config;
use crate::regex::parse_regex;
use crate::timeparse::parse_time;
use super::{Command, display::{Sheet, entries_for_display}};
/// Given a local datetime, returns the time of the previous monday's start
fn prev_monday(now: DateTime<Local>) -> DateTime<Utc> {
let on_monday = match now.weekday() {
Weekday::Mon => now,
Weekday::Tue => now - Duration::days(1),
Weekday::Wed => now - Duration::days(2),
Weekday::Thu => now - Duration::days(3),
Weekday::Fri => now - Duration::days(4),
Weekday::Sat => now - Duration::days(5),
Weekday::Sun => now - Duration::days(6),
};
on_monday.date().and_hms(0, 0, 0).with_timezone(&Utc)
}
#[derive(Default)]
pub struct Args {
ids: bool,
end: Option<DateTime<Utc>>,
format: Formatter,
grep: Option<Regex>,
sheet: Option<Sheet>,
}
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
type Error = Error;
fn try_from(matches: &'a ArgMatches) -> Result<Args> {
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()?,
grep: matches.value_of("grep").map(parse_regex).transpose()?,
sheet: matches.value_of("sheet").map(|s| s.parse()).transpose()?,
})
}
}
pub struct WeekCommand { }
impl<'a> Command<'a> for WeekCommand {
type Args = Args;
fn handle<D, O, E>(args: Self::Args, db: &mut D, out: &mut O, err: &mut E, _config: &Config) -> Result<()>
where
D: Database,
O: Write,
E: Write,
{
let start = prev_monday(Local::now());
entries_for_display(Some(start), args.end, args.sheet, db, out, err, args.format, args.ids, args.grep)
}
}

View File

@ -9,7 +9,7 @@ use tiempo::database::SqliteDatabase;
use tiempo::config::Config;
use tiempo::commands::{
Command, r#in::InCommand, display::DisplayCommand, sheet::SheetCommand,
today::TodayCommand, yesterday::YesterdayCommand,
today::TodayCommand, yesterday::YesterdayCommand, week::WeekCommand,
};
fn error_trap(matches: ArgMatches) -> error::Result<()> {
@ -25,6 +25,7 @@ fn error_trap(matches: ArgMatches) -> error::Result<()> {
("display", Some(matches)) => DisplayCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config),
("today", Some(matches)) => TodayCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config),
("yesterday", Some(matches)) => YesterdayCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config),
("week", Some(matches)) => WeekCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config),
("sheet", Some(matches)) => SheetCommand::handle(matches.try_into()?, &mut conn, &mut out, &mut err, &config),
@ -166,6 +167,16 @@ fn main() {
.arg(sheet_arg.clone())
)
.subcommand(SubCommand::with_name("week")
.visible_alias("w")
.about("Display entries starting last monday or later")
.arg(ids_arg.clone())
.arg(end_arg.clone())
.arg(format_arg.clone())
.arg(grep_arg.clone())
.arg(sheet_arg.clone())
)
.subcommand(SubCommand::with_name("in")
.visible_alias("i")
.about("Start an activity in the current timesheet")