|
| 1 | +use std::time::{Duration, Instant}; |
| 2 | + |
| 3 | +use ratatui::{ |
| 4 | + text::{Line, Span}, |
| 5 | + widgets::{LineGauge, Paragraph}, |
| 6 | +}; |
| 7 | +use tuirealm::{ |
| 8 | + AttrValue, Attribute, Component, Event, MockComponent, Props, State, |
| 9 | + command::CmdResult, |
| 10 | + props::{BorderType, Borders, Layout}, |
| 11 | + ratatui::{ |
| 12 | + Frame, |
| 13 | + layout::{Constraint, Direction}, |
| 14 | + prelude::Rect, |
| 15 | + style::{Color, Style}, |
| 16 | + widgets::Block, |
| 17 | + }, |
| 18 | +}; |
| 19 | + |
| 20 | +use crate::{ |
| 21 | + app::{ |
| 22 | + messages::Msg, |
| 23 | + user_events::{CscsEvent, StatusEvent, UserEvent}, |
| 24 | + }, |
| 25 | + config::Config, |
| 26 | +}; |
| 27 | + |
| 28 | +pub struct StatusBar { |
| 29 | + props: Props, |
| 30 | + last_updated: Instant, |
| 31 | + current_status: Option<StatusEvent>, |
| 32 | + status_clear_time: Duration, |
| 33 | + current_platform: String, |
| 34 | + current_system: String, |
| 35 | +} |
| 36 | + |
| 37 | +impl StatusBar { |
| 38 | + pub fn new() -> Self { |
| 39 | + let config = Config::new().unwrap(); |
| 40 | + Self { |
| 41 | + props: Props::default(), |
| 42 | + last_updated: Instant::now(), |
| 43 | + current_status: None, |
| 44 | + status_clear_time: Duration::from_secs(10), |
| 45 | + current_platform: config.cscs.current_platform.to_string(), |
| 46 | + current_system: config.cscs.current_system, |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl MockComponent for StatusBar { |
| 52 | + fn query(&self, attr: Attribute) -> Option<AttrValue> { |
| 53 | + self.props.get(attr) |
| 54 | + } |
| 55 | + |
| 56 | + fn attr(&mut self, attr: Attribute, value: AttrValue) { |
| 57 | + self.props.set(attr, value); |
| 58 | + } |
| 59 | + |
| 60 | + fn state(&self) -> State { |
| 61 | + State::None |
| 62 | + } |
| 63 | + |
| 64 | + fn perform(&mut self, _cmd: tuirealm::command::Cmd) -> CmdResult { |
| 65 | + CmdResult::None |
| 66 | + } |
| 67 | + fn view(&mut self, frame: &mut Frame, area: Rect) { |
| 68 | + if self.props.get_or(Attribute::Display, AttrValue::Flag(true)) == AttrValue::Flag(true) { |
| 69 | + let borders = Borders::default().modifiers(BorderType::Rounded); |
| 70 | + let div = Block::default() |
| 71 | + .borders(borders.sides) |
| 72 | + .border_style(borders.style()) |
| 73 | + .border_type(borders.modifiers); |
| 74 | + let layout = Layout::default() |
| 75 | + .constraints(&[Constraint::Percentage(30), Constraint::Percentage(70)]) |
| 76 | + .direction(Direction::Horizontal) |
| 77 | + .margin(1); |
| 78 | + frame.render_widget(div, area); |
| 79 | + |
| 80 | + let highlight_style = Style::default().fg(Color::Yellow); |
| 81 | + let info_style = Style::default().fg(Color::Blue); |
| 82 | + let warn_style = Style::default().fg(Color::Red); |
| 83 | + let system_status = Paragraph::new(Line::from(vec![ |
| 84 | + Span::styled("Platform: ", highlight_style), |
| 85 | + Span::raw(self.current_platform.clone().to_uppercase()), |
| 86 | + Span::raw(" "), |
| 87 | + Span::styled("System: ", highlight_style), |
| 88 | + Span::raw(self.current_system.clone()), |
| 89 | + ])); |
| 90 | + let chunks = layout.chunks(area); |
| 91 | + frame.render_widget(system_status, chunks[0]); |
| 92 | + if let Some(status) = self.current_status.clone() { |
| 93 | + match status { |
| 94 | + StatusEvent::Progress(msg, progress) => { |
| 95 | + let gauge = LineGauge::default() |
| 96 | + .filled_style(Style::default().fg(Color::DarkGray)) |
| 97 | + .label(msg) |
| 98 | + .ratio((progress as f64) / 100.0); |
| 99 | + frame.render_widget(gauge, chunks[1]); |
| 100 | + } |
| 101 | + StatusEvent::Info(info) => { |
| 102 | + let notification_status = Paragraph::new(info).style(info_style); |
| 103 | + frame.render_widget(notification_status, chunks[1]); |
| 104 | + } |
| 105 | + StatusEvent::Warning(warning) => { |
| 106 | + let notification_status = Paragraph::new(warning).style(warn_style); |
| 107 | + frame.render_widget(notification_status, chunks[1]); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +impl Component<Msg, UserEvent> for StatusBar { |
| 115 | + fn on(&mut self, ev: Event<UserEvent>) -> Option<Msg> { |
| 116 | + match ev { |
| 117 | + Event::Tick => { |
| 118 | + if self.last_updated.elapsed() > self.status_clear_time { |
| 119 | + self.current_status = None; |
| 120 | + } |
| 121 | + } |
| 122 | + Event::User(UserEvent::Status(status)) => { |
| 123 | + self.current_status = Some(status); |
| 124 | + self.last_updated = Instant::now(); |
| 125 | + } |
| 126 | + Event::User(UserEvent::Cscs(CscsEvent::SystemSelected(system))) => { |
| 127 | + self.current_system = system; |
| 128 | + } |
| 129 | + _ => {} |
| 130 | + } |
| 131 | + None |
| 132 | + } |
| 133 | +} |
0 commit comments