perf: add cache for icon colors (#89)

This commit is contained in:
Bertrand Chardon 2024-12-02 16:20:13 +01:00 committed by GitHub
parent b7ddb00c4e
commit fee4ed2671
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 4 deletions

View File

@ -47,6 +47,7 @@ pub struct Television {
/// are rendered correctly even when resizing the terminal while still
/// benefiting from a cache mechanism.
pub meta_paragraph_cache: HashMap<(String, u16, u16), Paragraph<'static>>,
pub icon_color_cache: HashMap<String, Color>,
pub(crate) spinner: Spinner,
pub(crate) spinner_state: SpinnerState,
}
@ -81,6 +82,7 @@ impl Television {
preview_pane_height: 0,
current_preview_total_lines: 0,
meta_paragraph_cache: HashMap::new(),
icon_color_cache: HashMap::new(),
spinner,
spinner_state: SpinnerState::from(&spinner),
}

View File

@ -65,6 +65,7 @@ impl Television {
.result_name_fg(mode_color(self.mode)),
),
self.config.ui.use_nerd_font_icons,
&mut self.icon_color_cache,
);
f.render_stateful_widget(

View File

@ -8,6 +8,7 @@ use ratatui::widgets::{
Block, BorderType, Borders, List, ListDirection, Padding,
};
use ratatui::Frame;
use std::collections::HashMap;
use std::str::FromStr;
use television_channels::channels::OnAir;
use television_channels::entry::Entry;
@ -69,6 +70,7 @@ pub fn build_results_list<'a, 'b>(
list_direction: ListDirection,
results_list_colors: Option<ResultsListColors>,
use_icons: bool,
icon_color_cache: &mut HashMap<String, Color>,
) -> List<'a>
where
'b: 'a,
@ -79,10 +81,21 @@ where
// optional icon
if let Some(icon) = entry.icon.as_ref() {
if use_icons {
spans.push(Span::styled(
icon.to_string(),
Style::default().fg(Color::from_str(icon.color).unwrap()),
));
if let Some(icon_color) = icon_color_cache.get(icon.color) {
spans.push(Span::styled(
icon.to_string(),
Style::default().fg(*icon_color),
));
} else {
let icon_color = Color::from_str(icon.color).unwrap();
icon_color_cache
.insert(icon.color.to_string(), icon_color);
spans.push(Span::styled(
icon.to_string(),
Style::default().fg(icon_color),
));
}
spans.push(Span::raw(" "));
}
}
@ -203,6 +216,7 @@ impl Television {
},
None,
self.config.ui.use_nerd_font_icons,
&mut self.icon_color_cache,
);
f.render_stateful_widget(