From 98a8356150f1525a133b31d5085f6245c5fd3cf7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 21 Jul 2016 15:54:38 +0200 Subject: [PATCH] Reimplement list() --- imag-todo/src/main.rs | 101 ++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 54 deletions(-) diff --git a/imag-todo/src/main.rs b/imag-todo/src/main.rs index b0d5ee0e..ecf127eb 100644 --- a/imag-todo/src/main.rs +++ b/imag-todo/src/main.rs @@ -17,6 +17,8 @@ use std::process::exit; use std::process::{Command, Stdio}; use std::io::stdin; +use toml::Value; + use task_hookrs::import::import_tasks; use task_hookrs::status::TaskStatus; @@ -99,63 +101,54 @@ fn tw_hook(rt: &Runtime) { } fn list(rt: &Runtime) { - let subcmd = rt.cli().subcommand_matches("list").unwrap(); - let mut args = Vec::new(); - let verbose = subcmd.is_present("verbose"); - let iter = match Task::all(rt.store()) { - Ok(iter) => iter, - Err(e) => { - trace_error(&e); - exit(1); - }, - }; + let subcmd = rt.cli().subcommand_matches("list").unwrap(); + let verbose = subcmd.is_present("verbose"); - 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) => { + let res = Task::all(rt.store()) + .map(|iter| { + let uuids : Vec<_> = iter.filter_map(|t| match t { + 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, + Err(e) => { trace_error(&e); - continue; + None } - }; - - if verbose { - args.clear(); - args.push(format!("uuid:{} information", uuid)); - - let tw_process = Command::new("task") - .stdin(Stdio::null()) - .args(&args) - .spawn() - .unwrap_or_else(|e| { - trace_error(&e); - panic!("failed"); - }); - let output = tw_process - .wait_with_output() - .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)); - - println!("{}", outstring); - } else { - println!("{}", match uuid { - toml::Value::String(s) => s, - _ => { - error!("Unexpected type for todo.uuid: {}", uuid); - continue; - }, - }); + }, + Err(e) => { + trace_error(&e); + None } - } - Err(e) => { - trace_error(&e); - continue; - } - } // end match task - } // end for + }) + .collect(); + + let outstring = if verbose { + let output = Command::new("task") + .stdin(Stdio::null()) + .args(&uuids) + .spawn() + .unwrap_or_else(|e| { + trace_error(&e); + panic!("Failed to execute `task` on the commandline. I'm dying now."); + }) + .wait_with_output() + .unwrap_or_else(|e| panic!("failed to unwrap output: {}", e)); + + String::from_utf8(output.stdout) + .unwrap_or_else(|e| panic!("failed to execute: {}", e)) + } else { + uuids.join("\n") + }; + + println!("{}", outstring); + }); + + if let Err(e) = res { + trace_error(&e); + } }