brain: Add contextual action bar

6cf3f7993518947ce19db276d6b3a7fc44b072a0
Show mode-specific action hints beneath the status bar so review
controls are visible without duplicating hunk-only help text.

Assisted-by: Codex:gpt-5.5
Alexis Sellier committed ago 1 parent d4033ca2
crates/brain/src/session.rs +31 -18
23 23
mod status;
24 24
25 25
use commit::{CommitReview, CommitSlot};
26 26
use event::{ContextFetch, Resize, TerminalEvent, Update, context_change};
27 27
use hunk::{CommentEdit, HunkView};
28 -
use status::{Status, commit_row, viewer_area};
28 +
use status::{ActionContext, Chrome, Status, commit_row, viewer_area};
29 29
30 30
pub(crate) use status::{BRAIN_BADGE_BG, progress_bar};
31 31
32 32
/// Terminal backend used by the interactive viewer.
33 33
type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>;
892 892
        }
893 893
    }
894 894
895 895
    /// Draws one terminal frame without scheduling highlight work.
896 896
    fn draw_frame(&mut self) -> io::Result<()> {
897 -
        let mut footer = self.footer();
897 +
        let mut chrome = self.chrome();
898 898
        let current = self.current;
899 899
        match &mut self.mode {
900 900
            Mode::Hunk(view) => {
901 901
                let mut comments = self.commits[current]
902 902
                    .loaded
903 903
                    .as_ref()
904 904
                    .map_or_else(Vec::new, |loaded| {
905 905
                        loaded.comments.for_hunk(view.hunk).to_vec()
906 906
                    });
907 907
                self.terminal.draw(|frame| {
908 -
                    let (body, footer_area) = Status::areas(frame.area);
908 +
                    let (body, chrome_area) = Chrome::areas(frame.area);
909 909
                    frame.render_stateful_widget(&mut **view, body, &mut comments);
910 -
                    frame.render_widget(&mut footer, footer_area);
910 +
                    frame.render_widget(&mut chrome, chrome_area);
911 911
                })?;
912 912
            }
913 913
            Mode::Commits => {
914 914
                let palette = self.options.diff_palette;
915 915
                let rows: Vec<Line> = self
916 916
                    .commits
917 917
                    .iter()
918 918
                    .map(|slot| commit_row(slot, palette))
919 919
                    .collect();
920 920
                self.terminal.draw(|frame| {
921 -
                    let (body, footer_area) = Status::areas(frame.area);
921 +
                    let (body, chrome_area) = Chrome::areas(frame.area);
922 922
                    frame.render_stateful_widget(List::new(rows), body, &mut self.list);
923 -
                    frame.render_widget(&mut footer, footer_area);
923 +
                    frame.render_widget(&mut chrome, chrome_area);
924 924
                })?;
925 925
            }
926 926
            Mode::Diff => {
927 927
                self.terminal.draw(|frame| {
928 -
                    let (body, footer_area) = Status::areas(frame.area);
928 +
                    let (body, chrome_area) = Chrome::areas(frame.area);
929 929
                    if let Some(loaded) = &mut self.commits[current].loaded {
930 930
                        frame.render_widget(&mut loaded.viewer, body);
931 931
                    }
932 -
                    frame.render_widget(&mut footer, footer_area);
932 +
                    frame.render_widget(&mut chrome, chrome_area);
933 933
                })?;
934 934
            }
935 935
        }
936 936
        // The terminal caret is a pure function of the active view: only the
937 937
        // hunk view's comment editor shows it.
938 938
        self.terminal
939 939
            .set_cursor_visible(matches!(self.mode, Mode::Hunk(ref view) if view.is_editing()))
940 940
    }
941 941
942 -
    /// Returns the footer status line for the current commit.
