tree-tui 0.1.0

An interactive terminal UI for exploring tokei code statistics.
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! The scrollable tree table: one row per visible node.
//!
//! Columns drop progressively (blanks → comments → code) as the available width
//! shrinks, so the name and total-lines columns always stay readable. The
//! `languages` column flexes too: it lists every language with percentages when
//! wide, collapses tail languages into `Other`, and finally falls back to an
//! `N languages` count — important when the detail panel is open.

use std::cmp::Reverse;

use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Cell, Row, Table};
use tokei::LanguageType;
use unicode_width::UnicodeWidthStr;

use super::theme;
use crate::app::Loaded;
use crate::model::{NodeKind, TreeNode};

/// Smallest worthwhile languages column (also the width of the `languages`
/// header); below this it is dropped entirely.
const LANG_MIN: usize = 9;
/// Upper bound on the languages column so it never dominates a wide terminal.
const LANG_MAX: usize = 64;
/// Floor and ceiling for the name reservation, sized around the longest name.
const NAME_FLOOR: usize = 6;
const NAME_MAX: usize = 44;

/// Which optional columns are shown, chosen from the available width.
struct Columns {
    code: bool,
    comments: bool,
    blanks: bool,
    /// Resolved width of the languages column (`0` hides it entirely).
    lang_width: usize,
}

impl Columns {
    /// Choose columns for inner width `width`. The languages column takes the
    /// width the widest visible legend needs (`desired`), after reserving room
    /// for the longest visible name (`name_needed`) so names aren't truncated.
    fn new(width: usize, name_needed: usize, desired: usize) -> Self {
        let code = width >= 30;
        let comments = width >= 56;
        let blanks = width >= 68;

        let fixed = 8 // total lines (always)
            + usize::from(code) * 8
            + usize::from(comments) * 9
            + usize::from(blanks) * 8;
        // Columns sharing one cell of spacing each: name, lines, the optional
        // numeric columns, and (tentatively) the languages column.
        let spacing = 2 + usize::from(code) + usize::from(comments) + usize::from(blanks);

        let budget = width.saturating_sub(fixed + spacing);
        let name_reserve = name_needed.clamp(NAME_FLOOR, NAME_MAX).min(budget);
        let avail = budget.saturating_sub(name_reserve).min(LANG_MAX);
        // Size the column to the widest legend, but never below the `languages`
        // header (`LANG_MIN`) — single-language trees want a narrow legend that
        // would otherwise be dropped. Hide it only when even the header won't fit.
        let lang_width = if desired == 0 || avail < LANG_MIN {
            0
        } else {
            desired.max(LANG_MIN).min(avail)
        };

        Self {
            code,
            comments,
            blanks,
            lang_width,
        }
    }

    fn widths(&self) -> Vec<Constraint> {
        let mut widths = vec![Constraint::Fill(1)];
        if self.lang_width > 0 {
            widths.push(Constraint::Length(self.lang_width as u16));
        }
        if self.code {
            widths.push(Constraint::Length(8));
        }
        if self.comments {
            widths.push(Constraint::Length(9));
        }
        if self.blanks {
            widths.push(Constraint::Length(8));
        }
        widths.push(Constraint::Length(8)); // total lines (always)
        widths
    }

    fn header(&self) -> Row<'static> {
        let right = |text: &'static str| Cell::from(Line::from(text).alignment(Alignment::Right));
        let mut cells = vec![Cell::from("name")];
        if self.lang_width > 0 {
            cells.push(Cell::from("languages"));
        }
        if self.code {
            cells.push(right("code"));
        }
        if self.comments {
            cells.push(right("comments"));
        }
        if self.blanks {
            cells.push(right("blanks"));
        }
        cells.push(right("lines"));
        Row::new(cells).style(
            Style::default()
                .fg(theme::MUTED)
                .add_modifier(Modifier::BOLD),
        )
    }

    fn row(&self, node: &TreeNode) -> Row<'static> {
        let mut cells = vec![Cell::from(Line::from(name_spans(node)))];
        if self.lang_width > 0 {
            cells.push(Cell::from(Line::from(language_legend(
                node,
                self.lang_width,
            ))));
        }
        if self.code {
            cells.push(num_cell(node.stats.code, theme::CODE));
        }
        if self.comments {
            cells.push(num_cell(node.stats.comments, theme::COMMENTS));
        }
        if self.blanks {
            cells.push(num_cell(node.stats.blanks, theme::BLANKS));
        }
        cells.push(Cell::from(
            Line::from(Span::styled(
                theme::group_thousands(node.stats.lines()),
                Style::default().add_modifier(Modifier::BOLD),
            ))
            .alignment(Alignment::Right),
        ));
        Row::new(cells)
    }
}

