Improve status bars

66aeffd95676cf897e4dc823608ffbf49604569a
Alexis Sellier committed ago 1 parent bda488b4
crates/brain/src/session/status.rs +28 -2
18 18
const STATUS_ROWS: usize = 1;
19 19
/// Number of rows used by the action bar.
20 20
const ACTION_ROWS: usize = 1;
21 21
/// Number of rows reserved for the bottom footer when possible.
22 22
const CHROME_ROWS: usize = STATUS_ROWS + ACTION_ROWS;
23 +
/// Empty columns between status content and the status bar's right edge.
24 +
const STATUS_RIGHT_PADDING: usize = 1;
23 25
/// Foreground color for dim footer text.
24 26
const FOOTER_FG: Color = Color::Fixed(240);
25 27
/// Background color for the footer row, matching file border chrome.
26 28
const FOOTER_BG: Color = colors::FAINT;
27 29
/// Background color for the brain application badge.
151 153
                Line::new(Label::new(error).fg(self.palette.negative_fg))
152 154
                    .truncated(cols, FOOTER_ELLIPSIS),
153 155
            );
154 156
        }
155 157
        let right = self.right_status();
156 -
        let left_width = cols.saturating_sub(right.width()).saturating_sub(1);
158 +
        let left_width = cols
159 +
            .saturating_sub(right.width())
160 +
            .saturating_sub(STATUS_RIGHT_PADDING)
161 +
            .saturating_sub(1);
157 162
158 163
        HStack::default()
159 164
            .child(self.left_status().truncated(left_width, FOOTER_ELLIPSIS))
160 165
            .spacer()
161 166
            .child(right)
167 +
            .child(Line::new(Label::space().bg(FOOTER_BG)).padded(STATUS_RIGHT_PADDING))
162 168
    }
163 169
164 170
    /// Returns the left status section with application, mode, summary, and hash.
165 171
    fn left_status(&self) -> Line {
166 172
        Line::blank()
208 214
            .item(Label::new(&format!("{files} file{plural}")).fg(FOOTER_VALUE_FG))
209 215
            .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG))
210 216
            .item(Label::new(APPROVED_HUNKS_MODE_ICON).fg(accepted_fg))
211 217
            .item(Label::space())
212 218
            .item(Label::new(FULL_FILE_MODE_ICON).fg(full_file_fg))
213 -
            .item(Label::space())
214 219
            .style(Style::default().bg(FOOTER_BG))
215 220
    }
216 221
}
217 222
218 223
/// Context that determines which action hints are shown.
496 501
            buffer.get(0, 0).unwrap().style().fg_color(),
497 502
            palette.negative_fg
498 503
        );
499 504
    }
500 505
506 +
    #[test]
507 +
    fn status_renders_right_padding() {
508 +
        let mut status = Status {
509 +
            mode: "commit",
510 +
            short: "1234567".to_owned(),
511 +
            summary: "summary".to_owned(),
512 +
            progress: ReviewProgress::default(),
513 +
            stats: DiffStat::default(),
514 +
            palette: DiffPalette::default(),
515 +
            visibility: Visibility::default(),
516 +
            error: None,
517 +
        };
518 +
        let mut buffer = Buffer::empty(Rect::new(0, 0, 80, 1));
519 +
520 +
        status.render(buffer.area(), &mut buffer);
521 +
522 +
        assert_eq!(buffer.get(78, 0).unwrap().glyph(), FULL_FILE_MODE_ICON);
523 +
        assert_eq!(buffer.get(79, 0).unwrap().glyph(), " ");
524 +
        assert_eq!(buffer.get(79, 0).unwrap().style().bg_color(), FOOTER_BG);
525 +
    }
526 +
501 527
    #[test]