943 -
    fn footer(&self) -> Status {
942 +
    /// Returns the footer chrome for the current session mode.
943 +
    fn chrome(&self) -> Chrome {
944 944
        let slot = &self.commits[self.current];
945 945
        let progress = slot
946 946
            .loaded
947 947
            .as_ref()
948 948
            .map_or_else(ReviewProgress::default, |loaded| ReviewProgress {
949 949
                accepted: loaded.review.accepted().len(),
950 950
                total: loaded.file_hunks.values().map(Vec::len).sum(),
951 951
            });
952 -
        Status {
953 -
            mode: self.mode.label(),
954 -
            short: slot.commit.id.short(),
955 -
            summary: slot.commit.summary.clone(),
956 -
            progress,
957 -
            stats: slot.commit.stats,
958 -
            palette: self.options.diff_palette,
959 -
            visibility: self.visibility,
952 +
        Chrome {
953 +
            status: Status {
954 +
                mode: self.mode.label(),
955 +
                short: slot.commit.id.short(),
956 +
                summary: slot.commit.summary.clone(),
957 +
                progress,
958 +
                stats: slot.commit.stats,
959 +
                palette: self.options.diff_palette,
960 +
                visibility: self.visibility,
961 +
            },
962 +
            actions: self.action_context(),
963 +
        }
964 +
    }
965 +
966 +
    /// Returns the action-bar context for the current session mode.
967 +
    fn action_context(&self) -> ActionContext {
968 +
        match &self.mode {
969 +
            Mode::Commits => ActionContext::Commits,
970 +
            Mode::Diff => ActionContext::Commit,
971 +
            Mode::Hunk(view) if view.is_editing() => ActionContext::HunkEdit,
972 +
            Mode::Hunk(_) => ActionContext::HunkBrowse,
960 973
        }
961 974
    }
962 975
}
crates/brain/src/session/hunk.rs +13 -32
1 1
//! Hunk comment editor view.
2 2
3 3
use rustik_diff::{DiffViewer, HunkId};
4 -
use rustik_tui::layout::Layout;
5 4
use rustik_tui::{
6 -
    Buffer, Color, Constraint, Direction, Length, Line, Rect, RenderResult, Size, StatefulWidget,
7 -
    Style, TextField, TextInput, Widget,
5 +
    Buffer, Color, Constraint, Line, Rect, RenderResult, Size, StatefulWidget, Style, TextField,
6 +
    TextInput, Widget,
8 7
};
9 8
10 9
use super::status::{
11 10
    COMMENT_CARD_CHROME_COLS, COMMENT_CARD_CHROME_ROWS, COMMENT_CARD_ROWS, comment_age,
12 -
    comment_card_lines, comment_editor_lines, heading,
11 +
    comment_card_lines, comment_editor_lines,
13 12
};
14 13
use crate::comments::{Comment, CommentId};
15 14
16 15
/// Maximum rows the comment input grows to.
17 16
const MAX_INPUT_ROWS: usize = 6;
18 17
/// Maximum rows used to list a hunk's existing comments.
19 18
const MAX_COMMENT_ROWS: usize = 8;
20 -
/// Hint shown above the comment input box.
21 -
const BROWSE_HINT: &str = "comments · c new · e edit · d delete · n/p select · Esc back";
22 -
/// Hint shown above the comment input box.
23 -
const EDIT_HINT: &str = "comment · Enter newline · Ctrl-Enter save · Esc cancel";
24 -
25 19
/// Comment editor target.
26 20
#[derive(Clone, Debug, Eq, PartialEq)]
27 21
pub(super) enum CommentEdit {
28 22
    /// Add a new comment.
29 23
    Add,
177 171
        *selected = comment;
178 172
    }
179 173
180 174
    /// Returns the area occupied by the isolated hunk viewer.
181 175
    pub(super) fn viewer_area(&self, area: Rect, comment_rows: usize) -> Rect {
182 -
        self.areas(area, comment_rows)[0]
176 +
        self.areas(area, comment_rows).0
183 177
    }
184 178
185 179
    /// Returns rendered rows needed for `comments` stored cards and the editor.
186 180
    ///
187 181
    /// A comment being edited is rendered as the editor card in its place, so
214 208
        } else {
215 209
            0
216 210
        }
217 211
    }
218 212
219 -
    /// Returns the hunk, comment, spacer, and input areas.
