diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2024-06-02 12:32:23 +0200 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2024-06-02 12:32:23 +0200 |
| commit | d669610cae0051b60dfb9564a52582611968dfad (patch) | |
| tree | dfa1909c86f5612139d679f6f1747310a7aa71bf /src | |
| parent | chore: update cargo deps (diff) | |
| download | sl0pstatus-d669610cae0051b60dfb9564a52582611968dfad.tar.gz sl0pstatus-d669610cae0051b60dfb9564a52582611968dfad.tar.xz sl0pstatus-d669610cae0051b60dfb9564a52582611968dfad.zip | |
feat: implement click action for command widget
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 22 | ||||
| -rw-r--r-- | src/widgets/command.rs | 35 | ||||
| -rw-r--r-- | src/widgets/datetime.rs | 2 | ||||
| -rw-r--r-- | src/widgets/mode.rs | 2 | ||||
| -rw-r--r-- | src/widgets/notification.rs | 2 | ||||
| -rw-r--r-- | src/widgets/session.rs | 2 | ||||
| -rw-r--r-- | src/widgets/swap_layout.rs | 2 | ||||
| -rw-r--r-- | src/widgets/tabs.rs | 2 | ||||
| -rw-r--r-- | src/widgets/widget.rs | 2 |
9 files changed, 54 insertions, 17 deletions
diff --git a/src/config.rs b/src/config.rs index f1eb111..6d7612e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -210,6 +210,7 @@ impl ModuleConfig { } if !output_center.is_empty() { + tracing::debug!("widgetclick center"); offset += console::measure_text_width(&self.get_spacer_left( &output_left, &output_center, @@ -256,18 +257,23 @@ impl ModuleConfig { let mut rendered_output = widget_string.clone(); - let tokens: Vec<String> = widget_map.keys().map(|k| format!("{{{k}}}")).collect(); + let tokens: Vec<String> = widget_map.keys().map(|k| k.to_owned()).collect(); - let widgets_regex = Regex::new("(\\{[a-z_]+\\})").unwrap(); + let widgets_regex = Regex::new("(\\{[a-z_0-9]+\\})").unwrap(); for widget in widgets_regex.captures_iter(widget_string.as_str()) { let match_name = widget.get(0).unwrap().as_str(); - if !tokens.contains(&match_name.to_owned()) { - continue; + let widget_key = match_name.trim_matches(|c| c == '{' || c == '}'); + let mut widget_key_name = widget_key; + + if widget_key.starts_with("command_") { + widget_key_name = "command"; } - let wid_name = match_name.trim_matches(|c| c == '{' || c == '}'); + if !tokens.contains(&widget_key_name.to_owned()) { + continue; + } - let wid = match widget_map.get(wid_name) { + let wid = match widget_map.get(widget_key_name) { Some(wid) => wid, None => continue, }; @@ -280,7 +286,7 @@ impl ModuleConfig { None => continue, }; - let wid_res = wid.process(wid_name, state); + let wid_res = wid.process(widget_key, state); rendered_output = rendered_output.replace(match_name, &wid_res); if click_pos < pos + offset @@ -289,7 +295,7 @@ impl ModuleConfig { continue; } - wid.process_click(state, click_pos - (pos + offset)); + wid.process_click(widget_key, state, click_pos - (pos + offset)); } console::measure_text_width(&rendered_output) diff --git a/src/widgets/command.rs b/src/widgets/command.rs index 83609c4..184d354 100644 --- a/src/widgets/command.rs +++ b/src/widgets/command.rs @@ -37,6 +37,7 @@ struct CommandConfig { cwd: Option<PathBuf>, interval: i64, render_mode: RenderMode, + click_action: String, } #[derive(Clone, Debug, Default)] @@ -113,7 +114,29 @@ impl Widget for CommandWidget { } } - fn process_click(&self, _state: &ZellijState, _pos: usize) {} + fn process_click(&self, name: &str, _state: &ZellijState, _pos: usize) { + let command_config = match self.config.get(name) { + Some(cc) => cc, + None => { + return; + } + }; + + if command_config.click_action.is_empty() { + return; + } + + let command = commandline_parser(&command_config.click_action); + let context: BTreeMap<String, String> = BTreeMap::new(); + + tracing::debug!("Running command {:?} {:?}", command, context); + + #[cfg(all(not(feature = "bench"), not(test)))] + run_command( + &command.iter().map(|x| x.as_str()).collect::<Vec<&str>>(), + context, + ); + } } fn render_dynamic_formatted_content(content: &str) -> String { @@ -190,6 +213,7 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC env: None, interval: 1, render_mode: RenderMode::Static, + click_action: "".to_owned(), }; if let Some(existing_conf) = config.get(command_name.as_str()) { @@ -199,7 +223,13 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC if key.ends_with("command") { command_conf .command - .clone_from(&zj_conf.get(&key).unwrap().to_owned()) + .clone_from(&zj_conf.get(&key).unwrap().to_owned()); + } + + if key.ends_with("clickaction") { + command_conf + .click_action + .clone_from(&zj_conf.get(&key).unwrap().to_owned()); } if key.ends_with("env") { @@ -437,6 +467,7 @@ mod test { cwd: None, interval, render_mode: RenderMode::Static, + click_action: "".to_owned(), }, "test", state, diff --git a/src/widgets/datetime.rs b/src/widgets/datetime.rs index 18390f2..3cf4721 100644 --- a/src/widgets/datetime.rs +++ b/src/widgets/datetime.rs @@ -63,5 +63,5 @@ impl Widget for DateTimeWidget { self.color_format.format_string(&output) } - fn process_click(&self, _state: &ZellijState, _pos: usize) {} + fn process_click(&self, _name: &str, _state: &ZellijState, _pos: usize) {} } diff --git a/src/widgets/mode.rs b/src/widgets/mode.rs index a452c53..68c9bfa 100644 --- a/src/widgets/mode.rs +++ b/src/widgets/mode.rs @@ -139,7 +139,7 @@ impl Widget for ModeWidget { }) } - fn process_click(&self, _state: &ZellijState, _pos: usize) {} + fn process_click(&self, _name: &str, _state: &ZellijState, _pos: usize) {} } impl ModeWidget { diff --git a/src/widgets/notification.rs b/src/widgets/notification.rs index 5023fa7..1b8cfcd 100644 --- a/src/widgets/notification.rs +++ b/src/widgets/notification.rs @@ -74,5 +74,5 @@ impl Widget for NotificationWidget { output.to_owned() } - fn process_click(&self, _state: &ZellijState, _pos: usize) {} + fn process_click(&self, _name: &str, _state: &ZellijState, _pos: usize) {} } diff --git a/src/widgets/session.rs b/src/widgets/session.rs index 4916a65..9e27f6d 100644 --- a/src/widgets/session.rs +++ b/src/widgets/session.rs @@ -18,5 +18,5 @@ impl Widget for SessionWidget { } } - fn process_click(&self, _state: &ZellijState, _pos: usize) {} + fn process_click(&self, _name: &str, _state: &ZellijState, _pos: usize) {} } diff --git a/src/widgets/swap_layout.rs b/src/widgets/swap_layout.rs index 7d36b14..d8f9ffb 100644 --- a/src/widgets/swap_layout.rs +++ b/src/widgets/swap_layout.rs @@ -66,7 +66,7 @@ impl Widget for SwapLayoutWidget { output } - fn process_click(&self, _state: &ZellijState, _pos: usize) { + fn process_click(&self, _name: &str, _state: &ZellijState, _pos: usize) { next_swap_layout() } } diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs index b0e9ce4..6853465 100644 --- a/src/widgets/tabs.rs +++ b/src/widgets/tabs.rs @@ -101,7 +101,7 @@ impl Widget for TabsWidget { output } - fn process_click(&self, state: &ZellijState, pos: usize) { + fn process_click(&self, _name: &str, state: &ZellijState, pos: usize) { let mut offset = 0; let mut index = 1; let mut counter = 0; diff --git a/src/widgets/widget.rs b/src/widgets/widget.rs index ff4a784..7087cba 100644 --- a/src/widgets/widget.rs +++ b/src/widgets/widget.rs @@ -2,5 +2,5 @@ use crate::config::ZellijState; pub trait Widget { fn process(&self, name: &str, state: &ZellijState) -> String; - fn process_click(&self, state: &ZellijState, pos: usize); + fn process_click(&self, name: &str, state: &ZellijState, pos: usize); } |
