diff --git a/src/lib.rs b/src/lib.rs index 856b5af..d4bd02c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use std::io; -use std::io::Write; -use std::fs::File; -use std::fs::OpenOptions; +use std::io::{Write,BufReader,BufRead}; +use std::fs::{File,OpenOptions}; +use std::str::FromStr; mod conf; mod project; @@ -14,9 +14,25 @@ pub fn start(mut bartib_file: File, project_name: &str, task_description: &str) write!(bartib_file, "{}", task).expect("Could not write new task to file"); } +pub fn list_running(bartib_file: File) { + + let reader = BufReader::new(bartib_file); + + reader.lines() + .filter_map(|line_result| line_result.ok()) + .map(|line| task::Task::from_str(&line)) + .filter_map(|task_result| task_result.ok()) + .filter(|task| !task.is_stopped()) + .for_each(|task| print!("{}", task)); +} + pub fn get_bartib_file_writable(file_name: &str) -> Result { OpenOptions::new() .create(true) .append(true) .open(file_name) } + +pub fn get_bartib_file_readable(file_name: &str) -> Result { + File::open(file_name) +} diff --git a/src/main.rs b/src/main.rs index 2febbff..cd35d41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,19 +36,28 @@ fn main() { .takes_value(true) ) ) + .subcommand( + SubCommand::with_name("current") + .about("lists all currently running tasks") + ) .get_matches(); let file_name = matches.value_of("file").unwrap(); - let bartib_file = bartib::get_bartib_file_writable(file_name).unwrap(); match matches.subcommand() { ("start", Some(sub_m)) => { + let bartib_file = bartib::get_bartib_file_writable(file_name).unwrap(); + let project_name = sub_m.value_of("project").unwrap(); let task_description = sub_m.value_of("description").unwrap(); bartib::start(bartib_file, project_name, task_description); - } + }, + ("current", Some(_)) => { + let bartib_file = bartib::get_bartib_file_readable(file_name).unwrap(); + bartib::list_running(bartib_file) + }, _ => println!("Unknown command") } } diff --git a/src/task.rs b/src/task.rs index 8c9eec3..8d855a4 100644 --- a/src/task.rs +++ b/src/task.rs @@ -36,6 +36,10 @@ impl Task { pub fn stop(&mut self) { self.end = Some(Local::now().naive_local()); } + + pub fn is_stopped(&self) -> bool { + self.end.is_some() + } } impl fmt::Display for Task { @@ -60,9 +64,7 @@ impl FromStr for Task { fn from_str(s: &str) -> Result { let parts : Vec = split_with_escaped_delimeter(s).collect(); - - println!("{:?}", parts); - + if parts.len() < 2 { return Err(TaskError::GeneralParseError); }