rs-hop 0.4.1

Fuzzy-finder TUI to jump between git repositories and folders
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Renders the repository list as a table, with columns depending on the tab.
//!
//! The git tabs show name, branch, status and GitHub name; the Files and
//! Folders tab shows name, type and path. A leading marker column flags entries
//! whose path no longer exists, followed by the favourite star.

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

use chrono::{DateTime, Local};
use ratada::text::truncate;
use ratatui::Frame;
use ratatui::layout::{Constraint, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Cell, Row, Table, TableState};
use unicode_width::UnicodeWidthStr;

use crate::config::{ColumnWidth, Config};
use crate::domain::filter::{Tab, TabKind};
use crate::domain::repo::{GitInfo, Repo};
use crate::domain::sections::UNGROUPED;
use crate::domain::stats::{CodeEntry, GitStats};
use crate::theme::Skin;
use crate::tui::columns::{ColumnSet, StatColumn, stat_columns};
use crate::tui::git_columns::{
    branch_text, effective_info, github_text, status_display, zip_date_text,
};
use crate::tui::list_layout::{
    ListScroll, SLUG_HEADER, settled_offset, slug_column_width,
};
use crate::tui::presentation::{
    IconSet, highlight_name, slug_style, status_span,
};
use crate::tui::row_cells::{
    GlyphContext, StatContext, fav_span, marker_span, type_label,
};
use crate::tui::skin::Colors;

/// The styling context for a table render, bundled to keep the parameter count
/// low.
pub struct TableView<'a> {
    /// The active tab (decides the columns).
    pub tab: Tab,
    /// The resolved settings (column widths).
    pub config: &'a Config,
    /// The active theme, for the scrollbar.
    pub skin: &'a Skin,
    /// The colour roles resolved from the active theme.
    pub colors: &'a Colors,
    /// The glyph set.
    pub icons: &'a IconSet,
    /// Whether to show example git info instead of live status.
    pub example_mode: bool,
    /// While a refresh runs: the still-in-flight paths and the current spinner
    /// frame glyph. Rows whose path is in the set show the spinner. `None` when
    /// no refresh is running.
    pub spinner: Option<(&'a HashSet<PathBuf>, &'a str)>,
    /// Per visible row: whether it is part of the multi-selection.
    pub selected: &'a [bool],
    /// Whether a multi-selection is active (shows the leading marker column).
    pub has_selection: bool,
    /// Paths flagged missing by the on-demand file/folder existence check.
    pub missing: &'a HashSet<PathBuf>,
    /// Whether to show the slug in its own dim-italic column after the name.
    pub show_slugs: bool,
    /// Whether to show a leading Section column (the flat view, when grouping is
    /// toggled off, so an entry's group is still visible without header bars).
    pub show_section: bool,
    /// The active fuzzy query, to highlight matched characters in the name.
    pub query: Option<&'a str>,
    /// Last ZIP-backup time per repo path, for the "ZIP Backup" column.
    pub zip_backups: &'a HashMap<PathBuf, DateTime<Local>>,
    /// The scroll offset carried across frames, so moving the cursor up while it
    /// is still visible does not scroll the list.
    pub offset: &'a std::cell::Cell<usize>,
    /// Which columns to show.
    pub columns: ColumnSet,
    /// Cached code and size statistics, keyed by entry path.
    pub code: &'a HashMap<PathBuf, CodeEntry>,
    /// Cached history statistics, keyed by entry path.
    pub git: &'a HashMap<PathBuf, GitStats>,
    /// Paths a statistics worker has not reported yet (drive the cell spinner).
    pub computing: &'a HashSet<PathBuf>,
    /// The reference time for ages, in unix seconds.
    pub now: i64,
}

impl TableView<'_> {
    /// What the shared statistics cells read from.
    fn stats(&self) -> StatContext<'_> {
        StatContext {
            code: self.code,
            git: self.git,
            computing: self.computing,
            spinner: self.spinner,
            now: self.now,
        }
    }

    /// What the shared leading glyphs read from.
    fn glyphs(&self) -> GlyphContext<'_> {
        GlyphContext {
            example_mode: self.example_mode,
            missing: self.missing,
            icons: self.icons,
            colors: self.colors,
        }
    }

    /// The rendered text of one statistics cell.
    fn stat_text(&self, repo: &Repo, column: StatColumn) -> String {
        self.stats().text(repo, column)
    }
}

/// The name-cell spans: the name, fuzzy-highlighted when a query is active. The
/// slug is its own column (see `row_for`), not appended here.
fn name_cell_spans(repo: &Repo, view: &TableView) -> Vec<Span<'static>> {
    let name = repo.display_name();
    match view.query {
        Some(query) if !query.trim().is_empty() => {
            highlight_name(&name, query, view.colors)
        }
        _ => vec![Span::raw(name)],
    }
}

