Skip to main content

todo_tree/printer/
mod.rs

1pub mod flat;
2pub mod json;
3pub mod options;
4pub mod summary;
5pub mod tree;
6pub mod utils;
7
8use flat::print_flat;
9use json::print_json;
10pub use options::{OutputFormat, PrintOptions};
11use std::io::{self, Write};
12use summary::print_summary;
13use todo_tree_core::ScanResult;
14use tree::print_tree;
15
16pub struct Printer {
17    options: PrintOptions,
18}
19
20impl Printer {
21    pub fn new(options: PrintOptions) -> Self {
22        if !options.colored {
23            colored::control::set_override(false);
24        }
25        Self { options }
26    }
27
28    pub fn print(&self, result: &ScanResult) -> io::Result<()> {
29        let stdout = io::stdout();
30        let mut handle = stdout.lock();
31        self.print_to(&mut handle, result)
32    }
33
34    pub fn print_to<W: Write>(&self, writer: &mut W, result: &ScanResult) -> io::Result<()> {
35        match self.options.format {
36            OutputFormat::Tree => print_tree(writer, result, &self.options)?,
37            OutputFormat::Flat => print_flat(writer, result, &self.options)?,
38            OutputFormat::Json => print_json(writer, result, &self.options)?,
39        }
40
41        if self.options.show_summary && self.options.format != OutputFormat::Json {
42            writeln!(writer)?;
43            print_summary(writer, result, &self.options)?;
44        }
45
46        Ok(())
47    }
48}