brain: Make commit history responsive

64369681976f8e2381a4b35e828288e79820b279
Use the shared table layout policy to hide branch, author, time, and
diffstat columns in order as terminal width contracts.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent e2a68436
crates/brain/src/session.rs +21 -33
11 11
    FileDiff, FileTreeMode, HighlightOutput, HighlightRequest, Highlighter, ReviewProgress,
12 12
    ViewChange, spawn_highlighter,
13 13
};
14 14
use rustik_repo::DiffStat;
15 15
use rustik_tui::{
16 -
    Backend as TuiBackend, Constraint, Control, Event, Key, Line, List, ListState, Mouse,
16 +
    Backend as TuiBackend, Column, Constraint, Control, Event, Key, Line, List, ListState, Mouse,
17 17
    MouseButton, Rect, ScrollView, Style, Table, TableOptions, Terminal,
18 18
};
19 19
use rustik_tui_termion as tui_termion;
20 20
21 21
use crate::backend::{
34 34
use event::{ContextFetch, Resize, TerminalEvent, Update, context_change};
35 35
use hunk::{CommentEdit, HunkView};
36 36
pub(crate) use status::BRAIN_BADGE_BG;
37 37
use status::{
38 38
    ActionContext, Chrome, HISTORY_DIFFSTAT_MAX_WIDTH, SELECTED_COMMIT_BG, Status, StatusContext,
39 -
    commit_row, commit_row_suffix, commit_row_suffix_with_author, commit_table_lines,
40 -
    current_timestamp, viewer_area,
39 +
    commit_row, commit_row_suffix, current_timestamp, viewer_area,
41 40
};
42 41
43 42
/// Terminal backend used by the interactive viewer.
44 43
type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>;
45 44
1394 1393
                self.terminal.draw(|frame| {
1395 1394
                    let (body, chrome_area) = Chrome::areas(frame.area);
1396 1395
                    let cols = ScrollView::body_cols(body);
1397 1396
                    let diffstat_width = (cols / HISTORY_DIFFSTAT_WIDTH_DIVISOR)
1398 1397
                        .clamp(HISTORY_DIFFSTAT_MIN_WIDTH, HISTORY_DIFFSTAT_MAX_WIDTH);
1399 -
                    let mut table = Table::<3, Line>::new(TableOptions {
1398 +
                    let mut table = Table::<8, Line>::new(TableOptions {
1400 1399
                        spacing: 2,
1401 1400
                        ..TableOptions::default()
1402 -
                    });
1403 -
                    let mut author_suffix = Table::<5, Line>::new(TableOptions {
1404 -
                        spacing: 2,
1405 -
                        ..TableOptions::default()
1406 -
                    });
1401 +
                    })
1402 +
                    .with_layout([
1403 +
                        Column::auto(),
1404 +
                        Column::auto(),
1405 +
                        Column::flex(1),
1406 +
                        Column::collapse(2),
1407 +
                        Column::collapse(3),
1408 +
                        Column::collapse(1),
1409 +
                        Column::auto(),
1410 +
                        Column::collapse(4),
1411 +
                    ]);
1407 1412
                    for (index, slot) in self.commits.iter().enumerate() {
1408 1413
                        let graph = match (index, last) {
1409 1414
                            (0, 0) => "──◉︎",
1410 1415
                            (0, _) => "╭─◉︎",
1411 1416
                            (index, last) if index == last => "╰─◉︎",
1412 1417
                            _ => "├─◉︎",
1413 1418
                        };
1414 -
                        table.push(commit_row(slot, graph));
1415 -
                        author_suffix.push(commit_row_suffix_with_author(
1419 +
                        let [graph, id, summary] = commit_row(slot, graph);
1420 +
                        let [author, age, branches, progress, diffstat] = commit_row_suffix(
1416 1421
                            slot,
1417 1422
                            palette,
1418 1423
                            now,
1419 1424
                            largest_change_count,
1420 1425
                            diffstat_width,
1421 -
                        ));
1426 +
                        );
1427 +
                        table.push([
1428 +
                            graph, id, summary, author, age, branches, progress, diffstat,
1429 +
                        ]);
1422 1430
                    }
1423 -
                    let author_suffix_cols = author_suffix.size(Constraint::UNBOUNDED).cols;
1424 -
                    let rows = if table.size(Constraint::UNBOUNDED).cols + author_suffix_cols + 2
1425 -
                        <= cols
1426 -
                    {
1427 -
                        commit_table_lines(&table, &author_suffix, cols)
1428 -
                    } else {
1429 -
                        let mut suffix = Table::<4, Line>::new(TableOptions {
1430 -
                            spacing: 2,
1431 -
                            ..TableOptions::default()
1432 -
                        });
1433 -
                        for slot in &self.commits {
1434 -
                            suffix.push(commit_row_suffix(
1435 -
                                slot,
1436 -
                                palette,
1437 -
                                now,
1438 -
                                largest_change_count,
1439 -
                                diffstat_width,
1440 -
                            ));
1441 -
                        }
1442 -
                        commit_table_lines(&table, &suffix, cols)
1443 -
                    };
1431 +
                    let rows = table.lines(Constraint::tight(cols));
1444 1432
                    let selected = Style::default().bg(SELECTED_COMMIT_BG);
1445 1433
                    let list = List::new(rows)
1446 1434
                        .selected_styles(selected, selected)
1447 1435
                        .scrollbar(DiffViewer::scrollbar_style());
1448 1436
                    frame.render_stateful_widget(list, body, &mut self.list);
a.rs +0 -0
crates/brain/src/session/status.rs +10 -99
346 346
        Line::new(Label::new(&slot.commit.id.short()).fg(colors::rgb::ORANGE)),
347 347
        Line::new(slot.commit.summary.as_str()),
348 348
    ]
349 349
}
350 350
351 -
/// Returns the protected commit columns without an author column.
351 +
/// Returns the optional right-side columns for one commit-table row.
352 +
///
353 +
/// Columns are ordered by their wide-layout position: author, age, branches,
354 +
/// review progress, and diffstat.
352 355
pub(super) fn commit_row_suffix(
353 356
    slot: &CommitSlot,
354 357
    palette: DiffPalette,
355 358
    now: u64,
356 359
    largest_change_count: usize,
357 360
    max_graph_width: usize,
358 -
) -> [Line; 4] {
359 -
    let [_, age, branches, progress, diffstat] =
360 -
        commit_row_suffix_with_author(slot, palette, now, largest_change_count, max_graph_width);
361 -
    [age, branches, progress, diffstat]
362 -
}
363 -
364 -
/// Returns the protected commit columns including an author column.
365 -
pub(super) fn commit_row_suffix_with_author(
366 -
    slot: &CommitSlot,
367 -
    palette: DiffPalette,
368 -
    now: u64,
369 -
    largest_change_count: usize,
370 -
    max_graph_width: usize,
371 361
) -> [Line; 5] {
372 362
    let stats = slot.remaining_stats();
373 363
    let diffstat = match slot.state {
374 364
        CommitReviewState::Pending => {
375 365
            history_diffstat(stats, largest_change_count, max_graph_width, palette)
443 433
        .item(Label::space())
444 434
        .item(Label::new(&"+".repeat(addition_width)).fg(palette.positive_fg))
445 435
        .item(Label::new(&"-".repeat(deletion_width)).fg(palette.negative_fg))
446 436
}
447 437
448 -
/// Renders commit rows while reserving the right columns from truncation.
449 -
pub(super) fn commit_table_lines<const W: usize, const S: usize>(
450 -
    metadata: &Table<W, Line>,
451 -
    suffix: &Table<S, Line>,
452 -
    cols: usize,
453 -
) -> Vec<Line> {
454 -
    let suffix_cols = suffix.size(Constraint::UNBOUNDED).cols.min(cols);
455 -
    let spacing = cols.saturating_sub(suffix_cols).min(2);
456 -
    let metadata_cols = cols - suffix_cols - spacing;
457 -
    let metadata = metadata.lines(Constraint::tight(metadata_cols));
458 -
    let suffix = suffix.lines(Constraint::tight(suffix_cols));
459 -
460 -
    metadata
461 -
        .into_iter()
462 -
        .zip(suffix)
463 -
        .map(|(metadata, suffix)| {
464 -
            (0..spacing)
465 -
                .fold(metadata, |line, _| line.item(Label::space()))
466 -
                .extend(suffix)
467 -
        })
468 -
        .collect()
469 -
}
470 -
471 438
/// Renders one stored comment as a bordered card.
472 439
pub(super) fn comment_card_lines(comment: &Comment, selected: bool, cols: usize) -> Vec<Line> {
473 440
    let border = if selected {
474 441
        SELECTED_COMMENT_BORDER
475 442
    } else {
667 634
668 635
        assert!(rendered.ends_with("━━━━━━━━ · ✔ ↕ "));
669 636
    }
670 637
671 638
    #[test]
672 -
    fn commit_row_reserves_stats_suffix_before_truncating_metadata() {
639 +
    fn completed_commit_rows_hide_diff_summary() {
673 640
        let now = 2_000_000_000;
674 -
        let recency = CommitTime {
675 -
            seconds: now - 3_600,
676 -
        };
677 641
        let stats = DiffStat {
678 642
            files_changed: 1,
679 643
            insertions: 2,
680 644
            deletions: 3,
681 645
        };
682 646
        let mut slot = CommitSlot {
683 647
            commit: Commit {
684 648
                id: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
685 -
                summary: "A commit summary that cannot fit in the remaining width".to_owned(),
649 +
                summary: "Summary".to_owned(),
686 650
                author: "Alice".to_owned(),
687 651
                branches: vec!["main".to_owned()],
688 -
                recency,
652 +
                recency: CommitTime {
653 +
                    seconds: now - 3_600,
654 +
                },
689 655
                stats,
690 656
            },
691 657
            progress: ReviewProgress {
692 658
                accepted: 0,
693 659
                total: 1,
696 662
            remaining_stats: stats,
697 663
            document: None,
698 664
            identities: None,
699 665
            loaded: None,
700 666
        };
701 -
        let mut table = Table::<3, Line>::new(TableOptions {
702 -
            spacing: 2,
703 -
            ..TableOptions::default()
704 -
        });
705 -
        table.push(commit_row(&slot, "──◉︎"));
706 -
        let mut suffix = Table::<5, Line>::new(TableOptions {
707 -
            spacing: 2,
708 -
            ..TableOptions::default()
709 -
        });
710 -
        suffix.push(commit_row_suffix_with_author(
711 -
            &slot,
712 -
            DiffPalette::default(),
713 -
            now as u64,
714 -
            stats.insertions + stats.deletions,
715 -
            8,
716 -
        ));
717 -
        let wide_cols =
718 -
            table.size(Constraint::UNBOUNDED).cols + suffix.size(Constraint::UNBOUNDED).cols + 2;
719 -
        let wide = commit_table_lines(&table, &suffix, wide_cols)
720 -
            .pop()
721 -
            .unwrap()
722 -
            .to_string();
723 -
        let narrow_cols = wide_cols - slot.commit.summary.chars().count() + 1;
724 -
        let narrow = commit_table_lines(&table, &suffix, narrow_cols)
725 -
            .pop()
726 -
            .unwrap()
727 -
            .to_string();
728 -
729 -
        assert!(wide.contains(&slot.commit.summary), "line: {wide:?}");
730 -
        assert!(wide.ends_with("━━━━━━━━  5 ++++----"), "line: {wide:?}");
731 -
        assert!(narrow.contains("main"), "line: {narrow:?}");
732 -
        assert!(narrow.ends_with("━━━━━━━━  5 ++++----"), "line: {narrow:?}");
733 -
734 -
        let mut hidden = Table::<4, Line>::new(TableOptions {
735 -
            spacing: 2,
736 -
            ..TableOptions::default()
737 -
        });
738 -
        hidden.push(commit_row_suffix(
739 -
            &slot,
740 -
            DiffPalette::default(),
741 -
            now as u64,
742 -
            stats.insertions + stats.deletions,
743 -
            8,
744 -
        ));
745 -
        assert_eq!(
746 -
            suffix.size(Constraint::UNBOUNDED).cols,
747 -
            hidden.size(Constraint::UNBOUNDED).cols + "Alice".len() + 2
748 -
        );
749 -
        assert!(
750 -
            !commit_table_lines(&table, &hidden, wide_cols)
751 -
                .pop()
752 -
                .unwrap()
753 -
                .to_string()
754 -
                .contains("Alice")
755 -
        );
756 667
757 668
        for state in [CommitReviewState::Accepted, CommitReviewState::Rejected] {
758 669
            slot.state = state;
759 670
            assert!(
760 671
                commit_row_suffix(
761 672
                    &slot,
762 673
                    DiffPalette::default(),
763 674
                    now as u64,
764 675
                    stats.insertions + stats.deletions,
765 676
                    8,
766 -
                )[3]
677 +
                )[4]
767 678
                .to_string()
768 679
                .is_empty()
769 680
            );
770 681
        }
771 682
    }