use a generic Write instance for command output

This commit is contained in:
Dustin J. Mitchell
2020-12-20 19:45:24 -05:00
parent 6b550e7516
commit 7d17740ca8
13 changed files with 154 additions and 46 deletions

View File

@@ -4,8 +4,13 @@ use crate::table;
use failure::Fallible;
use prettytable::{cell, row, Table};
use taskchampion::Replica;
use termcolor::WriteColor;
pub(crate) fn execute(replica: &mut Replica, report: Report) -> Fallible<()> {
pub(crate) fn execute<W: WriteColor>(
w: &mut W,
replica: &mut Replica,
report: Report,
) -> Fallible<()> {
let mut t = Table::new();
t.set_format(table::format());
t.set_titles(row![b->"id", b->"act", b->"description"]);
@@ -21,7 +26,7 @@ pub(crate) fn execute(replica: &mut Replica, report: Report) -> Fallible<()> {
};
t.add_row(row![id, active, task.get_description()]);
}
t.printstd();
t.print(w)?;
Ok(())
}
@@ -29,17 +34,23 @@ pub(crate) fn execute(replica: &mut Replica, report: Report) -> Fallible<()> {
mod test {
use super::*;
use crate::argparse::Filter;
use crate::invocation::cmd::test::test_replica;
use crate::invocation::cmd::test::*;
use taskchampion::Status;
#[test]
fn test_list() {
let mut w = test_writer();
let mut replica = test_replica();
replica
.new_task(Status::Pending, "my task".to_owned())
.unwrap();
let report = Report {
filter: Filter {
..Default::default()
},
};
execute(&mut replica, report).unwrap();
// output is to stdout, so this is as much as we can check
execute(&mut w, &mut replica, report).unwrap();
assert!(w.into_string().contains("my task"));
}
}