/// Renders the table of `repos` (already filtered and sorted) into `area`,
/// highlighting `cursor`.
pub fn render_table(
    frame: &mut Frame,
    area: Rect,
    repos: &[&Repo],
    cursor: usize,
    view: &TableView,
) {
    // Reserve the last column for a scrollbar when the list overflows.
    let viewport = area.height.saturating_sub(1) as usize; // minus header row
    let overflow = viewport > 0 && repos.len() > viewport;
    let table_area = if overflow {
        Rect {
            width: area.width.saturating_sub(1),
            ..area
        }
    } else {
        area
    };

    let rows: Vec<Row> = repos
        .iter()
        .enumerate()
        .map(|(row, repo)| {
            let selected = view.selected.get(row).copied().unwrap_or(false);
            let built = row_for(repo, view, selected);
            if selected {
                built.style(Style::default().bg(view.colors.multi_select_bg))
            } else {
                built
            }
        })
        .collect();
    let table = Table::new(rows, widths(repos, view, table_area.width))
        .header(header_row(view))
        .column_spacing(1)
        .row_highlight_style(view.colors.selection_style());
    // Carry the offset across frames so the cursor moves within the viewport
    // without scrolling until it reaches an edge.
    let offset = settled_offset(&ListScroll {
        saved: view.offset.get(),
        cursor,
        row_count: repos.len(),
        viewport,
        first_entry_row: None,
    });
    view.offset.set(offset);
    let mut state = TableState::default()
        .with_offset(offset)
        .with_selected(Some(cursor));
    frame.render_stateful_widget(table, table_area, &mut state);

    if overflow {
        // Align the bar with the rows (below the header); it follows the same
        // offset the table scrolled to.
        let bar_area = Rect {
            x: area.x,
            y: area.y + 1,
            width: area.width,
            height: area.height.saturating_sub(1),
        };
        ratada::scroll::render_scrollbar(
            frame,
            bar_area,
            view.skin,
            ratada::nav::ScrollView {
                total: repos.len(),
                offset,
                viewport,
            },
        );
    }
}

/// Per-column width constraints sized to the visible content, so no column is
/// wider than it needs to be. Each text column is as wide as its longest cell
/// (and header), floored at the configured minimum and capped at the configured
/// maximum; remaining width is left unused. The Files tab keeps the path column
/// flexible to use the leftover space. On the git tabs the GitHub column yields
/// width to the Name column when `available` is tight (Name has priority).
fn widths(
    repos: &[&Repo],
    view: &TableView,
    available: u16,
) -> Vec<Constraint> {
    let cols = &view.config.column_widths;
    let name = sized(content_width(repos, |r| r.display_name()), 4, cols.name);
    // The lead: a 2-cell selection marker (only while a selection is active),
    // the marker + fav cells, and the leading Section column when grouping is
    // toggled off. The Name column and the optional Slug column follow.
    let mut cells: Vec<Constraint> = if view.has_selection {
        vec![
            Constraint::Length(2),
            Constraint::Length(1),
            Constraint::Length(1),
        ]
    } else {
        vec![Constraint::Length(1), Constraint::Length(1)]
    };
    if view.show_section {
        cells.push(Constraint::Length(section_width(repos)));
    }
    cells.push(Constraint::Length(name));
    if view.show_slugs {
        cells.push(Constraint::Length(slug_width(repos)));
    }
    if view.columns.is_statistics() {
        cells.extend(
            stat_columns(view.columns)
                .iter()
                .map(|column| Constraint::Length(column.width())),
        );
        // Leave the remainder unused rather than stretching a number column.
        cells.push(Constraint::Min(0));
        return cells;
    }
    match view.tab.kind() {
        TabKind::Files => {
            cells.push(Constraint::Length(6));
            cells.push(Constraint::Min(20));
        }
        TabKind::Git => {
            let branch = sized(
                content_width(repos, |r| {
                    branch_text(effective_info(r, view.example_mode))
                }),
                6,
                cols.current_branch_name,
            );
            let status = sized(
                content_width(repos, |r| {
                    status_display(
                        effective_info(r, view.example_mode),
                        view.icons,
                    )
                }),
                6,
                cols.status,
            );
            let github_desired = sized(
                content_width(repos, |r| {
                    github_text(effective_info(r, view.example_mode))
                }),
                6,
                cols.github_repo_name,
            );
            let zip = sized(
                content_width(repos, |r| {
                    zip_date_text(r, view.icons, view.zip_backups)
                }),
                "ZIP Backup".len(),
                cols.zip_backup,
            );
            // GitHub is the column that yields: it keeps its content width when
            // there is room, but shrinks so the rigid columns (above all Name)
            // are never squeezed. The optional Section and Slug columns count as
            // part of the fixed lead.
            let section = if view.show_section {
                section_width(repos)
            } else {
                0
            };
            let slug = if view.show_slugs {
                slug_width(repos)
            } else {
                0
            };
            let lead_cells: u16 = (if view.has_selection { 3 } else { 2 })
                + section_cells(view)
                + slug_cells(view);
            let lead_width: u16 =
                (if view.has_selection { 4 } else { 2 }) + section + slug;
            let spacing = (lead_cells + 5).saturating_sub(1); // column_spacing 1
            let fixed = lead_width + name + branch + status + zip + spacing;
            let github = github_width(github_desired, fixed, available);
            cells.push(Constraint::Length(branch));
            cells.push(Constraint::Length(status));
            cells.push(Constraint::Length(github));
            cells.push(Constraint::Length(zip));
        }
    }
    cells
}

