implement month command
This commit is contained in:
parent
4154b59310
commit
f5f6a44a4e
|
@ -13,6 +13,7 @@ pub mod today;
|
|||
pub mod yesterday;
|
||||
pub mod sheet;
|
||||
pub mod week;
|
||||
pub mod month;
|
||||
|
||||
pub trait Command<'a> {
|
||||
type Args: TryFrom<&'a ArgMatches<'a>>;
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
use std::convert::TryFrom;
|
||||
use std::io::Write;
|
||||
|
||||
use clap::ArgMatches;
|
||||
use chrono::{DateTime, Utc, Local, 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 beginning_of_month(now: DateTime<Local>) -> DateTime<Utc> {
|
||||
now.date().with_day(1).unwrap().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 MonthCommand { }
|
||||
|
||||
impl<'a> Command<'a> for MonthCommand {
|
||||
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 = beginning_of_month(Local::now());
|
||||
|
||||
entries_for_display(Some(start), args.end, args.sheet, db, out, err, args.format, args.ids, args.grep)
|
||||
}
|
||||
}
|
12
src/main.rs
12
src/main.rs
|
@ -10,6 +10,7 @@ use tiempo::config::Config;
|
|||
use tiempo::commands::{
|
||||
Command, r#in::InCommand, display::DisplayCommand, sheet::SheetCommand,
|
||||
today::TodayCommand, yesterday::YesterdayCommand, week::WeekCommand,
|
||||
month::MonthCommand,
|
||||
};
|
||||
|
||||
fn error_trap(matches: ArgMatches) -> error::Result<()> {
|
||||
|
@ -26,6 +27,7 @@ fn error_trap(matches: ArgMatches) -> error::Result<()> {
|
|||
("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),
|
||||
("month", Some(matches)) => MonthCommand::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),
|
||||
|
||||
|
@ -177,6 +179,16 @@ fn main() {
|
|||
.arg(sheet_arg.clone())
|
||||
)
|
||||
|
||||
.subcommand(SubCommand::with_name("month")
|
||||
.visible_alias("m")
|
||||
.about("Display entries starting this month")
|
||||
.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")
|
||||
|
|
Loading…
Reference in New Issue