Skip to main content

tvui/
status_bar.rs

1//! The status bar: one row of hotkeys and their labels.
2
3use ratatui::buffer::Buffer;
4use ratatui::layout::Rect;
5use ratatui::text::{Line, Span};
6use ratatui::widgets::Widget;
7
8use crate::{Role, Theme};
9
10/// Hotkey and label pairs on one row: `F1 Help  M-x Exit`, the key in the `status-hotkey`
11/// role. An item that does not fit the width is left out whole; later, shorter ones still show.
12pub struct StatusBar<'a> {
13    theme: &'a Theme,
14    items: &'a [(String, String)],
15}
16
17impl<'a> StatusBar<'a> {
18    #[must_use]
19    pub fn new(theme: &'a Theme, items: &'a [(String, String)]) -> Self {
20        StatusBar { theme, items }
21    }
22
23    fn line(&self, width: usize) -> Line<'a> {
24        let key = self.theme.style(Role::StatusHotkey);
25        let label = self.theme.style(Role::Status);
26        let mut spans = Vec::new();
27        for slot in fit(self.items, width) {
28            let (hotkey, text) = &self.items[slot.index];
29            spans.extend([
30                Span::styled(" ".repeat(slot.gap), label),
31                Span::styled(hotkey.as_str(), key),
32                Span::styled(format!(" {text}"), label),
33            ]);
34        }
35        Line::from(spans)
36    }
37
38    /// The cells of each item shown in `area`, gap excluded, as `(index into items, area)`.
39    #[must_use]
40    pub fn item_areas(area: Rect, items: &[(String, String)]) -> Vec<(usize, Rect)> {
41        let mut x = area.x;
42        let mut areas = Vec::new();
43        for slot in fit(items, usize::from(area.width)) {
44            let start = x.saturating_add(u16::try_from(slot.gap).unwrap_or(u16::MAX));
45            let width = u16::try_from(slot.width - slot.gap).unwrap_or(u16::MAX);
46            areas.push((
47                slot.index,
48                Rect::new(start, area.y, width, 1).intersection(area),
49            ));
50            x = start.saturating_add(width);
51        }
52        areas
53    }
54}
55
56/// An item that fits: its index, the blank columns before it and its width with them.
57struct Slot {
58    index: usize,
59    gap: usize,
60    width: usize,
61}
62
63fn fit(items: &[(String, String)], width: usize) -> Vec<Slot> {
64    let mut used = 0;
65    let mut slots: Vec<Slot> = Vec::new();
66    for (index, (hotkey, text)) in items.iter().enumerate() {
67        let gap = if slots.is_empty() { 1 } else { 2 };
68        let wide = gap + hotkey.chars().count() + 1 + text.chars().count();
69        if used + wide > width {
70            continue;
71        }
72        used += wide;
73        slots.push(Slot {
74            index,
75            gap,
76            width: wide,
77        });
78    }
79    slots
80}
81
82impl Widget for StatusBar<'_> {
83    fn render(self, area: Rect, buf: &mut Buffer) {
84        buf.set_style(area, self.theme.style(Role::Status));
85        self.line(usize::from(area.width)).render(area, buf);
86    }
87}
88
89#[cfg(test)]
90mod tests;