502 528
    fn comment_card_lines_render_a_bordered_comment_card() {
503 529
        let comment = Comment {
504 530
            id: 1.into(),
505 531
            author: "alice".to_owned(),
crates/diff/src/bin/rustik-diff/session.rs +45 -6
20 20
21 21
use crate::error::Error;
22 22
23 23
/// Number of rows reserved for the bottom footer when possible.
24 24
const FOOTER_ROWS: usize = 1;
25 +
/// Empty columns between status content and the status bar's right edge.
26 +
const STATUS_RIGHT_PADDING: usize = 1;
25 27
/// Foreground color for dim footer text.
26 28
const FOOTER_FG: Color = Color::Fixed(240);
27 29
/// Background color for the footer row, matching file border chrome.
28 30
const FOOTER_BG: Color = colors::FAINT;
31 +
/// Dark purple background color for the application badge.
32 +
const APP_BADGE_BG: Color = Color::RGB(68, 36, 84);
29 33
/// Foreground color for neutral footer values.
30 34
const FOOTER_VALUE_FG: Color = Color::Fixed(250);
31 35
/// Separator shown between footer status segments: ·.
32 36
const FOOTER_SEPARATOR: &str = " · ";
33 37
275 279
    frame.render_widget(status_line(area.cols, stats, palette, debug), row);
276 280
}
277 281
278 282
/// Returns the padded bottom status line.
279 283
fn status_line(cols: usize, stats: Stats, palette: DiffPalette, debug: Option<&str>) -> Line {
284 +
    let badge = Line::new(
285 +
        Label::new(" diff ")
286 +
            .fg(Color::White)
287 +
            .bg(APP_BADGE_BG)
288 +
            .style(Style::default().bold()),
289 +
    );
290 +
    let stats_cols = cols
291 +
        .saturating_sub(badge.width())
292 +
        .saturating_sub(STATUS_RIGHT_PADDING)
293 +
        .saturating_sub(1);
294 +
280 295
    HStack::default()
281 -
        .child(
282 -
            Line::new(Label::new("diff").fg(FOOTER_FG))
283 -
                .style(Style::default().bg(FOOTER_BG))
284 -
                .padded(4),
285 -
        )
296 +
        .child(badge)
286 297
        .spacer()
287 -
        .child(status_stats(stats, palette, debug).truncated(cols.saturating_sub(5), ""))
298 +
        .child(status_stats(stats, palette, debug).truncated(stats_cols, ""))
299 +
        .child(Line::new(Label::space().bg(FOOTER_BG)).padded(STATUS_RIGHT_PADDING))
288 300
        .to_line(Constraint::tight(cols))
289 301
        .style(Style::default().bg(FOOTER_BG))
290 302
        .padded(cols)
291 303
        .truncated(cols, "")
292 304
}
334 346
            status_line(24, Stats::default(), DiffPalette::default(), None).width(),
335 347
            24
336 348
        );
337 349
    }
338 350
351 +
    #[test]
352 +
    fn status_line_renders_application_badge() {
353 +
        let mut line = status_line(24, Stats::default(), DiffPalette::default(), None);
354 +
        let mut buffer = tui::Buffer::empty(Rect::new(0, 0, 24, 1));
355 +
        let badge_style = Style::default().fg(Color::White).bg(APP_BADGE_BG).bold();
356 +
357 +
        line.render(buffer.area(), &mut buffer);
358 +
359 +
        let badge = (0..6)
360 +
            .map(|x| buffer.get(x, 0).unwrap().glyph())
361 +
            .collect::<String>();
362 +
        assert_eq!(badge, " diff ");
363 +
        assert!((0..6).all(|x| buffer.get(x, 0).unwrap().style() == badge_style));
364 +
    }
365 +
366 +
    #[test]
367 +
    fn status_line_renders_right_padding() {
368 +
        let mut line = status_line(24, Stats::default(), DiffPalette::default(), None);
369 +
        let mut buffer = tui::Buffer::empty(Rect::new(0, 0, 24, 1));
370 +
371 +
        line.render(buffer.area(), &mut buffer);
372 +
373 +
        assert_eq!(buffer.get(22, 0).unwrap().glyph(), "s");
374 +
        assert_eq!(buffer.get(23, 0).unwrap().glyph(), " ");
375 +
        assert_eq!(buffer.get(23, 0).unwrap().style().bg_color(), FOOTER_BG);
376 +
    }
377 +
339 378
    #[test]
340 379
    fn resize_requests_redraw_even_when_viewer_continues() {
341 380
        let resized = true;
342 381
        let control = DiffViewer::new(Default::default(), Default::default()).handle_input(
343 382
            Event::Resize(tui::Size::new(80, 24)),