hex_patch/app/
status_bar.rs

1use ratatui::text::{Line, Span, Text};
2
3use super::{log::NotificationLevel, App};
4
5impl App {
6    pub(super) fn build_status_bar(&self) -> Text<'static> {
7        let mut status_bar = Text::default();
8        status_bar.style = self.settings.color.status_bar;
9        let mut line = Line {
10            style: self.settings.color.status_bar,
11            ..Default::default()
12        };
13        let max_len = self.screen_size.0 as usize;
14        let current_position = self.get_cursor_position();
15
16        line.spans
17            .push(Span::styled(" ", self.settings.color.status_bar));
18
19        let (notification_str, notification_style) = match self.logger.get_notification_level() {
20            NotificationLevel::None => (" ", self.settings.color.status_bar),
21            NotificationLevel::Debug => ("●", self.settings.color.status_debug),
22            NotificationLevel::Info => ("●", self.settings.color.status_info),
23            NotificationLevel::Warning => ("●", self.settings.color.status_warning),
24            NotificationLevel::Error => ("●", self.settings.color.status_error),
25        };
26        line.spans
27            .push(Span::styled(notification_str, notification_style));
28        line.spans
29            .push(Span::styled(" ", self.settings.color.status_bar));
30        if self.logger.get_notification_level() != NotificationLevel::None {
31            line.spans.push(Span::styled(
32                self.logger[self.logger.len() - 1]
33                    .message
34                    .chars()
35                    .take(max_len - 25)
36                    .collect::<String>(),
37                self.settings.color.status_bar,
38            ));
39        }
40
41        let current_location_span = Span::styled(
42            format!(
43                "{:16X} {} ",
44                current_position.global_byte_index,
45                if current_position.high_byte { "H" } else { "L" }
46            ),
47            self.settings.color.status_bar,
48        );
49        let space_number =
50            max_len as isize - line.width() as isize - current_location_span.width() as isize - 2;
51        if space_number < 0 {
52            return Text::default();
53        }
54        let space_number = space_number as usize;
55        let padding_spaces_string = " ".repeat(space_number);
56
57        line.spans.push(Span::raw(padding_spaces_string));
58        line.spans.push(current_location_span);
59        status_bar.lines.push(line);
60        status_bar
61    }
62}