220 -
    fn areas(&self, area: Rect, comment_rows: usize) -> [Rect; 4] {
221 -
        let hint_h = 1.min(area.rows);
222 -
        let available = area.rows.saturating_sub(hint_h);
223 -
        let comments_h = comment_rows.min(available);
224 -
        let hunk_available = available.saturating_sub(comments_h);
213 +
    /// Returns the hunk and comment areas.
214 +
    fn areas(&self, area: Rect, comment_rows: usize) -> (Rect, Rect) {
215 +
        let comments_h = comment_rows.min(area.rows);
216 +
        let hunk_available = area.rows.saturating_sub(comments_h);
225 217
        let hunk_h = self
226 218
            .viewer
227 219
            .size(Constraint::max(Size::new(area.cols, hunk_available)))
228 220
            .rows
229 221
            .min(hunk_available);
230 222
231 -
        Layout::new(Direction::Vertical)
232 -
            .child(Length::Fixed(hunk_h))
233 -
            .child(Length::Fixed(comments_h))
234 -
            .child(Length::Fill(1))
235 -
            .child(Length::Fixed(hint_h))
236 -
            .areas(area)
223 +
        (
224 +
            Rect::new(area.x, area.y, area.cols, hunk_h),
225 +
            Rect::new(area.x, area.y + hunk_h, area.cols, comments_h),
226 +
        )
237 227
    }
238 228
}
239 229
240 230
impl HunkView {
241 231
    /// Returns lines for every comment card, swapping the edited card for the
308 298
    ) -> RenderResult {
309 299
        if area.is_empty() {
310 300
            return RenderResult::default();
311 301
        }
312 302
        self.clamp_selection(comments.len());
313 -
        let [hunk_area, comments_area, _, hint_area] =
314 -
            self.areas(area, self.comment_rows(comments.len()));
303 +
        let (hunk_area, comments_area) = self.areas(area, self.comment_rows(comments.len()));
315 304
        let mut result = RenderResult::default();
316 305
317 306
        self.viewer.render(hunk_area, buf);
318 307
319 308
        let (lines, editor_row) = self.comment_lines(comments, comments_area.cols);
342 331
                    .placeholder("Write a comment...")
343 332
                    .render_with_state(body_area, buf, input);
344 333
            }
345 334
        }
346 335
347 -
        let hint = if self.is_editing() {
348 -
            EDIT_HINT
349 -
        } else {
350 -
            BROWSE_HINT
351 -
        };
352 -
        if !hint_area.is_empty() {
353 -
            buf.set_line(hint_area.x, hint_area.y, &heading(hint, area.cols));
354 -
        }
355 336
        result
356 337
    }
357 338
}
358 339
359 340
#[cfg(test)]
a.txt +0 -0
crates/brain/src/session/status.rs +154 -21
12 12
use crate::comments::Comment;
13 13
use crate::review::Visibility;
14 14
15 15
use super::commit::CommitSlot;
16 16
17 +
/// Number of rows used by the status bar.
18 +
const STATUS_ROWS: usize = 1;
19 +
/// Number of rows used by the action bar.
20 +
const ACTION_ROWS: usize = 1;
17 21
/// Number of rows reserved for the bottom footer when possible.
18 -
const FOOTER_ROWS: usize = 1;
22 +
const CHROME_ROWS: usize = STATUS_ROWS + ACTION_ROWS;
19 23
/// Foreground color for dim footer text.
20 24
const FOOTER_FG: Color = Color::Fixed(240);
21 25
/// Background color for the footer row, matching file border chrome.
22 26
const FOOTER_BG: Color = colors::FAINT;
23 27
/// Background color for the brain application badge.
44 48
const COMMIT_PROGRESS_BAR_GLYPH: &str = "━";
45 49
/// Separator shown between footer status segments: ·.
46 50
const FOOTER_SEPARATOR: &str = " · ";
47 51
/// Marker used when footer text truncates: ….
48 52
const FOOTER_ELLIPSIS: &str = "…";
53 +
/// Separator shown between plain action hints.
54 +
const ACTION_SEPARATOR: &str = " · ";
55 +
/// Key label shown for Tab: ⭾.
56 +
const TAB_KEY: &str = "⭾";
57 +
/// Key label shown for Return: ⮐.
58 +
const RETURN_KEY: &str = "⮐";
59 +
/// Key label shown for Ctrl-Return.
60 +
const CTRL_RETURN_KEY: &str = "ctrl-⮐";
61 +
/// Foreground color for contextual action labels.
62 +
const ACTION_LABEL_FG: Color = Color::Fixed(244);
63 +
/// Foreground color for contextual action keys.
64 +
const ACTION_KEY_FG: Color = Color::Fixed(250);
49 65
/// Foreground color for active context-expansion mode.
50 66
const FULL_FILE_MODE_FG: Color = colors::rgb::TEAL;
67 +
/// Action hints shown in commit-list mode.
68 +
const COMMITS_ACTIONS: &[(&str, &str)] = &[
69 +
    (TAB_KEY, "commit"),
70 +
    ("j/k", "select"),
71 +
    (RETURN_KEY, "open"),
72 +
    ("q", "quit"),
73 +
];
74 +
/// Action hints shown in commit diff mode.
75 +
const COMMIT_ACTIONS: &[(&str, &str)] = &[
76 +
    (TAB_KEY, "history"),
77 +
    ("n/p", "hunk"),
78 +
    ("a", "accept hunk"),
79 +
    ("c", "comment"),
80 +
];
81 +
/// Action hints shown while browsing hunk comments.
82 +
const HUNK_BROWSE_ACTIONS: &[(&str, &str)] = &[
83 +
    (TAB_KEY, "history"),
84 +
    ("c", "new"),
85 +
    ("e", "edit"),
86 +
    ("d", "delete"),
87 +
    ("n/p", "select"),
88 +
    ("esc", "commit"),
89 +
];
90 +
/// Action hints shown while editing a hunk comment.
91 +
const HUNK_EDIT_ACTIONS: &[(&str, &str)] = &[
92 +
    (RETURN_KEY, "newline"),
93 +
    (CTRL_RETURN_KEY, "save"),
94 +
    ("esc", "cancel"),
95 +
    (TAB_KEY, "history"),
96 +
];
51 97
/// Rendered rows in one stored comment card.
52 98
pub(super) const COMMENT_CARD_ROWS: usize = 4;
53 99
/// Rows of card chrome around a comment body: the top border, the header row,
54 100
/// and the bottom border.
55 101
pub(super) const COMMENT_CARD_CHROME_ROWS: usize = 3;
76 122
    pub(super) visibility: Visibility,
