Small optimizations

This commit is contained in:
Marco De Araujo 2026-01-12 14:37:52 -04:00
parent 3d69f530d4
commit 389f2b886b

View file

@ -18,8 +18,7 @@ use ratatui::{
const STATUS_LINE_LENGTH: usize = 100;
const MAX_NAME_LENGTH: usize = 30;
static STATUS_CHARS: OnceLock<HashMap<MonitorStatus, Cow<'static, str>>> = OnceLock::new();
static STATUS_SPANS: OnceLock<HashMap<MonitorStatus, Span<'static>>> = OnceLock::new();
pub fn render_monitor_list(main_frame: &mut Frame, area: Rect, state: &mut DashboardViewState) {
let available_height = area.height as usize;
@ -150,17 +149,6 @@ fn render_monitor_table(
frame.render_widget(table, area);
}
fn get_status_char(status: &MonitorStatus) -> Cow<'static, str> {
let map = STATUS_CHARS.get_or_init(|| {
let mut m = HashMap::new();
m.insert(MonitorStatus::Up, Cow::Borrowed(""));
m.insert(MonitorStatus::Down, Cow::Borrowed(""));
m.insert(MonitorStatus::Unknown, Cow::Borrowed(""));
m
});
map.get(status).cloned().unwrap_or(Cow::Borrowed("?"))
}
pub fn get_status_color(status: &MonitorStatus) -> Color {
match status {
MonitorStatus::Up => Color::Green,
@ -170,8 +158,8 @@ pub fn get_status_color(status: &MonitorStatus) -> Color {
}
pub fn get_status_emoji(status: &MonitorStatus) -> Cow<'static, str> {
static UP_ICON: &str ="";
static DOWN_ICON: &str ="";
static UP_ICON: &str = "";
static DOWN_ICON: &str = "";
static UNKNOWN_ICON: &str = "";
match status {
MonitorStatus::Up => Cow::Borrowed(UP_ICON),
@ -217,21 +205,33 @@ fn get_formated_line(text: String, color: Color, modifier: Modifier) -> Line<'st
)])
}
fn get_cached_status_span(status: &MonitorStatus) -> Span<'static> {
let cache = STATUS_SPANS.get_or_init(|| {
let mut m = HashMap::new();
m.insert(
MonitorStatus::Up,
Span::styled("", Style::default().fg(Color::Green)),
);
m.insert(
MonitorStatus::Down,
Span::styled("", Style::default().fg(Color::Red)),
);
m.insert(
MonitorStatus::Unknown,
Span::styled("", Style::default().fg(Color::Yellow)),
);
m
});
cache.get(status).cloned().unwrap_or_default()
}
fn create_status_line_spans(status_history: &[MonitorStatus]) -> Line<'_> {
let recent_status: Vec<_> = status_history
let spans: Vec<_> = status_history
.iter()
.rev()
.take(STATUS_LINE_LENGTH)
.map(|status| get_cached_status_span(status))
.collect();
let mut spans = Vec::with_capacity(recent_status.len());
for status in recent_status.iter().rev() {
let c = get_status_char(status);
let color = get_status_color(status);
spans.push(Span::styled(c.to_string(), Style::default().fg(color)));
}
Line::from(spans)
}