Reimplement list()

This commit is contained in:
Matthias Beyer 2016-07-21 15:54:38 +02:00
parent 0da8df3c26
commit 98a8356150

View file

@ -17,6 +17,8 @@ use std::process::exit;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::io::stdin; use std::io::stdin;
use toml::Value;
use task_hookrs::import::import_tasks; use task_hookrs::import::import_tasks;
use task_hookrs::status::TaskStatus; use task_hookrs::status::TaskStatus;
@ -100,62 +102,53 @@ fn tw_hook(rt: &Runtime) {
fn list(rt: &Runtime) { fn list(rt: &Runtime) {
let subcmd = rt.cli().subcommand_matches("list").unwrap(); let subcmd = rt.cli().subcommand_matches("list").unwrap();
let mut args = Vec::new();
let verbose = subcmd.is_present("verbose"); let verbose = subcmd.is_present("verbose");
let iter = match Task::all(rt.store()) {
Ok(iter) => iter, let res = Task::all(rt.store())
Err(e) => { .map(|iter| {
trace_error(&e); let uuids : Vec<_> = iter.filter_map(|t| match t {
exit(1); Ok(v) => match v.get_header().read("todo.uuid") {
Ok(Some(Value::String(ref u))) => Some(u.clone()),
Ok(Some(_)) => {
warn!("Header type error");
None
}, },
}; Ok(None) => None,
for task in iter {
match task {
Ok(val) => {
let uuid = match val.get_header().read("todo.uuid") {
Ok(Some(u)) => u,
Ok(None) => continue,
Err(e) => { Err(e) => {
trace_error(&e); trace_error(&e);
continue; None
} }
}; },
Err(e) => {
trace_error(&e);
None
}
})
.collect();
if verbose { let outstring = if verbose {
args.clear(); let output = Command::new("task")
args.push(format!("uuid:{} information", uuid));
let tw_process = Command::new("task")
.stdin(Stdio::null()) .stdin(Stdio::null())
.args(&args) .args(&uuids)
.spawn() .spawn()
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
trace_error(&e); trace_error(&e);
panic!("failed"); panic!("Failed to execute `task` on the commandline. I'm dying now.");
}); })
let output = tw_process
.wait_with_output() .wait_with_output()
.unwrap_or_else(|e| panic!("failed to unwrap output: {}", e)); .unwrap_or_else(|e| panic!("failed to unwrap output: {}", e));
let outstring = String::from_utf8(output.stdout)
.unwrap_or_else(|e| panic!("failed to execute: {}", e)); String::from_utf8(output.stdout)
.unwrap_or_else(|e| panic!("failed to execute: {}", e))
} else {
uuids.join("\n")
};
println!("{}", outstring); println!("{}", outstring);
} else {
println!("{}", match uuid {
toml::Value::String(s) => s,
_ => {
error!("Unexpected type for todo.uuid: {}", uuid);
continue;
},
}); });
}
} if let Err(e) = res {
Err(e) => {
trace_error(&e); trace_error(&e);
continue;
} }
} // end match task
} // end for
} }