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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crate::{eval, git, model};

// Color is an alias for yansi::Paint.
pub(crate) type Color<T> = yansi::Paint<T>;

pub(crate) fn display_missing_tree(tree: &model::Tree, path: &str, verbose: u8) -> String {
    if verbose > 0 {
        format!(
            "{} {} {} {}",
            Color::black("#").bold(),
            Color::black(tree.get_name()).bold(),
            Color::black(path).bold(),
            Color::black("(skipped)").bold()
        )
    } else {
        format!(
            "{} {} {}",
            Color::black("#").bold(),
            Color::black(tree.get_name()).bold(),
            Color::black("(skipped)").bold()
        )
    }
}

pub(crate) fn display_tree(
    tree: &model::Tree,
    path_str: &str,
    tree_branches: bool,
    verbose: u8,
) -> String {
    if verbose > 0 {
        if tree_branches {
            if let Some(path) = tree.canonical_pathbuf() {
                if let Some(branch) = git::branch(&path) {
                    return format!(
                        "{} {} {}{}{} {}",
                        Color::cyan("#"),
                        Color::blue(tree.get_name()).bold(),
                        Color::blue("["),
                        Color::green(&branch).bold(),
                        Color::blue("]"),
                        Color::blue(&path_str)
                    );
                }
            }
        }
        format!(
            "{} {} {}",
            Color::cyan("#"),
            Color::blue(tree.get_name()).bold(),
            Color::blue(path_str)
        )
    } else {
        if tree_branches {
            if let Some(path) = tree.canonical_pathbuf() {
                if let Some(branch) = git::branch(&path) {
                    return format!(
                        "{} {} {}{}{}",
                        Color::cyan("#"),
                        Color::blue(tree.get_name()).bold(),
                        Color::blue("["),
                        Color::green(&branch).bold(),
                        Color::blue("]")
                    );
                }
            }
        }
        format!(
            "{} {}",
            Color::cyan("#"),
            Color::blue(tree.get_name()).bold()
        )
    }
}

/// Print a tree if it exists, otherwise print a missing tree
pub(crate) fn print_tree(
    tree: &model::Tree,
    tree_branches: bool,
    verbose: u8,
    quiet: bool,
) -> bool {
    if let Ok(path) = tree.path_as_ref() {
        // Sparse gardens/missing trees are expected. Skip these entries.
        if !std::path::PathBuf::from(&path).exists() {
            if !quiet {
                eprintln!("{}", display_missing_tree(tree, path, verbose));
            }
            return false;
        }

        print_tree_details(tree, tree_branches, verbose, quiet);
        return true;
    } else if !quiet {
        eprintln!("{}", display_missing_tree(tree, "[invalid-path]", verbose));
    }

    false
}

/// Print a tree.
pub(crate) fn print_tree_details(
    tree: &model::Tree,
    tree_branches: bool,
    verbose: u8,
    quiet: bool,
) {
    if !quiet {
        if let Ok(path) = tree.path_as_ref() {
            eprintln!("{}", display_tree(tree, path, tree_branches, verbose));
        }
    }
}

/// Print an ungrown / missing tree.
pub(crate) fn print_missing_tree(tree: &model::Tree, path: &str, verbose: u8) {
    if verbose > 0 {
        println!(
            "{} {} {}",
            Color::red("#-").dimmed(),
            Color::red(tree.get_name()),
            Color::red(path).dimmed()
        );
    } else {
        println!(
            "{} {}",
            Color::red("#-").dimmed(),
            Color::red(tree.get_name())
        );
    }
}

/// Print a symlink tree entry.
pub(crate) fn print_symlink_tree_entry(tree: &model::Tree, path: &str, verbose: u8) {
    let symlink = match tree.symlink_as_ref() {
        Ok(symlink) => symlink,
        Err(_) => return,
    };
    if verbose > 0 {
        println!(
            "{} {} {} {} {}",
            Color::cyan("#+"),
            Color::blue(tree.get_name()).bold(),
            Color::green(path),
            Color::green("->"),
            Color::yellow(symlink)
        );
    } else {
        println!(
            "{} {} {} {}",
            Color::cyan("#"),
            Color::blue(tree.get_name()).bold(),
            Color::green("->"),
            Color::yellow(symlink)
        );
    }
}

/// Print the description, url, remotes and links for a tree
pub(crate) fn print_tree_extended_details(
    app_context: &model::ApplicationContext,
    context: &model::TreeContext,
    tree: &model::Tree,
    display_worktrees: bool,
) {
    let config = match context.config {
        Some(config_id) => app_context.get_config(config_id),
        None => app_context.get_root_config(),
    };
    if !tree.description.is_empty() {
        println!("{}", Color::green(&tree.description));
    }
    if tree.is_worktree && !display_worktrees {
        return;
    }
    if !tree.remotes.is_empty() {
        println!("{}", Color::blue("remotes:"));
        for (name, remote) in &tree.remotes {
            let value = eval::tree_value(
                app_context,
                config,
                remote.get_expr(),
                &context.tree,
                context.garden.as_ref(),
            );
            println!(
                "  {}{} {}",
                Color::blue(name),
                Color::blue(":"),
                Color::yellow(value)
            );
        }
    }
    if !tree.links.is_empty() {
        println!("{}", Color::blue("links:"));
        for link in &tree.links {
            let value = eval::tree_value(
                app_context,
                config,
                link.get_expr(),
                &context.tree,
                context.garden.as_ref(),
            );
            println!("  {} {}", Color::blue("-"), Color::yellow(value));
        }
    }
}

/// Print a list of commands
pub(crate) fn print_commands(commands: &model::MultiVariableHashMap) {
    println!("{}", Color::blue("commands:"));
    for cmd in commands.keys() {
        println!("  {} {}", Color::blue("-"), Color::yellow(cmd));
    }
}

/// Print groups
pub(crate) fn print_groups(groups: &model::GroupMap) {
    println!("{}", Color::blue("groups:"));
    for group in groups.keys() {
        println!("  {} {}", Color::blue("-"), Color::yellow(group));
    }
}

/// Print gardens
pub(crate) fn print_gardens(gardens: &model::GardenMap) {
    println!("{}", Color::blue("gardens:"));
    for garden in gardens.keys() {
        println!("  {} {}", Color::blue("-"), Color::yellow(garden));
    }
}