aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDaniel Jankowski <daniel.jankowski@rub.de>2023-12-26 12:07:33 +0100
committerDaniel Jankowski <daniel.jankowski@rub.de>2023-12-26 12:07:33 +0100
commit7e939328f9ec45fbcd856474dd7c464e3ff402bb (patch)
tree9bb682a5d63c4214d14d6ab25f80091a54c64bea /src
parentchore: update cargo deps (diff)
downloadsl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.tar.gz
sl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.tar.xz
sl0pstatus-7e939328f9ec45fbcd856474dd7c464e3ff402bb.zip
feat: implement cache for dynamic command rendering
Diffstat (limited to '')
-rw-r--r--src/render.rs24
-rw-r--r--src/widgets/command.rs11
2 files changed, 31 insertions, 4 deletions
diff --git a/src/render.rs b/src/render.rs
index 3f645f6..048b82d 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -1,3 +1,4 @@
+use cached::{proc_macro::cached, SizedCache};
use lazy_static::lazy_static;
use std::{collections::BTreeMap, sync::Arc};
@@ -20,6 +21,24 @@ pub struct FormattedPart {
pub content: String,
}
+#[cached(
+ type = "SizedCache<String, FormattedPart>",
+ create = "{ SizedCache::with_size(100) }",
+ convert = r#"{ (format.to_owned()) }"#
+)]
+pub fn formatted_part_from_string_cached(format: &str) -> FormattedPart {
+ FormattedPart::from_format_string(format)
+}
+
+#[cached(
+ type = "SizedCache<String, Vec<FormattedPart>>",
+ create = "{ SizedCache::with_size(100) }",
+ convert = r#"{ (config.to_owned()) }"#
+)]
+pub fn formatted_parts_from_string_cached(config: &str) -> Vec<FormattedPart> {
+ FormattedPart::multiple_from_format_string(config)
+}
+
impl FormattedPart {
pub fn multiple_from_format_string(config: &str) -> Vec<Self> {
config
@@ -144,6 +163,11 @@ fn hex_to_rgb(s: &str) -> anyhow::Result<Vec<u8>> {
.collect()
}
+#[cached(
+ type = "SizedCache<String, Option<Color>>",
+ create = "{ SizedCache::with_size(100) }",
+ convert = r#"{ (color.to_owned()) }"#
+)]
fn parse_color(color: &str) -> Option<Color> {
if color.starts_with('#') {
let rgb = match hex_to_rgb(color.strip_prefix('#').unwrap()) {
diff --git a/src/widgets/command.rs b/src/widgets/command.rs
index 22b2892..b9e9aba 100644
--- a/src/widgets/command.rs
+++ b/src/widgets/command.rs
@@ -8,9 +8,10 @@ use std::{
use chrono::{DateTime, Duration, Local};
use regex::Regex;
+#[cfg(not(feature = "bench"))]
use zellij_tile::shim::run_command;
-use crate::render::FormattedPart;
+use crate::render::{formatted_parts_from_string_cached, FormattedPart};
use crate::{config::ZellijState, widgets::widget::Widget};
@@ -113,7 +114,7 @@ impl Widget for CommandWidget {
}
fn render_dynamic_formatted_content(content: &str) -> String {
- FormattedPart::multiple_from_format_string(content)
+ formatted_parts_from_string_cached(content)
.iter()
.map(|fp| fp.format_string(&fp.content))
.collect::<Vec<String>>()
@@ -133,9 +134,11 @@ fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &Zell
);
let command = commandline_parser(&command_config.command);
- let command = command.iter().map(|x| x.as_str()).collect::<Vec<&str>>();
#[cfg(not(feature = "bench"))]
- run_command(&command, context);
+ run_command(
+ &command.iter().map(|x| x.as_str()).collect::<Vec<&str>>(),
+ context,
+ );
}
}