pub fn render(frame: &mut Frame, loaded: &mut Loaded, area: Rect) {
    // Visible tree rows = body height minus borders (2) and the header row (1).
    loaded.viewport_rows = area.height.saturating_sub(3) as usize;

    // Inner width available to columns: minus borders (2) and the 2-cell
    // selection gutter.
    let inner = area.width.saturating_sub(4) as usize;
    let mut name_needed = 0;
    let mut desired = 0;
    for &id in &loaded.visible {
        let node = &loaded.tree.nodes[id];
        name_needed = name_needed.max(spans_width(&name_spans(node)));
        desired = desired.max(desired_legend_width(node));
    }
    let columns = Columns::new(inner, name_needed, desired);

    let rows: Vec<Row> = loaded
        .visible
        .iter()
        .map(|&id| columns.row(&loaded.tree.nodes[id]))
        .collect();

    let table = Table::new(rows, columns.widths())
        .header(columns.header())
        .column_spacing(1)
        .row_highlight_style(
            Style::default()
                .bg(theme::SELECTION_BG)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("")
        .block(
            Block::bordered()
                .border_style(Style::default().fg(theme::MUTED))
                .title(" tree "),
        );

    frame.render_stateful_widget(table, area, &mut loaded.table_state);
}

fn name_spans(node: &TreeNode) -> Vec<Span<'static>> {
    let indent = node.depth.saturating_sub(1);
    let mut spans = vec![Span::raw("  ".repeat(indent))];
    match node.kind {
        NodeKind::Dir => {
            let glyph = if node.expanded {
                theme::GLYPH_EXPANDED
            } else {
                theme::GLYPH_COLLAPSED
            };
            spans.push(Span::styled(
                format!("{glyph} "),
                Style::default().fg(theme::DIR),
            ));
            spans.push(Span::styled(
                format!("{}/", node.name),
                Style::default().fg(theme::DIR).add_modifier(Modifier::BOLD),
            ));
        }
        NodeKind::File => {
            let color = node
                .primary_lang
                .map_or(theme::MUTED, theme::language_color);
            spans.push(Span::styled(
                format!("{} ", theme::GLYPH_FILE),
                Style::default().fg(color),
            ));
            spans.push(Span::raw(node.name.clone()));
        }
    }
    spans
}

/// A node's languages, largest first, dropping any with no lines.
fn sorted_langs(node: &TreeNode) -> Vec<(LanguageType, usize)> {
    let mut langs: Vec<(LanguageType, usize)> = node
        .langs
        .iter()
        .map(|(lang, stats)| (*lang, stats.lines()))
        .filter(|(_, lines)| *lines > 0)
        .collect();
    langs.sort_by_key(|(_, lines)| Reverse(*lines));
    langs
}

/// Total display width of a sequence of spans.
fn spans_width(spans: &[Span<'_>]) -> usize {
    spans.iter().map(|s| s.content.width()).sum()
}

/// `Lang (12.3%)` entries (plus an optional `Other` bucket) joined by `, `.
fn legend_spans(shown: &[(LanguageType, usize)], other: usize, total: usize) -> Vec<Span<'static>> {
    let mut spans = Vec::new();
    let entry = |label: String, color: Color, lines: usize, spans: &mut Vec<Span<'static>>| {
        if !spans.is_empty() {
            spans.push(Span::styled(", ", Style::default().fg(theme::MUTED)));
        }
        spans.push(Span::styled(
            format!("{label} "),
            Style::default().fg(color),
        ));
        spans.push(Span::styled(
            format!("({})", theme::percent(lines, total)),
            Style::default().fg(theme::MUTED),
        ));
    };
    for (lang, lines) in shown {
        entry(
            theme::language_label(*lang),
            theme::language_color(*lang),
            *lines,
            &mut spans,
        );
    }
    if other > 0 {
        entry("Other".to_string(), theme::MUTED, other, &mut spans);
    }
    spans
}

/// Width the full (untruncated) legend would occupy for `node`.
fn desired_legend_width(node: &TreeNode) -> usize {
    let langs = sorted_langs(node);
    match langs.as_slice() {
        [] => 0,
        [(lang, _)] => theme::language_label(*lang).width(),
        _ => spans_width(&legend_spans(&langs, 0, node.stats.lines())),
    }
}

