aboutsummaryrefslogtreecommitdiffstats
path: root/src/render.rs
diff options
context:
space:
mode:
authorDaniel Jankowski <daniel.jankowski@rub.de>2023-12-17 13:46:19 +0100
committerDaniel Jankowski <daniel.jankowski@rub.de>2023-12-17 13:46:19 +0100
commitf51455d02e14c447fe174b2ae4869c24e83349d4 (patch)
treef441f8f2181fceab62975041b6b5b4f102e8f174 /src/render.rs
parentfeat: add more benches for widgets (diff)
downloadsl0pstatus-f51455d02e14c447fe174b2ae4869c24e83349d4.tar.gz
sl0pstatus-f51455d02e14c447fe174b2ae4869c24e83349d4.tar.xz
sl0pstatus-f51455d02e14c447fe174b2ae4869c24e83349d4.zip
feat: perf + mem optimization of rendering
Diffstat (limited to 'src/render.rs')
-rw-r--r--src/render.rs18
1 files changed, 11 insertions, 7 deletions
diff --git a/src/render.rs b/src/render.rs
index 64b39a5..370b01d 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -1,3 +1,4 @@
+use lazy_static::lazy_static;
use std::{collections::BTreeMap, sync::Arc};
use anstyle::{Ansi256Color, AnsiColor, Color, RgbColor, Style};
@@ -6,6 +7,10 @@ use zellij_tile::prelude::bail;
use crate::{config::ZellijState, widgets::widget::Widget};
+lazy_static! {
+ static ref WIDGET_REGEX: Regex = Regex::new("(\\{[a-z_0-9]+\\})").unwrap();
+}
+
#[derive(Clone, Debug, PartialEq)]
pub struct FormattedPart {
pub fg: Option<Color>,
@@ -66,7 +71,7 @@ impl FormattedPart {
result
}
- pub fn format_string(&self, text: String) -> String {
+ pub fn format_string(&self, text: &str) -> String {
let mut style = Style::new();
style = style.fg_color(self.fg);
@@ -91,13 +96,12 @@ impl FormattedPart {
pub fn format_string_with_widgets(
&self,
- widgets: BTreeMap<String, Arc<dyn Widget>>,
- state: ZellijState,
+ widgets: &BTreeMap<String, Arc<dyn Widget>>,
+ state: &ZellijState,
) -> String {
let mut output = self.content.clone();
- let widgets_regex = Regex::new("(\\{[a-z_0-9]+\\})").unwrap();
- for widget in widgets_regex.captures_iter(self.content.clone().as_str()) {
+ for widget in WIDGET_REGEX.captures_iter(&self.content) {
let match_name = widget.get(0).unwrap().as_str();
let mut widget_key = match_name.trim_matches(|c| c == '{' || c == '}');
@@ -108,7 +112,7 @@ impl FormattedPart {
let result = match widgets.get(widget_key) {
Some(widget) => widget.process(
match_name.trim_matches(|c| c == '{' || c == '}'),
- state.clone(),
+ state,
),
None => "Use of uninitialized widget".to_string(),
};
@@ -116,7 +120,7 @@ impl FormattedPart {
output = output.replace(match_name, &result);
}
- self.format_string(output)
+ self.format_string(&output)
}
}