String Optizations
This commit is contained in:
parent
3929a4a277
commit
3d69f530d4
2 changed files with 22 additions and 12 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
use crate::i18n::t;
|
use crate::i18n::t;
|
||||||
use crate::ui::dashboard::{
|
use crate::ui::dashboard::{
|
||||||
|
|
@ -17,6 +19,8 @@ use ratatui::{
|
||||||
const STATUS_LINE_LENGTH: usize = 100;
|
const STATUS_LINE_LENGTH: usize = 100;
|
||||||
const MAX_NAME_LENGTH: usize = 30;
|
const MAX_NAME_LENGTH: usize = 30;
|
||||||
|
|
||||||
|
static STATUS_CHARS: OnceLock<HashMap<MonitorStatus, Cow<'static, str>>> = OnceLock::new();
|
||||||
|
|
||||||
pub fn render_monitor_list(main_frame: &mut Frame, area: Rect, state: &mut DashboardViewState) {
|
pub fn render_monitor_list(main_frame: &mut Frame, area: Rect, state: &mut DashboardViewState) {
|
||||||
let available_height = area.height as usize;
|
let available_height = area.height as usize;
|
||||||
let max_scroll = state.get_total_lenght().saturating_sub(available_height);
|
let max_scroll = state.get_total_lenght().saturating_sub(available_height);
|
||||||
|
|
@ -31,7 +35,7 @@ pub fn render_monitor_list(main_frame: &mut Frame, area: Rect, state: &mut Dashb
|
||||||
let mut lines_skipped = 0;
|
let mut lines_skipped = 0;
|
||||||
let half = state.get_total_lenght().saturating_div(2);
|
let half = state.get_total_lenght().saturating_div(2);
|
||||||
|
|
||||||
for (i, group) in state.groups.iter().enumerate() {
|
for (_i, group) in state.groups.iter().enumerate() {
|
||||||
let group_height = group.monitors.len() + BORDER_LINES_VIEW;
|
let group_height = group.monitors.len() + BORDER_LINES_VIEW;
|
||||||
|
|
||||||
if lines_skipped + group_height <= scroll_pos {
|
if lines_skipped + group_height <= scroll_pos {
|
||||||
|
|
@ -146,12 +150,15 @@ fn render_monitor_table(
|
||||||
frame.render_widget(table, area);
|
frame.render_widget(table, area);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_status_char(status: &MonitorStatus) -> char {
|
fn get_status_char(status: &MonitorStatus) -> Cow<'static, str> {
|
||||||
match status {
|
let map = STATUS_CHARS.get_or_init(|| {
|
||||||
MonitorStatus::Up => '■',
|
let mut m = HashMap::new();
|
||||||
MonitorStatus::Down => '■',
|
m.insert(MonitorStatus::Up, Cow::Borrowed("■"));
|
||||||
MonitorStatus::Unknown => '■',
|
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 {
|
pub fn get_status_color(status: &MonitorStatus) -> Color {
|
||||||
|
|
@ -162,11 +169,14 @@ pub fn get_status_color(status: &MonitorStatus) -> Color {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_status_emoji(status: &MonitorStatus) -> &str {
|
pub fn get_status_emoji(status: &MonitorStatus) -> Cow<'static, str> {
|
||||||
|
static UP_ICON: &str ="✅";
|
||||||
|
static DOWN_ICON: &str ="❌";
|
||||||
|
static UNKNOWN_ICON: &str = "❓";
|
||||||
match status {
|
match status {
|
||||||
MonitorStatus::Up => "✅",
|
MonitorStatus::Up => Cow::Borrowed(UP_ICON),
|
||||||
MonitorStatus::Down => "❌",
|
MonitorStatus::Down => Cow::Borrowed(DOWN_ICON),
|
||||||
MonitorStatus::Unknown => "❓",
|
MonitorStatus::Unknown => Cow::Borrowed(UNKNOWN_ICON),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use rayon::prelude::*;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
pub const BORDER_LINES_VIEW: usize = 3;
|
pub const BORDER_LINES_VIEW: usize = 3;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum MonitorStatus {
|
pub enum MonitorStatus {
|
||||||
Up,
|
Up,
|
||||||
Down,
|
Down,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue