src/items/messagebar.rs

89 lines · 2941 bytes

 1use crate::{
 2    config::{AppearanceConfig, MainConfig},
 3    global::structs::{Message, Status},
 4};
 5use tui::{
 6    layout::Rect,
 7    style::Style,
 8    widgets::{Block, Borders, Paragraph},
 9};
10use tui_additions::framework::FrameworkItem;
11
12/// a message bar item, contains no fields because the message is taken from `data.global.Message`
13#[derive(Clone, Copy, Default)]
14pub struct MessageBar;
15
16impl FrameworkItem for MessageBar {
17    fn render(
18        &mut self,
19        frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
20        framework: &mut tui_additions::framework::FrameworkClean,
21        area: tui::layout::Rect,
22        popup_render: bool,
23        _info: tui_additions::framework::ItemInfo,
24    ) {
25        if popup_render {
26            return;
27        }
28
29        let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
30        let message = framework.data.global.get::<Message>().unwrap();
31
32        // the Option<TextList> that is Some if keys were captured for entering command
33        let command_capture = &framework
34            .data
35            .global
36            .get::<Status>()
37            .unwrap()
38            .command_capture;
39
40        // display with different border style according to type of message and config
41        let block = Block::default()
42            .borders(Borders::ALL)
43            .border_type(appearance.borders)
44            .border_style(Style::default().fg(if command_capture.is_some() {
45                appearance.colors.command_capture
46            } else {
47                match message {
48                    Message::None => appearance.colors.outline,
49                    Message::Success(_) => appearance.colors.message_success_outline,
50                    Message::Error(_) => appearance.colors.message_error_outline,
51                    Message::Message(_) => appearance.colors.message_outline,
52                }
53            }));
54
55        // if keys are captured, render the textlist instead of the message text, and exits the
56        // function
57        if let Some(textfield) = command_capture {
58            let paragraph = Paragraph::new(":").block(block);
59            frame.render_widget(paragraph, area);
60            let mut textfield = textfield.clone();
61            textfield.set_width(area.width - 3);
62            let _ = textfield.update();
63            frame.render_widget(
64                textfield,
65                Rect::new(area.x + 2, area.y + 1, area.width - 3, 1),
66            );
67
68            return;
69        }
70
71        let paragraph = Paragraph::new(
72            message.to_string(
73                &framework
74                    .data
75                    .global
76                    .get::<MainConfig>()
77                    .unwrap()
78                    .message_bar_default,
79            ),
80        )
81        .block(block);
82
83        frame.render_widget(paragraph, area);
84    }
85
86    fn selectable(&self) -> bool {
87        false
88    }
89}