/// The widest rendered cell (display columns) over `repos` for one column.
fn content_width<F>(repos: &[&Repo], cell: F) -> usize
where
    F: Fn(&Repo) -> String,
{
    repos
        .iter()
        .map(|repo| UnicodeWidthStr::width(cell(repo).as_str()))
        .max()
        .unwrap_or(0)
}

/// The column width for `content`: at least the `header` label and the
/// configured minimum, capped at the configured maximum.
fn sized(content: usize, header: usize, width: ColumnWidth) -> u16 {
    let floor = width.min.max(header);
    let mut chosen = content.max(floor);
    if let Some(max) = width.max {
        chosen = chosen.min(max.max(floor));
    }
    chosen as u16
}

/// The GitHub column width: its `desired` content width, but capped at the space
/// left after the rigid columns (`fixed`) so Name and friends are never
/// squeezed. Shrinks toward 0 when tight; never overflows the row.
fn github_width(desired: u16, fixed: u16, available: u16) -> u16 {
    desired.min(available.saturating_sub(fixed))
}

/// The header label and floor width of the leading Section column.
const SECTION_HEADER: &str = "Section";
/// The upper bound of the Section column, so a long name cannot dominate.
const SECTION_MAX: usize = 24;

/// The Section column width, sized to the widest section label (floored at the
/// header, capped at [`SECTION_MAX`]).
fn section_width(repos: &[&Repo]) -> u16 {
    content_width(repos, section_label)
        .max(SECTION_HEADER.len())
        .min(SECTION_MAX) as u16
}

/// Whether the layout adds a Section column cell (0 or 1), for lead-cell counts.
fn section_cells(view: &TableView) -> u16 {
    u16::from(view.show_section)
}

/// The section label for `repo`: its section name, or the Ungrouped label.
fn section_label(repo: &Repo) -> String {
    repo.section
        .clone()
        .unwrap_or_else(|| UNGROUPED.to_string())
}

/// The Slug column width, sized to the widest slug. Only meaningful while
/// `show_slugs` is on.
fn slug_width(repos: &[&Repo]) -> u16 {
    slug_column_width(content_width(repos, slug_text)) as u16
}

/// Whether the layout adds a Slug column cell (0 or 1), for lead-cell counts.
fn slug_cells(view: &TableView) -> u16 {
    u16::from(view.show_slugs)
}

/// The slug text for `repo` (empty when it has none).
fn slug_text(repo: &Repo) -> String {
    repo.slug.clone().unwrap_or_default()
}

/// The header row for `tab` (a leading blank cell for the selection column when
/// a selection is active, and a Section header when the flat section column is
/// shown).
fn header_row(view: &TableView) -> Row<'static> {
    let titles: Vec<String> = if view.columns.is_statistics() {
        let mut titles = vec![String::new(), String::new(), "Name".to_string()];
        titles.extend(
            stat_columns(view.columns)
                .iter()
                .map(|column| column.title().to_string()),
        );
        titles.push(String::new());
        titles
    } else {
        match view.tab.kind() {
            TabKind::Files => vec!["", "", "Name", "Type", "Path"],
            TabKind::Git => {
                vec!["", "", "Name", "Branch", "Status", "GitHub", "ZIP Backup"]
            }
        }
        .into_iter()
        .map(str::to_string)
        .collect()
    };
    let mut cells: Vec<Cell> = Vec::new();
    if view.has_selection {
        cells.push(Cell::from(""));
    }
    let mut titles = titles;
    // Name sits at index 2 (after the marker + fav blanks). The Slug header
    // goes right after it, the Section header right before it; insert Slug first
    // so the Section insert keeps Name at index 2.
    if view.show_slugs {
        titles.insert(3, SLUG_HEADER.to_string());
    }
    if view.show_section {
        titles.insert(2, SECTION_HEADER.to_string());
    }
    cells.extend(titles.into_iter().map(Cell::from));
    Row::new(cells).style(view.colors.header_style())
}