77 123
}
78 124
79 125
impl Widget for Status {
80 126
    fn size(&self, parent: Constraint) -> Size {
81 -
        Size::new(parent.max.cols, FOOTER_ROWS).constrain(parent)
127 +
        Size::new(parent.max.cols, STATUS_ROWS).constrain(parent)
82 128
    }
83 129
84 130
    fn render(&mut self, area: Rect, buf: &mut Buffer) {
85 131
        if area.is_empty() {
86 132
            return;
89 135
        self.bar(area.cols).render(area, buf);
90 136
    }
91 137
}
92 138
93 139
impl Status {
94 -
    /// Returns the content and footer areas for a full session frame.
95 -
    pub(super) fn areas(area: Rect) -> (Rect, Rect) {
96 -
        area.split_bottom(FOOTER_ROWS)
97 -
    }
98 -
99 140
    /// Returns the full-width status row background.
100 141
    fn background(cols: usize) -> Line {
101 142
        Line::new(Label::space().bg(FOOTER_BG)).padded(cols)
102 143
    }
103 144
164 205
            .item(Label::space())
165 206
            .style(Style::default().bg(FOOTER_BG))
166 207
    }
167 208
}
168 209
210 +
/// Context that determines which action hints are shown.
211 +
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
212 +
pub(super) enum ActionContext {
213 +
    /// Commit-list mode.
214 +
    Commits,
215 +
    /// Commit diff mode.
216 +
    Commit,
217 +
    /// Hunk comments are being browsed.
218 +
    HunkBrowse,
219 +
    /// A hunk comment is being edited.
220 +
    HunkEdit,
221 +
}
222 +
223 +
impl ActionContext {
224 +
    /// Returns the action hints for this context.
225 +
    fn actions(self) -> &'static [(&'static str, &'static str)] {
226 +
        match self {
227 +
            Self::Commits => COMMITS_ACTIONS,
228 +
            Self::Commit => COMMIT_ACTIONS,
229 +
            Self::HunkBrowse => HUNK_BROWSE_ACTIONS,
230 +
            Self::HunkEdit => HUNK_EDIT_ACTIONS,
231 +
        }
232 +
    }
