1
//! A field caption as Turbo Vision draws it: lit while the control it names has the focus.
2

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

            
8
use crate::{Role, Theme};
9

            
10
/// The text of a field caption, drawn as it is.
11
pub struct Label<'a> {
12
    theme: &'a Theme,
13
    text: &'a str,
14
    lit: bool,
15
}
16

            
17
impl<'a> Label<'a> {
18
    /// `lit` is whether the linked control has the focus.
19
    #[must_use]
20
11030
    pub fn new(theme: &'a Theme, text: &'a str, lit: bool) -> Self {
21
11030
        Label { theme, text, lit }
22
11030
    }
23
}
24

            
25
impl Widget for Label<'_> {
26
11030
    fn render(self, area: Rect, buf: &mut Buffer) {
27
11030
        let role = if self.lit {
28
1297
            Role::LabelLit
29
        } else {
30
9733
            Role::Label
31
        };
32
11030
        let base = self.theme.style(role);
33
11030
        buf.set_style(area.intersection(buf.area), base);
34
11030
        Paragraph::new(Line::styled(self.text, base)).render(area, buf);
35
11030
    }
36
}
37

            
38
#[cfg(test)]
39
mod tests;