/// The languages cell: the widest representation that fits in `width`.
///
/// Falls back from the full percentage list, to leading languages plus an
/// `Other` bucket, to a bare `N languages` count.
fn language_legend(node: &TreeNode, width: usize) -> Vec<Span<'static>> {
    let langs = sorted_langs(node);
    let total = node.stats.lines();
    match langs.as_slice() {
        [] => Vec::new(),
        [(lang, _)] => vec![Span::styled(
            theme::language_label(*lang),
            Style::default().fg(theme::language_color(*lang)),
        )],
        _ => {
            let full = legend_spans(&langs, 0, total);
            if spans_width(&full) <= width {
                return full;
            }
            // Drop tail languages into an `Other` bucket until it fits.
            for keep in (1..langs.len()).rev() {
                let other: usize = langs[keep..].iter().map(|(_, lines)| lines).sum();
                let spans = legend_spans(&langs[..keep], other, total);
                if spans_width(&spans) <= width {
                    return spans;
                }
            }
            // Degrade the count gracefully rather than truncate mid-word.
            let n = langs.len();
            let label = [
                format!("{n} languages"),
                format!("{n} langs"),
                n.to_string(),
            ]
            .into_iter()
            .find(|s| s.width() <= width)
            .unwrap_or_else(|| format!("{n} languages"));
            vec![Span::styled(label, Style::default().fg(theme::MUTED))]
        }
    }
}

fn num_cell(value: usize, color: Color) -> Cell<'static> {
    Cell::from(
        Line::from(Span::styled(
            theme::group_thousands(value),
            Style::default().fg(color),
        ))
        .alignment(Alignment::Right),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::Stats;
    use tokei::LanguageType as L;

    fn node(langs: &[(L, usize)]) -> TreeNode {
        let mut node = TreeNode::dir("src".into(), "src".into(), None, 1);
        let mut total = 0;
        for (lang, lines) in langs {
            node.langs.insert(
                *lang,
                Stats {
                    code: *lines,
                    ..Stats::default()
                },
            );
            total += *lines;
        }
        node.stats = Stats {
            code: total,
            ..Stats::default()
        };
        node
    }

    fn text(node: &TreeNode, width: usize) -> String {
        language_legend(node, width)
            .iter()
            .map(|s| s.content.as_ref())
            .collect()
    }

    // src/: Rust 75.0%, Python 17.4%, TOML 4.4%, Markdown 3.2%.
    fn polyglot() -> TreeNode {
        node(&[
            (L::Rust, 775),
            (L::Python, 180),
            (L::Toml, 45),
            (L::Markdown, 33),
        ])
    }

    #[test]
    fn legend_lists_every_language_when_wide() {
        let s = text(&polyglot(), 200);
        assert_eq!(
            s,
            "Rust (75.0%), Python (17.4%), TOML (4.4%), Markdown (3.2%)"
        );
        assert_eq!(desired_legend_width(&polyglot()), s.width());
    }

    #[test]
    fn legend_collapses_tail_into_other() {
        // Fits "Rust (75.0%), Other (25.0%)" (27) but not the next tier (42).
        assert_eq!(text(&polyglot(), 27), "Rust (75.0%), Other (25.0%)");
    }

    #[test]
    fn legend_falls_back_to_a_count_then_degrades() {
        assert_eq!(text(&polyglot(), 15), "4 languages");
        assert_eq!(text(&polyglot(), 7), "4 langs");
        assert_eq!(text(&polyglot(), 3), "4");
    }

    #[test]
    fn legend_single_language_is_just_the_label() {
        assert_eq!(text(&node(&[(L::Rust, 500)]), 40), "Rust");
    }

    #[test]
    fn legend_empty_node_is_blank() {
        assert!(language_legend(&node(&[]), 40).is_empty());
    }

    #[test]
    fn languages_column_shows_when_legends_are_narrow() {
        // A wide terminal whose widest legend is a short single label still
        // shows the column, floored to the header width. (The earlier bug
        // dropped it because `desired` (8) fell below LANG_MIN.)
        assert_eq!(Columns::new(96, 11, 8).lang_width, LANG_MIN);
    }

    #[test]
    fn languages_column_caps_at_available_space() {
        // A legend wider than the room left for it is truncated to fit.
        assert!(Columns::new(96, 11, 100).lang_width < 100);
        assert!(Columns::new(96, 11, 100).lang_width >= LANG_MIN);
    }

    #[test]
    fn languages_column_hidden_when_absent_or_cramped() {
        assert_eq!(Columns::new(96, 11, 0).lang_width, 0); // no languages
        assert_eq!(Columns::new(40, 30, 50).lang_width, 0); // detail panel squeeze
    }
}