/// Builds the data row for one entry; `selected` drives the marker column.
fn row_for<'a>(repo: &Repo, view: &TableView, selected: bool) -> Row<'a> {
    let mut cells: Vec<Cell> = Vec::new();
    if view.has_selection {
        cells.push(selection_cell(selected, view.colors));
    }
    cells.push(marker_cell(repo, view));
    cells.push(fav_cell(repo, view));
    if view.show_section {
        cells.push(Cell::from(Span::styled(
            truncate(&section_label(repo), SECTION_MAX),
            Style::default().fg(view.colors.dim),
        )));
    }
    cells.push(Cell::from(Line::from(name_cell_spans(repo, view))));
    if view.show_slugs {
        cells.push(Cell::from(Span::styled(
            slug_text(repo),
            slug_style(view.colors),
        )));
    }
    if view.columns.is_statistics() {
        for column in stat_columns(view.columns) {
            let text = view.stat_text(repo, *column);
            let cell = if column.is_numeric() {
                Cell::from(Line::from(text).right_aligned())
            } else {
                Cell::from(text)
            };
            cells.push(cell);
        }
        cells.push(Cell::from(""));
        return Row::new(cells);
    }
    match view.tab.kind() {
        TabKind::Files => {
            cells.push(Cell::from(type_label(repo)));
            cells.push(Cell::from(Span::styled(
                repo.path.to_string_lossy().into_owned(),
                Style::default().fg(view.colors.dim),
            )));
        }
        TabKind::Git => {
            let info = effective_info(repo, view.example_mode);
            // Only ellipsize a branch that exceeds its configured maximum; the
            // column itself is sized to the content otherwise.
            let branch_cap = view
                .config
                .column_widths
                .current_branch_name
                .max
                .unwrap_or(usize::MAX);
            cells.push(Cell::from(truncate(&branch_text(info), branch_cap)));
            cells.push(status_cell(repo, info, view));
            cells.push(Cell::from(github_text(info)));
            cells.push(Cell::from(Span::styled(
                zip_date_text(repo, view.icons, view.zip_backups),
                Style::default().fg(view.colors.dim),
            )));
        }
    }
    Row::new(cells)
}

/// The selection-marker cell: a bold accent `▸` when selected, else blank.
fn selection_cell<'a>(selected: bool, colors: &Colors) -> Cell<'a> {
    let symbol = if selected { "\u{25b8} " } else { "  " };
    Cell::from(Span::styled(
        symbol,
        Style::default()
            .fg(colors.accent)
            .add_modifier(Modifier::BOLD),
    ))
}

/// The marker cell, wrapping the shared error-marker span.
fn marker_cell<'a>(repo: &Repo, view: &TableView) -> Cell<'a> {
    Cell::from(marker_span(repo, &view.glyphs()))
}

/// The favourite cell, wrapping the shared favourite-star span.
fn fav_cell<'a>(repo: &Repo, view: &TableView) -> Cell<'a> {
    Cell::from(fav_span(repo, &view.glyphs()))
}

/// The status cell: a pending marker while this row still awaits a running
/// refresh, dim while first loading, green when clean, yellow when there are
/// uncommitted changes, default otherwise (e.g. only ahead/behind).
fn status_cell<'a>(
    repo: &Repo,
    info: Option<&GitInfo>,
    view: &TableView,
) -> Cell<'a> {
    // A refresh is running and this row is still in flight: animated spinner.
    if let Some((in_flight, glyph)) = view.spinner
        && in_flight.contains(&repo.path)
    {
        return Cell::from(Span::styled(
            glyph.to_string(),
            Style::default().fg(view.colors.accent),
        ));
    }
    // Otherwise the shared coloured status span (loading / clean / changes).
    Cell::from(status_span(info, view.icons, view.colors))
}

#[cfg(test)]
mod tests {
    use super::github_width;

    #[test]
    fn github_keeps_content_width_when_there_is_room() {
        // Plenty of space: GitHub shows its full content width.
        assert_eq!(github_width(20, 60, 200), 20);
    }

    #[test]
    fn github_yields_width_when_tight() {
        // 90 wide, 64 taken by the rigid columns: GitHub shrinks from 39 to 26
        // so Name (inside `fixed`) keeps its width.
        assert_eq!(github_width(39, 64, 90), 26);
    }

    #[test]
    fn github_collapses_rather_than_overflowing() {
        // No room left: GitHub goes to 0 instead of pushing the row over.
        assert_eq!(github_width(39, 95, 90), 0);
    }
}