From 17af9b65cc75ddc15091d1f3a4365c13336cceb7 Mon Sep 17 00:00:00 2001 From: Till Höppner Date: Sat, 23 Jan 2016 23:32:54 +0100 Subject: Add seen command --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index d650158..4bbb9ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,10 +24,12 @@ extern crate blist; use std::process; use std::io::{ self, BufRead, BufReader, Write, BufWriter }; +use std::path::{ Path, PathBuf }; use std::fs::File; use std::error::Error; use std::str::FromStr; use std::collections::HashMap; +use std::ffi::OsStr; use docopt::Docopt; @@ -61,6 +63,7 @@ Usage: ilc parse [options] [-i FILE...] ilc convert [options] [-i FILE...] ilc freq [options] [-i FILE...] + ilc seen [options] [-i FILE...] ilc sort [options] [-i FILE...] ilc dedup [options] [-i FILE...] ilc (-h | --help | -v | --version) @@ -75,6 +78,7 @@ Options: --outf OUTF Set the output format. --in -i IN Give an input file, instead of stdin. --out -o OUT Give an output file, instead of stdout. + --infer-date Try to use the filename as date for the log. "#; #[derive(RustcDecodable, Debug)] @@ -82,9 +86,11 @@ struct Args { cmd_parse: bool, cmd_convert: bool, cmd_freq: bool, + cmd_seen: bool, cmd_sort: bool, cmd_dedup: bool, arg_file: Vec, + arg_nick: String, flag_in: Vec, flag_out: Option, flag_inf: Option, @@ -93,7 +99,8 @@ struct Args { flag_version: bool, flag_date: Option, flag_tz: Option, - flag_channel: Option + flag_channel: Option, + flag_infer_date: bool } fn error(e: Box) -> ! { @@ -143,21 +150,31 @@ fn main() { process::exit(1) } - let context = Context { + let mut context = Context { timezone: FixedOffset::west(args.flag_tz.and_then(|s| s.parse().ok()).unwrap_or(0)), override_date: args.flag_date.and_then(|d| NaiveDate::from_str(&d).ok()), channel: args.flag_channel.clone() }; let mut input: Box = if args.flag_in.len() > 0 { - let input_files = args.flag_in.iter() + let input_files: Vec = args.flag_in.iter() .flat_map(|p| { match glob(p) { Ok(paths) => paths, Err(e) => die(&format!("{}", e.msg)) } - }).filter_map(Result::ok).map(|p| File::open(p).unwrap()).collect(); - Box::new(BufReader::new(chain::Chain::new(input_files))) + }).filter_map(Result::ok).collect();//.map(|p| File::open(p).unwrap()).collect(); + if args.flag_infer_date { + if input_files.len() > 1 { die("Too many input files, can't infer date") } + if let Some(date) = input_files.iter().next() + .map(PathBuf::as_path) + .and_then(Path::file_stem) + .and_then(OsStr::to_str) + .and_then(|s: &str| NaiveDate::from_str(s).ok()) { + context.override_date = Some(date); + } + } + Box::new(BufReader::new(chain::Chain::new(input_files.iter().map(|p| File::open(p).unwrap()).collect()))) } else { Box::new(BufReader::new(io::stdin())) }; @@ -178,7 +195,7 @@ fn main() { let e = e.unwrap(); let _ = encoder.encode(&context, &mut output, &e); } - }else if args.cmd_convert { + } else if args.cmd_convert { let mut decoder = force_decoder(args.flag_inf); let encoder = force_encoder(args.flag_outf); for e in decoder.decode(&context, &mut input) { @@ -253,6 +270,22 @@ fn main() { "{}:\n\tTotal lines: {}\n\tLines without alphabetic characters: {}\n\tTotal words: {}\n\tWords per line: {}\n", name, stat.lines, stat.lines - stat.alpha_lines, stat.words, stat.words as f32 / stat.lines as f32); } + } else if args.cmd_seen { + let mut decoder = force_decoder(args.flag_inf); + let mut last: Option = None; + for e in decoder.decode(&context, &mut input) { + let m = match e { + Ok(m) => m, + Err(err) => error(Box::new(err)) + }; + + if m.ty.involves(&args.arg_nick) + && last.as_ref().map_or(true, |last| m.time.as_timestamp() > last.time.as_timestamp()) { last = Some(m) } + } + let encoder = format::weechat3::Weechat3; + if let Some(ref m) = last { + let _ = encoder.encode(&context, &mut output, m); + } } else if args.cmd_sort { let mut decoder = force_decoder(args.flag_inf); let encoder = force_encoder(args.flag_outf); -- cgit v1.2.3