diff options
author | Till Höppner | 2016-02-23 17:00:53 +0100 |
---|---|---|
committer | Till Höppner | 2016-02-24 18:21:24 +0100 |
commit | e866dceae987acd51d43bd457351bd2188c5f95a (patch) | |
tree | 2cc5a67492bdf00ff68041656a1e870b2b4de17f /src/app | |
parent | 815f31f5cef61709c50087c9f7601ea330929bb7 (diff) | |
download | ilc-e866dceae987acd51d43bd457351bd2188c5f95a.tar.gz ilc-e866dceae987acd51d43bd457351bd2188c5f95a.tar.xz ilc-e866dceae987acd51d43bd457351bd2188c5f95a.zip |
Test CI
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/freq.rs | 51 | ||||
-rw-r--r-- | src/app/mod.rs | 138 |
2 files changed, 122 insertions, 67 deletions
diff --git a/src/app/freq.rs b/src/app/freq.rs index 6d80f2e..88a8e1f 100644 --- a/src/app/freq.rs +++ b/src/app/freq.rs @@ -2,14 +2,14 @@ use clap::ArgMatches; use std::collections::HashMap; -use ilc::event::{ Event, Type }; +use ilc::event::{Event, Type}; use super::*; struct Person { lines: u32, alpha_lines: u32, - words: u32 + words: u32, } fn words_alpha(s: &str) -> (u32, bool) { @@ -18,30 +18,37 @@ fn words_alpha(s: &str) -> (u32, bool) { for w in s.split_whitespace() { if !w.is_empty() { words += 1; - if w.chars().any(char::is_alphabetic) { alpha = true } + if w.chars().any(char::is_alphabetic) { + alpha = true + } } } (words, alpha) } fn strip_nick_prefix(s: &str) -> &str { - if s.is_empty() { return s } + if s.is_empty() { + return s; + } match s.as_bytes()[0] { b'~' | b'&' | b'@' | b'%' | b'+' => &s[1..], - _ => s + _ => s, } } -pub fn freq(args: &ArgMatches) { +pub fn freq(args: &ArgMatches) { let env = Environment(args); - let (context, mut decoder, mut input, mut output) = (env.context(), env.decoder(), env.input(), env.output()); + let (context, mut decoder, mut input, mut output) = (env.context(), + env.decoder(), + env.input(), + env.output()); let mut stats: HashMap<String, Person> = HashMap::new(); for e in decoder.decode(&context, &mut input) { let m = match e { Ok(m) => m, - Err(err) => error(Box::new(err)) + Err(err) => error(Box::new(err)), }; match m { @@ -51,18 +58,21 @@ pub fn freq(args: &ArgMatches) { let p: &mut Person = stats.get_mut(nick).unwrap(); let (words, alpha) = words_alpha(content); p.lines += 1; - if alpha { p.alpha_lines += 1 } + if alpha { + p.alpha_lines += 1 + } p.words += words; } else { let (words, alpha) = words_alpha(content); - stats.insert(nick.to_owned(), Person { - lines: 1, - alpha_lines: if alpha { 1 } else { 0 }, - words: words - }); + stats.insert(nick.to_owned(), + Person { + lines: 1, + alpha_lines: if alpha { 1 } else { 0 }, + words: words, + }); } - }, - _ => () + } + _ => (), } } @@ -72,7 +82,12 @@ pub fn freq(args: &ArgMatches) { let count = value_t!(args, "count", usize).unwrap_or(stats.len()); for &(ref name, ref stat) in stats.iter().take(count) { let _ = write!(&mut output, - "{}:\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); + "{}:\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); } } diff --git a/src/app/mod.rs b/src/app/mod.rs index 3221ec9..9b02b1e 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -7,16 +7,16 @@ use glob::glob; use std::process; use std::str::FromStr; -use std::path::{ Path, PathBuf }; -use std::io::{ self, Write, BufWriter, BufRead, BufReader }; +use std::path::{Path, PathBuf}; +use std::io::{self, BufRead, BufReader, BufWriter, Write}; use std::fs::File; use std::error::Error; use std::ffi::OsStr; use ilc::context::Context; -use ilc::format::{ self, Encode, Decode }; +use ilc::format::{self, Decode, Encode}; -use ::chain; +use chain; pub mod freq; @@ -38,43 +38,47 @@ pub fn die(s: &str) -> ! { pub fn force_decoder(s: Option<&str>) -> Box<Decode> { let inf = match s { Some(s) => s, - None => die("You didn't specify the input format") + None => die("You didn't specify the input format"), }; match format::decoder(&inf) { Some(d) => d, - None => die(&format!("The format `{}` is unknown to me", inf)) + None => die(&format!("The format `{}` is unknown to me", inf)), } } pub fn force_encoder<'a>(s: Option<&str>) -> Box<Encode> { let outf = match s { Some(s) => s, - None => die("You didn't specify the output format") + None => die("You didn't specify the output format"), }; match format::encoder(&outf) { Some(e) => e, - None => die(&format!("The format `{}` is unknown to me", outf)) + None => die(&format!("The format `{}` is unknown to me", outf)), } } pub fn build_context(args: &ArgMatches) -> Context { let mut context = Context { - timezone: FixedOffset::west(args.value_of("timezone").and_then(|s| s.parse().ok()).unwrap_or(0)), + timezone: FixedOffset::west(args.value_of("timezone") + .and_then(|s| s.parse().ok()) + .unwrap_or(0)), override_date: args.value_of("date").and_then(|d| NaiveDate::from_str(&d).ok()), - channel: args.value_of("channel").map(str::to_owned).clone() + channel: args.value_of("channel").map(str::to_owned).clone(), }; if args.is_present("infer_date") { let input_files = gather_input(args); match input_files.len() { 0 => die("No input files given, can't infer date"), - 1 => if let Some(date) = input_files.get(0) - .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); - }, - _n => die("Too many input files, can't infer date") + 1 => { + if let Some(date) = input_files.get(0) + .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); + } + } + _n => die("Too many input files, can't infer date"), } } context @@ -83,17 +87,23 @@ pub fn build_context(args: &ArgMatches) -> Context { pub fn gather_input(args: &ArgMatches) -> Vec<PathBuf> { if let Some(iter) = args.values_of("input_files") { iter.flat_map(|p| { - match glob(p) { - Ok(paths) => paths, - Err(e) => die(&format!("{}", e.msg)) - } - }).filter_map(Result::ok).collect() - } else { Vec::new() } + match glob(p) { + Ok(paths) => paths, + Err(e) => die(&format!("{}", e.msg)), + } + }) + .filter_map(Result::ok) + .collect() + } else { + Vec::new() + } } pub fn open_files(files: Vec<PathBuf>) -> Box<BufRead> { if files.len() > 0 { - Box::new(BufReader::new(chain::Chain::new(files.iter().map(|p| File::open(p).unwrap()).collect()))) + Box::new(BufReader::new(chain::Chain::new(files.iter() + .map(|p| File::open(p).unwrap()) + .collect()))) } else { Box::new(BufReader::new(io::stdin())) } @@ -103,7 +113,7 @@ pub fn open_output(args: &ArgMatches) -> Box<Write> { if let Some(out) = args.value_of("output_file") { match File::create(out) { Ok(f) => Box::new(BufWriter::new(f)), - Err(e) => error(Box::new(e)) + Err(e) => error(Box::new(e)), } } else { Box::new(BufWriter::new(io::stdout())) @@ -113,11 +123,21 @@ pub fn open_output(args: &ArgMatches) -> Box<Write> { pub struct Environment<'a>(pub &'a ArgMatches<'a>); impl<'a> Environment<'a> { - pub fn context(&self) -> Context { build_context(self.0) } - pub fn input(&self) -> Box<BufRead> { open_files(gather_input(self.0)) } - pub fn output(&self) -> Box<Write> { open_output(self.0) } - pub fn decoder(&self) -> Box<Decode> { force_decoder(self.0.value_of("input_format")) } - pub fn encoder(&self) -> Box<Encode> { force_encoder(self.0.value_of("output_format")) } + pub fn context(&self) -> Context { + build_context(self.0) + } + pub fn input(&self) -> Box<BufRead> { + open_files(gather_input(self.0)) + } + pub fn output(&self) -> Box<Write> { + open_output(self.0) + } + pub fn decoder(&self) -> Box<Decode> { + force_decoder(self.0.value_of("format").or(self.0.value_of("input_format"))) + } + pub fn encoder(&self) -> Box<Encode> { + force_encoder(self.0.value_of("format").or(self.0.value_of("output_format"))) + } } pub mod parse { @@ -128,8 +148,11 @@ pub mod parse { let (context, mut decoder, mut input) = (env.context(), env.decoder(), env.input()); for e in decoder.decode(&context, &mut input) { match e { - Err(e) => { println!("Foo!"); error(Box::new(e)) }, - _ => () + Err(e) => { + println!("Foo!"); + error(Box::new(e)) + } + _ => (), } } } @@ -140,13 +163,18 @@ pub mod convert { use super::*; pub fn convert(args: &ArgMatches) { let env = Environment(args); - let (context, mut decoder, mut input, encoder, mut output) = - (env.context(), env.decoder(), env.input(), env.encoder(), env.output()); + let (context, mut decoder, mut input, encoder, mut output) = (env.context(), + env.decoder(), + env.input(), + env.encoder(), + env.output()); for e in decoder.decode(&context, &mut input) { match e { - Ok(e) => { let _ = encoder.encode(&context, &mut output, &e); }, - Err(e) => error(Box::new(e)) + Ok(e) => { + let _ = encoder.encode(&context, &mut output, &e); + } + Err(e) => error(Box::new(e)), } } } @@ -155,11 +183,14 @@ pub mod convert { pub mod seen { use clap::ArgMatches; use ilc::event::Event; - use ilc::format::{ self, Encode }; + use ilc::format::{self, Encode}; use super::*; pub fn seen(args: &ArgMatches) { let env = Environment(args); - let (context, mut decoder, mut input, mut output) = (env.context(), env.decoder(), env.input(), env.output()); + let (context, mut decoder, mut input, mut output) = (env.context(), + env.decoder(), + env.input(), + env.output()); let nick = args.value_of("nick").expect("Required argument <nick> not present"); @@ -167,11 +198,14 @@ pub mod seen { for e in decoder.decode(&context, &mut input) { let m = match e { Ok(m) => m, - Err(err) => error(Box::new(err)) + Err(err) => error(Box::new(err)), }; - if m.ty.involves(nick) - && last.as_ref().map_or(true, |last| m.time.as_timestamp() > last.time.as_timestamp()) { last = Some(m) } + if m.ty.involves(nick) && + last.as_ref().map_or(true, + |last| m.time.as_timestamp() > last.time.as_timestamp()) { + last = Some(m) + } } let encoder = format::Weechat; if let Some(ref m) = last { @@ -186,12 +220,15 @@ pub mod sort { use super::*; pub fn sort(args: &ArgMatches) { let env = Environment(args); - let (context, mut decoder, mut input, encoder, mut output) = - (env.context(), env.decoder(), env.input(), env.encoder(), env.output()); + let (context, mut decoder, mut input, encoder, mut output) = (env.context(), + env.decoder(), + env.input(), + env.encoder(), + env.output()); let mut events: Vec<Event> = decoder.decode(&context, &mut input) - .flat_map(Result::ok) - .collect(); + .flat_map(Result::ok) + .collect(); events.sort_by(|a, b| a.time.cmp(&b.time)); for e in events { @@ -203,12 +240,15 @@ pub mod sort { pub mod dedup { use clap::ArgMatches; use ilc::event::NoTimeHash; - use ::ageset::AgeSet; + use ageset::AgeSet; use super::*; pub fn dedup(args: &ArgMatches) { let env = Environment(args); - let (context, mut decoder, mut input, encoder, mut output) = - (env.context(), env.decoder(), env.input(), env.encoder(), env.output()); + let (context, mut decoder, mut input, encoder, mut output) = (env.context(), + env.decoder(), + env.input(), + env.encoder(), + env.output()); let mut backlog = AgeSet::new(); |