|
| 1 | +use lazy_static::lazy_static; |
| 2 | +use regex::Regex; |
| 3 | +use std::collections::BTreeMap; |
| 4 | + |
| 5 | +use crate::render::FormattedPart; |
| 6 | + |
| 7 | +use super::widget::Widget; |
| 8 | + |
| 9 | +lazy_static! { |
| 10 | + static ref PIPE_REGEX: Regex = Regex::new("_[a-zA-Z0-9]+$").unwrap(); |
| 11 | +} |
| 12 | + |
| 13 | +pub struct PipeWidget { |
| 14 | + config: BTreeMap<String, PipeConfig>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Clone)] |
| 18 | +struct PipeConfig { |
| 19 | + format: Vec<FormattedPart>, |
| 20 | +} |
| 21 | + |
| 22 | +impl PipeWidget { |
| 23 | + pub fn new(config: &BTreeMap<String, String>) -> Self { |
| 24 | + Self { |
| 25 | + config: parse_config(config), |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl Widget for PipeWidget { |
| 31 | + fn process(&self, name: &str, state: &crate::config::ZellijState) -> String { |
| 32 | + let pipe_config = match self.config.get(name) { |
| 33 | + Some(pc) => pc, |
| 34 | + None => { |
| 35 | + tracing::debug!("pipe no name {name}"); |
| 36 | + return "".to_owned(); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + let pipe_result = match state.pipe_results.get(name) { |
| 41 | + Some(pr) => pr, |
| 42 | + None => { |
| 43 | + tracing::debug!("pipe no content {name}"); |
| 44 | + return "".to_owned(); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + pipe_config |
| 49 | + .format |
| 50 | + .iter() |
| 51 | + .map(|f| { |
| 52 | + let mut content = f.content.clone(); |
| 53 | + |
| 54 | + if content.contains("{output}") { |
| 55 | + content = content.replace( |
| 56 | + "{output}", |
| 57 | + pipe_result.strip_suffix('\n').unwrap_or(pipe_result), |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + (f, content) |
| 62 | + }) |
| 63 | + .fold("".to_owned(), |acc, (f, content)| { |
| 64 | + format!("{acc}{}", f.format_string(&content)) |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + fn process_click(&self, _name: &str, _state: &crate::config::ZellijState, _pos: usize) {} |
| 69 | +} |
| 70 | + |
| 71 | +fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, PipeConfig> { |
| 72 | + let mut keys: Vec<String> = zj_conf |
| 73 | + .keys() |
| 74 | + .filter(|k| k.starts_with("pipe_")) |
| 75 | + .cloned() |
| 76 | + .collect(); |
| 77 | + keys.sort(); |
| 78 | + |
| 79 | + let mut config: BTreeMap<String, PipeConfig> = BTreeMap::new(); |
| 80 | + |
| 81 | + for key in keys { |
| 82 | + let pipe_name = PIPE_REGEX.replace(&key, "").to_string(); |
| 83 | + let mut pipe_conf = PipeConfig { format: vec![] }; |
| 84 | + |
| 85 | + if let Some(existing_conf) = config.get(pipe_name.as_str()) { |
| 86 | + pipe_conf = existing_conf.clone(); |
| 87 | + } |
| 88 | + |
| 89 | + if key.ends_with("format") { |
| 90 | + pipe_conf.format = |
| 91 | + FormattedPart::multiple_from_format_string(zj_conf.get(&key).unwrap(), zj_conf); |
| 92 | + } |
| 93 | + |
| 94 | + config.insert(pipe_name, pipe_conf); |
| 95 | + } |
| 96 | + config |
| 97 | +} |
0 commit comments