aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/filters.rs14
-rw-r--r--src/main.rs39
2 files changed, 51 insertions, 2 deletions
diff --git a/src/filters.rs b/src/filters.rs
index 1549c26..c6df6e8 100644
--- a/src/filters.rs
+++ b/src/filters.rs
@@ -31,3 +31,17 @@ pub fn filter_outcome(result: &LogResult, opt: &Opt) -> bool {
_ => false,
}
}
+
+/// Do filtering based on encounter time.
+pub fn filter_time(result: &LogResult, opt: &Opt) -> bool {
+ let after_ok = match opt.after {
+ Some(time) => time <= result.time,
+ None => true,
+ };
+ let before_ok = match opt.before {
+ Some(time) => time >= result.time,
+ None => true,
+ };
+
+ after_ok && before_ok
+}
diff --git a/src/main.rs b/src/main.rs
index d026776..c05a8b6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@ extern crate quick_error;
extern crate chrono;
extern crate colored;
extern crate evtclib;
+extern crate humantime;
extern crate num_traits;
extern crate rayon;
extern crate regex;
@@ -14,7 +15,7 @@ use std::io::{self, BufReader};
use std::path::PathBuf;
use std::str::FromStr;
-use chrono::NaiveDateTime;
+use chrono::{Duration, NaiveDateTime};
use num_traits::cast::FromPrimitive;
use regex::Regex;
use structopt::StructOpt;
@@ -85,6 +86,22 @@ pub struct Opt {
#[structopt(long = "no-color")]
no_color: bool,
+ /// Only show logs that are younger than the given time.
+ #[structopt(
+ short = "a",
+ long = "younger",
+ parse(try_from_str = "parse_time_arg")
+ )]
+ after: Option<NaiveDateTime>,
+
+ /// Only show logs that are older than the given time.
+ #[structopt(
+ short = "b",
+ long = "older",
+ parse(try_from_str = "parse_time_arg")
+ )]
+ before: Option<NaiveDateTime>,
+
/// Print more debugging information to stderr.
#[structopt(long = "debug")]
debug: bool,
@@ -179,6 +196,22 @@ impl FromStr for FightOutcome {
}
}
+fn parse_time_arg(input: &str) -> Result<NaiveDateTime, &'static str> {
+ if let Ok(duration) = humantime::parse_duration(input) {
+ let now = chrono::Local::now().naive_local();
+ let chrono_dur = Duration::from_std(duration).expect("Duration out of range!");
+ return Ok(now - chrono_dur);
+ }
+ if let Ok(time) = humantime::parse_rfc3339_weak(input) {
+ let timestamp = time
+ .duration_since(std::time::SystemTime::UNIX_EPOCH)
+ .unwrap()
+ .as_secs();
+ return Ok(NaiveDateTime::from_timestamp(timestamp as i64, 0));
+ }
+ Err("unknown time format")
+}
+
fn main() {
let opt = Opt::from_args();
@@ -254,7 +287,9 @@ fn search_log(entry: &DirEntry, opt: &Opt) -> Result<Option<LogResult>, RuntimeE
let info = extract_info(entry, &log);
- let take_log = filters::filter_name(&log, opt) && filters::filter_outcome(&info, opt);
+ let take_log = filters::filter_name(&log, opt)
+ && filters::filter_outcome(&info, opt)
+ && filters::filter_time(&info, opt);
if take_log {
Ok(Some(info))