1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use chrono::{FixedOffset, Utc};
use termcolor::WriteColor;

use crate::{
    cli::DisplayMode,
    core::{SearchResult, Todo, TodoState},
};

fn print_single(output_target: &mut dyn WriteColor, todo: &Todo, fixed_offset: &FixedOffset) {
    let mut output_target = output_target;
    match todo.state {
        TodoState::Valid | TodoState::Overdue => {
            if matches!(todo.state, TodoState::Overdue) {
                bunt::writeln!(
                    output_target,
                    "{$bold+red}TODO@ {$underline} {}:{}{/$} [overdue]{/$}",
                    &todo.file.as_path().display().to_string(),
                    &todo.line_number
                )
                .expect("Could not write to output.");
            } else {
                bunt::writeln!(
                    output_target,
                    "{$bold}TODO@{/$} {$underline} {}:{}{/$}",
                    &todo.file.as_path().display().to_string(),
                    &todo.line_number
                )
                .expect("Could not write to output.");
            }

            let date_str = &todo
                .date
                .expect("Date should be set if TODO is valid/overdue")
                .format("%Y-%m-%d")
                .to_string();

            let day_difference = -Utc::now()
                .with_timezone(fixed_offset)
                .date()
                .naive_local()
                .signed_duration_since(
                    todo.date
                        .expect("Date should be set if TODO is valid/overdue"),
                )
                .num_days();

            bunt::writeln!(
                output_target,
                "  {$magenta+dimmed}Due:        {/$} {} ({} days)",
                date_str,
                day_difference
            )
            .expect("Could not write to output.");
            bunt::writeln!(
                output_target,
                "  {$blue+dimmed}Description:{/$} {}",
                &todo.description
            )
            .expect("Could not write to output.");
        }
        TodoState::Malformed => {
            bunt::writeln!(
                output_target,
                "{$bold+yellow}TODO@ {$underline} {}:{}{/$} is malformed!{/$}",
                &todo.file.as_path().display().to_string(),
                &todo.line_number
            )
            .expect("Could not write to output.");
            bunt::writeln!(
                output_target,
                "  {$yellow+dimmed}Description:{/$} {}",
                &todo.description
            )
            .expect("Could not write to output.");
        }
    }

    bunt::writeln!(output_target).expect("Could not write to output.");
}

pub fn print(
    output_target: &mut dyn WriteColor,
    mode: DisplayMode,
    results: &SearchResult,
    fixed_offset: &FixedOffset,
) {
    let mut output_target = output_target;
    // Individual TODO details
    if !matches!(mode, DisplayMode::Concise) {
        results.todos.iter().for_each(|todo| {
            if !matches!(mode, DisplayMode::OverdueOnly) || matches!(todo.state, TodoState::Overdue)
            {
                print_single(&mut output_target, todo, fixed_offset)
            }
        })
    }

    // Total stats
    bunt::writeln!(
        output_target,
        "{$green+intense}Searched {} file(s):{/$}",
        results.statistics.files_searched
    )
    .expect("Could not write to output.");
    bunt::write!(
        output_target,
        "{$bold}{} todo(s) found{/$}",
        results.statistics.valid_todo_count + results.statistics.overdue_todo_count
    )
    .expect("Could not write to output.");

    if results.statistics.overdue_todo_count > 0 {
        bunt::writeln!(
            output_target,
            "{$bold+red} of which {} is/are overdue{/$}",
            results.statistics.overdue_todo_count,
        )
        .expect("Could not write to output.");
    } else {
        bunt::writeln!(output_target).expect("Could not write to output.");
    }
}