Implement --range
This commit is contained in:
parent
ff627b98df
commit
e9ec2998f0
4 changed files with 91 additions and 33 deletions
46
src/cli.rs
46
src/cli.rs
|
|
@ -1,11 +1,13 @@
|
|||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::{process, result};
|
||||
|
||||
use chrono::{Duration, NaiveDate, NaiveDateTime};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
use directories::ProjectDirs;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::eval::{DateRange, Entry, EntryMode};
|
||||
use crate::eval::{DateRange, Entry, EntryMode, SourceInfo};
|
||||
use crate::files::arguments::Range;
|
||||
use crate::files::{self, Files};
|
||||
|
||||
use self::error::Result;
|
||||
|
|
@ -25,14 +27,9 @@ pub struct Opt {
|
|||
/// Overwrite the current date
|
||||
#[structopt(short, long)]
|
||||
date: Option<NaiveDate>,
|
||||
// TODO Allow negative numbers for `before` and `after`
|
||||
// TODO Or just allow any Delta
|
||||
/// How many days to include before the current date
|
||||
#[structopt(short, long, default_value = "3")]
|
||||
before: u32,
|
||||
/// How many days to include after the current date
|
||||
#[structopt(short, long, default_value = "13")]
|
||||
after: u32,
|
||||
/// The range days to focus on
|
||||
#[structopt(short, long, default_value = "today-2d--today+13d")]
|
||||
range: String,
|
||||
#[structopt(subcommand)]
|
||||
command: Option<Command>,
|
||||
}
|
||||
|
|
@ -77,15 +74,6 @@ fn find_now(opt: &Opt, files: &Files) -> NaiveDateTime {
|
|||
}
|
||||
}
|
||||
|
||||
fn find_range(opt: &Opt, now: NaiveDateTime) -> DateRange {
|
||||
let range_date = opt.date.unwrap_or_else(|| now.date());
|
||||
DateRange::new(
|
||||
range_date - Duration::days(opt.before.into()),
|
||||
range_date + Duration::days(opt.after.into()),
|
||||
)
|
||||
.expect("determine range")
|
||||
}
|
||||
|
||||
fn find_entries(files: &Files, range: DateRange) -> Result<Vec<Entry>> {
|
||||
Ok(files.eval(EntryMode::Relevant, range)?)
|
||||
}
|
||||
|
|
@ -136,7 +124,25 @@ pub fn run() {
|
|||
};
|
||||
|
||||
let now = find_now(&opt, &files);
|
||||
let range = find_range(&opt, now);
|
||||
|
||||
// Kinda ugly, but it can stay for now (until it grows at least).
|
||||
let range = match Range::from_str(&opt.range) {
|
||||
Ok(range) => match range.eval(0, now.date()) {
|
||||
Ok(range) => range,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to evaluate --range:");
|
||||
e.print(&[SourceInfo {
|
||||
name: Some("--range".to_string()),
|
||||
content: &opt.range,
|
||||
}]);
|
||||
process::exit(1)
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Failed to parse --range:\n{}", e.with_path("--range"));
|
||||
process::exit(1)
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = run_command(&opt, &mut files, range, now) {
|
||||
e.print(&files.sources());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue