aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorTill Hoeppner2015-07-26 15:53:29 +0200
committerTill Hoeppner2015-07-26 15:53:29 +0200
commit1288ebd52be8fc8b739e95d88690ad2b57d5f88c (patch)
treedecfd30bc2b354f9fb13fff517025900d2375790 /src/main.rs
parentaa79405b22a06b1b97be17daa0a7d0b0456a693c (diff)
downloadilc-1288ebd52be8fc8b739e95d88690ad2b57d5f88c.tar.gz
ilc-1288ebd52be8fc8b739e95d88690ad2b57d5f88c.tar.xz
ilc-1288ebd52be8fc8b739e95d88690ad2b57d5f88c.zip
Merge src/freq.rs into the main executable.
Modularisation of functionality is postponed because the best way to seperate this is unclear.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 1f22995..8a8f289 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -30,6 +30,7 @@ use std::io::{ self, BufReader };
use std::fs::File;
use std::error::Error;
use std::str::FromStr;
+use std::collections::HashMap;
use docopt::Docopt;
@@ -38,6 +39,7 @@ use chrono::naive::date::NaiveDate;
use ilc::context::Context;
use ilc::format::{ self, Encode, Decode, DecodeBox };
+use ilc::event::{ Event, Type };
static USAGE: &'static str = r#"
d8b 888
@@ -54,6 +56,7 @@ A converter and statistics utility for IRC log files.
Usage:
ilc parse <file>...
ilc convert <informat> <outformat> [--date DATE] [--tz SECS] [--channel CH]
+ ilc freq
ilc (-h | --help | -v | --version)
Options:
@@ -68,6 +71,7 @@ Options:
struct Args {
cmd_parse: bool,
cmd_convert: bool,
+ cmd_freq: bool,
arg_file: Vec<String>,
arg_informat: Option<String>,
arg_outformat: Option<String>,
@@ -135,4 +139,64 @@ fn main() {
}
}
}
+
+ if args.cmd_freq {
+ struct Person {
+ lines: u32,
+ words: u32
+ }
+
+ fn words(s: &str) -> u32 {
+ s.split_whitespace().filter(|s| !s.is_empty()).count() as u32
+ }
+
+ fn strip_nick_prefix(s: &str) -> &str {
+ if s.is_empty() { return s }
+ match s.as_bytes()[0] {
+ b'~' | b'&' | b'@' | b'%' | b'+' => &s[1..],
+ _ => s
+ }
+ }
+
+ let stdin = io::stdin();
+
+ let mut stats: HashMap<String, Person> = HashMap::new();
+ let context = Context {
+ timezone: FixedOffset::west(0),
+ override_date: Some(NaiveDate::from_ymd(2015, 6, 10)),
+ channel: Some("#code".to_owned())
+ };
+
+ let mut parser = format::weechat3::Weechat3;
+ for e in parser.decode(&context, stdin.lock()) {
+ let m = match e {
+ Ok(m) => m,
+ Err(err) => panic!(err)
+ };
+
+ match m {
+ Event { ty: Type::Msg { ref from, ref content, .. }, .. } => {
+ let nick = strip_nick_prefix(from);
+ if stats.contains_key(nick) {
+ let p: &mut Person = stats.get_mut(nick).unwrap();
+ p.lines += 1;
+ p.words += words(content);
+ } else {
+ stats.insert(nick.to_owned(), Person {
+ lines: 1,
+ words: words(content)
+ });
+ }
+ },
+ _ => ()
+ }
+ }
+
+ let mut stats: Vec<(String, Person)> = stats.into_iter().collect();
+ stats.sort_by(|&(_, ref a), &(_, ref b)| b.words.cmp(&a.words));
+
+ for &(ref name, ref stat) in stats.iter().take(10) {
+ println!("{}:\n\tLines: {}\n\tWords: {}", name, stat.lines, stat.words)
+ }
+ }
}