Skip to main content

todo_tree/printer/
flat.rs

1use super::options::PrintOptions;
2use super::utils::{colorize_tag, format_path, make_clickable_link};
3use colored::Colorize;
4use std::io::{self, Write};
5use std::path::Path;
6use todo_tree_core::{ScanResult, TodoItem};
7
8pub fn print_flat<W: Write>(
9    writer: &mut W,
10    result: &ScanResult,
11    options: &PrintOptions,
12) -> io::Result<()> {
13    if result.is_empty() {
14        writeln!(writer, "{}", "No TODO items found.".dimmed())?;
15        return Ok(());
16    }
17
18    let mut all_items = result.all_items();
19    all_items.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.line.cmp(&b.1.line)));
20
21    for (path, item) in all_items {
22        print_flat_item(writer, &path, &item, options)?;
23    }
24
25    Ok(())
26}
27
28fn print_flat_item<W: Write>(
29    writer: &mut W,
30    path: &Path,
31    item: &TodoItem,
32    options: &PrintOptions,
33) -> io::Result<()> {
34    let display_path = format_path(path, options);
35    let link = make_clickable_link(path, item.line, options);
36
37    let path_str = link.unwrap_or_else(|| {
38        if options.colored {
39            display_path.bold().to_string()
40        } else {
41            display_path.to_string()
42        }
43    });
44
45    let line_col = format!(":{}:{}", item.line, item.column);
46    let line_col_display = if options.colored {
47        line_col.cyan().to_string()
48    } else {
49        line_col
50    };
51
52    let tag = colorize_tag(&item.tag, options);
53
54    writeln!(
55        writer,
56        "{}{} [{}] {}",
57        path_str, line_col_display, tag, item.message
58    )?;
59    Ok(())
60}