diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2025-01-18 09:32:18 +0100 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2025-01-18 09:34:07 +0100 |
| commit | f73635159598119a67533fda394e49830506cd85 (patch) | |
| tree | 78f3c2b0052af68d4bdcd8b31e548af613d34814 /src/widgets | |
| parent | chore: update cargo deps (diff) | |
| download | sl0pstatus-f73635159598119a67533fda394e49830506cd85.tar.gz sl0pstatus-f73635159598119a67533fda394e49830506cd85.tar.xz sl0pstatus-f73635159598119a67533fda394e49830506cd85.zip | |
feat: add pipe widget
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/mod.rs | 1 | ||||
| -rw-r--r-- | src/widgets/pipe.rs | 97 |
2 files changed, 98 insertions, 0 deletions
diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index edbf868..aa60cc3 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -2,6 +2,7 @@ pub mod command; pub mod datetime; pub mod mode; pub mod notification; +pub mod pipe; pub mod session; pub mod swap_layout; pub mod tabs; diff --git a/src/widgets/pipe.rs b/src/widgets/pipe.rs new file mode 100644 index 0000000..f6acf1a --- /dev/null +++ b/src/widgets/pipe.rs @@ -0,0 +1,97 @@ +use lazy_static::lazy_static; +use regex::Regex; +use std::collections::BTreeMap; + +use crate::render::FormattedPart; + +use super::widget::Widget; + +lazy_static! { + static ref PIPE_REGEX: Regex = Regex::new("_[a-zA-Z0-9]+$").unwrap(); +} + +pub struct PipeWidget { + config: BTreeMap<String, PipeConfig>, +} + +#[derive(Clone)] +struct PipeConfig { + format: Vec<FormattedPart>, +} + +impl PipeWidget { + pub fn new(config: &BTreeMap<String, String>) -> Self { + Self { + config: parse_config(config), + } + } +} + +impl Widget for PipeWidget { + fn process(&self, name: &str, state: &crate::config::ZellijState) -> String { + let pipe_config = match self.config.get(name) { + Some(pc) => pc, + None => { + tracing::debug!("pipe no name {name}"); + return "".to_owned(); + } + }; + + let pipe_result = match state.pipe_results.get(name) { + Some(pr) => pr, + None => { + tracing::debug!("pipe no content {name}"); + return "".to_owned(); + } + }; + + pipe_config + .format + .iter() + .map(|f| { + let mut content = f.content.clone(); + + if content.contains("{output}") { + content = content.replace( + "{output}", + pipe_result.strip_suffix('\n').unwrap_or(pipe_result), + ) + } + + (f, content) + }) + .fold("".to_owned(), |acc, (f, content)| { + format!("{acc}{}", f.format_string(&content)) + }) + } + + fn process_click(&self, _name: &str, _state: &crate::config::ZellijState, _pos: usize) {} +} + +fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, PipeConfig> { + let mut keys: Vec<String> = zj_conf + .keys() + .filter(|k| k.starts_with("pipe_")) + .cloned() + .collect(); + keys.sort(); + + let mut config: BTreeMap<String, PipeConfig> = BTreeMap::new(); + + for key in keys { + let pipe_name = PIPE_REGEX.replace(&key, "").to_string(); + let mut pipe_conf = PipeConfig { format: vec![] }; + + if let Some(existing_conf) = config.get(pipe_name.as_str()) { + pipe_conf = existing_conf.clone(); + } + + if key.ends_with("format") { + pipe_conf.format = + FormattedPart::multiple_from_format_string(zj_conf.get(&key).unwrap(), zj_conf); + } + + config.insert(pipe_name, pipe_conf); + } + config +} |
