src/items/messagebar.rs

86 lines · 2901 bytes

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