1
//! The status bar: one row of hotkeys and their labels.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::Rect;
5
use ratatui::text::{Line, Span};
6
use ratatui::widgets::Widget;
7

            
8
use 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.
12
pub struct StatusBar<'a> {
13
    theme: &'a Theme,
14
    items: &'a [(String, String)],
15
}
16

            
17
impl<'a> StatusBar<'a> {
18
    #[must_use]
19
3688
    pub fn new(theme: &'a Theme, items: &'a [(String, String)]) -> Self {
20
3688
        StatusBar { theme, items }
21
3688
    }
22

            
23
3688
    fn line(&self, width: usize) -> Line<'a> {
24
3688
        let key = self.theme.style(Role::StatusHotkey);
25
3688
        let label = self.theme.style(Role::Status);
26
3688
        let mut spans = Vec::new();
27
3688
        for slot in fit(self.items, width) {
28
16187
            let (hotkey, text) = &self.items[slot.index];
29
16187
            spans.extend([
30
16187
                Span::styled(" ".repeat(slot.gap), label),
31
16187
                Span::styled(hotkey.as_str(), key),
32
16187
                Span::styled(format!(" {text}"), label),
33
16187
            ]);
34
        }
35
3688
        Line::from(spans)
36
3688
    }
37

            
38
    /// The cells of each item shown in `area`, gap excluded, as `(index into items, area)`.
39
    #[must_use]
40
3687
    pub fn item_areas(area: Rect, items: &[(String, String)]) -> Vec<(usize, Rect)> {
41
3687
        let mut x = area.x;
42
3687
        let mut areas = Vec::new();
43
3687
        for slot in fit(items, usize::from(area.width)) {
44
16201
            let start = x.saturating_add(u16::try_from(slot.gap).unwrap_or(u16::MAX));
45
16201
            let width = u16::try_from(slot.width - slot.gap).unwrap_or(u16::MAX);
46
16201
            areas.push((
47
16201
                slot.index,
48
16201
                Rect::new(start, area.y, width, 1).intersection(area),
49
16201
            ));
50
16201
            x = start.saturating_add(width);
51
        }
52
3687
        areas
53
3687
    }
54
}
55

            
56
/// An item that fits: its index, the blank columns before it and its width with them.
57
struct Slot {
58
    index: usize,
59
    gap: usize,
60
    width: usize,
61
}
62

            
63
7375
fn fit(items: &[(String, String)], width: usize) -> Vec<Slot> {
64
7375
    let mut used = 0;
65
7375
    let mut slots: Vec<Slot> = Vec::new();
66
7375
    for (index, (hotkey, text)) in items.iter().enumerate() {
67
43404
        let gap = if slots.is_empty() { 1 } else { 2 };
68
43404
        let wide = gap + hotkey.chars().count() + 1 + text.chars().count();
69
43404
        if used + wide > width {
70
11016
            continue;
71
32388
        }
72
32388
        used += wide;
73
32388
        slots.push(Slot {
74
32388
            index,
75
32388
            gap,
76
32388
            width: wide,
77
32388
        });
78
    }
79
7375
    slots
80
7375
}
81

            
82
impl Widget for StatusBar<'_> {
83
3688
    fn render(self, area: Rect, buf: &mut Buffer) {
84
3688
        buf.set_style(area, self.theme.style(Role::Status));
85
3688
        self.line(usize::from(area.width)).render(area, buf);
86
3688
    }
87
}
88

            
89
#[cfg(test)]
90
mod tests;