233 +
}
234 +
235 +
/// Bottom chrome containing the status bar and contextual action bar.
236 +
#[derive(Debug)]
237 +
pub(super) struct Chrome {
238 +
    /// Current commit status.
239 +
    pub(super) status: Status,
240 +
    /// Active action context.
241 +
    pub(super) actions: ActionContext,
242 +
}
243 +
244 +
impl Chrome {
245 +
    /// Returns the content and bottom chrome areas for a full session frame.
246 +
    pub(super) fn areas(area: Rect) -> (Rect, Rect) {
247 +
        area.split_bottom(CHROME_ROWS)
248 +
    }
249 +
250 +
    /// Returns the status and action areas inside the chrome area.
251 +
    fn rows(area: Rect) -> (Rect, Rect) {
252 +
        area.split_top(STATUS_ROWS)
253 +
    }
254 +
255 +
    /// Returns the rendered action row.
256 +
    fn action_line(&self, cols: usize) -> Line {
257 +
        let mut line = Line::blank();
258 +
        let mut first = true;
259 +
        for (key, label) in self.actions.actions() {
260 +
            if !first {
261 +
                line = line.item(Label::new(ACTION_SEPARATOR).fg(FOOTER_FG));
262 +
            }
263 +
            first = false;
264 +
            line = line
265 +
                .item(
266 +
                    Label::new(key)
267 +
                        .fg(ACTION_KEY_FG)
268 +
                        .style(Style::default().bold()),
269 +
                )
270 +
                .item(Label::space())
271 +
                .item(Label::new(label).fg(ACTION_LABEL_FG));
272 +
        }
273 +
        line.truncated(cols, "")
274 +
    }
275 +
}
276 +
277 +
impl Widget for Chrome {
278 +
    fn size(&self, parent: Constraint) -> Size {
279 +
        Size::new(parent.max.cols, CHROME_ROWS).constrain(parent)
280 +
    }
281 +
282 +
    fn render(&mut self, area: Rect, buf: &mut Buffer) {
283 +
        if area.is_empty() {
284 +
            return;
285 +
        }
286 +
        let (status_area, action_area) = Self::rows(area);
287 +
        self.status.render(status_area, buf);
288 +
        if !action_area.is_empty() {
289 +
            self.action_line(action_area.cols).render(action_area, buf);
290 +
        }
291 +
    }
292 +
}
293 +
169 294
/// Returns a compact commit review progress bar.
170 295
///
171 296
/// The empty segment renders in `empty_fg`, chosen by the caller to fit its
172 297
/// background.
173 298
pub(crate) fn progress_bar(
188 313
        .item(Label::new(&COMMIT_PROGRESS_BAR_GLYPH.repeat(empty)).fg(empty_fg))
189 314
}
190 315
191 316
/// Returns the viewer area above the footer chrome.
192 317
pub(super) fn viewer_area(area: Rect) -> Rect {
193 -
    Status::areas(area).0
194 -
}
195 -
196 -
/// Returns a dim, full-width heading bar carrying `text`.
197 -
pub(super) fn heading(text: &str, cols: usize) -> Line {
198 -
    Line::new(Label::new(text).fg(FOOTER_FG))
199 -
        .style(Style::default().bg(FOOTER_BG))
200 -
        .padded(cols)
201 -
        .truncated(cols, "")
318 +
    Chrome::areas(area).0
202 319
}
203 320
204 321
/// Renders one commit-list row: hash, summary, and diff statistics.
205 322
pub(super) fn commit_row(slot: &CommitSlot, palette: DiffPalette) -> Line {
206 323
    let stats = slot.commit.stats;
315 432
316 433
    #[test]
317 434
    fn viewer_area_reserves_footer() {
318 435
        assert_eq!(
319 436
            viewer_area(Rect::new(0, 0, 80, 20)),
320 -
            Rect::new(0, 0, 80, 19)
437 +
            Rect::new(0, 0, 80, 18)
321 438
        );
322 439
        assert_eq!(viewer_area(Rect::new(0, 0, 80, 1)), Rect::new(0, 0, 80, 0));
323 440
    }
324 441
325 442
    #[test]
326 -
    fn status_areas_reserve_footer() {
443 +
    fn chrome_areas_reserve_footer() {
327 444
        assert_eq!(
328 -
            Status::areas(Rect::new(0, 0, 80, 20)),
329 -
            (Rect::new(0, 0, 80, 19), Rect::new(0, 19, 80, 1))
445 +
            Chrome::areas(Rect::new(0, 0, 80, 20)),
446 +
            (Rect::new(0, 0, 80, 18), Rect::new(0, 18, 80, 2))
330 447
        );
331 448
        assert_eq!(
332 -
            Status::areas(Rect::new(0, 0, 80, 1)),
449 +
            Chrome::areas(Rect::new(0, 0, 80, 1)),
333 450
            (Rect::new(0, 0, 80, 0), Rect::new(0, 0, 80, 1))
334 451
        );
335 452
    }
336 453
454 +
    #[test]
455 +
    fn action_context_changes_action_hints() {
456 +
        assert!(
457 +
            ActionContext::Commit
458 +
                .actions()
459 +
                .iter()
460 +
                .any(|(_, label)| *label == "accept hunk")
461 +
        );
462 +
        assert!(
463 +
            ActionContext::HunkEdit
464 +
                .actions()
465 +
                .iter()
466 +
                .any(|(_, label)| *label == "save")
467 +
        );
468 +
    }
469 +
337 470
    #[test]
338 471
    fn comment_card_lines_render_a_bordered_comment_card() {
339 472
        let comment = Comment {
340 473
            id: 1.into(),
341 474
            author: "alice".to_owned(),