diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-25 12:01:22 +0100 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-12-25 12:02:04 +0100 |
| commit | 336ace1ec9e4de0b372038dba6158c7dc567c833 (patch) | |
| tree | d52bf36750ab23eb3ee0564a1274315544aad1c1 /src | |
| parent | feat: don't call format_string if the command_xxx_format doesn't contains (#28) (diff) | |
| download | sl0pstatus-336ace1ec9e4de0b372038dba6158c7dc567c833.tar.gz sl0pstatus-336ace1ec9e4de0b372038dba6158c7dc567c833.tar.xz sl0pstatus-336ace1ec9e4de0b372038dba6158c7dc567c833.zip | |
feat: implement render modes for command widget
resolves #25
Diffstat (limited to 'src')
| -rw-r--r-- | src/border.rs | 2 | ||||
| -rw-r--r-- | src/config.rs | 6 | ||||
| -rw-r--r-- | src/render.rs | 10 | ||||
| -rw-r--r-- | src/widgets/command.rs | 45 | ||||
| -rw-r--r-- | src/widgets/datetime.rs | 2 | ||||
| -rw-r--r-- | src/widgets/mode.rs | 28 | ||||
| -rw-r--r-- | src/widgets/tabs.rs | 12 |
7 files changed, 66 insertions, 39 deletions
diff --git a/src/border.rs b/src/border.rs index e517b12..c3f0419 100644 --- a/src/border.rs +++ b/src/border.rs @@ -64,7 +64,7 @@ pub fn parse_border_config(config: BTreeMap<String, String>) -> Option<BorderCon Some(BorderConfig { enabled, char: char.to_owned(), - format: FormattedPart::from_format_string(format.to_owned()), + format: FormattedPart::from_format_string(format), position, }) } diff --git a/src/config.rs b/src/config.rs index dab66f1..7184a21 100644 --- a/src/config.rs +++ b/src/config.rs @@ -64,7 +64,7 @@ impl ModuleConfig { left_parts: parts_from_config(Some(&left_parts_config.to_owned())), right_parts_config: right_parts_config.to_owned(), right_parts: parts_from_config(Some(&right_parts_config.to_owned())), - format_space: FormattedPart::from_format_string(format_space_config.to_owned()), + format_space: FormattedPart::from_format_string(format_space_config), hide_frame_for_single_pane, border: border_config, } @@ -231,7 +231,7 @@ fn parts_from_config(format: Option<&String>) -> Vec<FormattedPart> { match format { Some(format) => format .split("#[") - .map(|f| FormattedPart::from_format_string(f.to_owned())) + .map(FormattedPart::from_format_string) .collect(), None => vec![], } @@ -246,7 +246,7 @@ mod test { fn test_formatted_part_from_string() { let input = "#[fg=#ff0000,bg=#00ff00,bold,italic]foo"; - let part = FormattedPart::from_format_string(input.to_owned()); + let part = FormattedPart::from_format_string(input); assert_eq!( part, diff --git a/src/render.rs b/src/render.rs index f4cc14f..3f645f6 100644 --- a/src/render.rs +++ b/src/render.rs @@ -21,16 +21,16 @@ pub struct FormattedPart { } impl FormattedPart { - pub fn multiple_from_format_string(config: String) -> Vec<Self> { + pub fn multiple_from_format_string(config: &str) -> Vec<Self> { config .split("#[") - .map(|c| FormattedPart::from_format_string(c.to_owned())) + .map(FormattedPart::from_format_string) .collect() } - pub fn from_format_string(format: String) -> Self { + pub fn from_format_string(format: &str) -> Self { let format = match format.starts_with("#[") { - true => format.strip_prefix("#[").unwrap().to_owned(), + true => format.strip_prefix("#[").unwrap(), false => format, }; @@ -39,7 +39,7 @@ impl FormattedPart { let mut format_content_split = format.split(']').collect::<Vec<&str>>(); if format_content_split.len() == 1 { - result.content = format; + result.content = format.to_owned(); return result; } diff --git a/src/widgets/command.rs b/src/widgets/command.rs index e9aebcd..22b2892 100644 --- a/src/widgets/command.rs +++ b/src/widgets/command.rs @@ -21,10 +21,18 @@ lazy_static! { } #[derive(Clone, Debug)] +enum RenderMode { + Static, + Dynamic, + Raw, +} + +#[derive(Clone, Debug)] struct CommandConfig { command: String, format: FormattedPart, interval: i64, + render_mode: RenderMode, } #[derive(Clone, Debug)] @@ -93,21 +101,28 @@ impl Widget for CommandWidget { .unwrap_or(&command_result.stderr), ); } - if command_config.format != FormattedPart::default() { - command_config.format.format_string(&content) - } - else{ - return content; + + match command_config.render_mode { + RenderMode::Static => command_config.format.format_string(&content), + RenderMode::Dynamic => render_dynamic_formatted_content(&content), + RenderMode::Raw => content, } } fn process_click(&self, _state: &ZellijState, _pos: usize) {} } +fn render_dynamic_formatted_content(content: &str) -> String { + FormattedPart::multiple_from_format_string(content) + .iter() + .map(|fp| fp.format_string(&fp.content)) + .collect::<Vec<String>>() + .join("") +} + fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &ZellijState) { let ts = Local::now(); - let last_run = - get_timestamp_from_event_or_default(name, state, command_config.interval); + let last_run = get_timestamp_from_event_or_default(name, state, command_config.interval); if ts.timestamp() - last_run.timestamp() >= command_config.interval { let mut context = BTreeMap::new(); @@ -141,6 +156,7 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC command: "".to_owned(), format: FormattedPart::default(), interval: 1, + render_mode: RenderMode::Static, }; if let Some(existing_conf) = config.get(command_name.as_str()) { @@ -152,14 +168,25 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC } if key.ends_with("format") { - command_conf.format = - FormattedPart::from_format_string(zj_conf.get(&key).unwrap().to_owned().clone()); + command_conf.format = FormattedPart::from_format_string(zj_conf.get(&key).unwrap()); } if key.ends_with("interval") { command_conf.interval = zj_conf.get(&key).unwrap().parse::<i64>().unwrap_or(1); } + if key.ends_with("rendermode") { + command_conf.render_mode = match zj_conf.get(&key) { + Some(mode) => match mode.as_str() { + "static" => RenderMode::Static, + "dynamic" => RenderMode::Dynamic, + "raw" => RenderMode::Raw, + _ => RenderMode::Static, + }, + None => RenderMode::Static, + }; + } + config.insert(command_name, command_conf); } diff --git a/src/widgets/datetime.rs b/src/widgets/datetime.rs index e846d34..18390f2 100644 --- a/src/widgets/datetime.rs +++ b/src/widgets/datetime.rs @@ -37,7 +37,7 @@ impl DateTimeWidget { Self { format: format.to_owned(), - color_format: FormattedPart::from_format_string(color_format.to_owned()), + color_format: FormattedPart::from_format_string(color_format), time_zone, } } diff --git a/src/widgets/mode.rs b/src/widgets/mode.rs index db73544..97d1239 100644 --- a/src/widgets/mode.rs +++ b/src/widgets/mode.rs @@ -27,72 +27,72 @@ pub struct ModeWidget { impl ModeWidget { pub fn new(config: &BTreeMap<String, String>) -> Self { let normal_format = match config.get("mode_normal") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => vec![], }; let locked_format = match config.get("mode_locked") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let resize_format = match config.get("mode_resize") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let pane_format = match config.get("mode_pane") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let tab_format = match config.get("mode_tab") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let scroll_format = match config.get("mode_scroll") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let enter_search_format = match config.get("mode_enter_search") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let search_format = match config.get("mode_search") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let rename_tab_format = match config.get("mode_rename_tab") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let rename_pane_format = match config.get("mode_rename_pane") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let session_format = match config.get("mode_session") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let move_format = match config.get("mode_move") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let prompt_format = match config.get("mode_prompt") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; let tmux_format = match config.get("mode_tmux") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_format.clone(), }; diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs index d8a292d..e7281ce 100644 --- a/src/widgets/tabs.rs +++ b/src/widgets/tabs.rs @@ -19,31 +19,31 @@ impl TabsWidget { pub fn new(config: &BTreeMap<String, String>) -> Self { let mut normal_tab_format: Vec<FormattedPart> = Vec::new(); if let Some(form) = config.get("tab_normal") { - normal_tab_format = FormattedPart::multiple_from_format_string(form.to_owned()); + normal_tab_format = FormattedPart::multiple_from_format_string(form); } let normal_tab_fullscreen_format = match config.get("tab_normal_fullscreen") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_tab_format.clone(), }; let normal_tab_sync_format = match config.get("tab_normal_sync") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => normal_tab_format.clone(), }; let mut active_tab_format = normal_tab_format.clone(); if let Some(form) = config.get("tab_active") { - active_tab_format = FormattedPart::multiple_from_format_string(form.to_owned()); + active_tab_format = FormattedPart::multiple_from_format_string(form); } let active_tab_fullscreen_format = match config.get("tab_active_fullscreen") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => active_tab_format.clone(), }; let active_tab_sync_format = match config.get("tab_active_sync") { - Some(form) => FormattedPart::multiple_from_format_string(form.to_owned()), + Some(form) => FormattedPart::multiple_from_format_string(form), None => active_tab_format.clone(), }; |
