add subcommand 'current'

This commit is contained in:
Nikolas Schmidt-Voigt 2021-04-16 22:05:18 +02:00
parent 7f366ee0d8
commit 108bf27ba2
3 changed files with 35 additions and 8 deletions

View file

@ -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<File, io::Error> {
OpenOptions::new()
.create(true)
.append(true)
.open(file_name)
}
pub fn get_bartib_file_readable(file_name: &str) -> Result<File, io::Error> {
File::open(file_name)
}

View file

@ -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")
}
}

View file

@ -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<Self, Self::Err> {
let parts : Vec<String> = split_with_escaped_delimeter(s).collect();
println!("{:?}", parts);
if parts.len() < 2 {
return Err(TaskError::GeneralParseError);
}