diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2024-02-23 11:01:09 +0100 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2024-02-23 11:01:09 +0100 |
| commit | 2f5f8fdbd9d0c78732c6293bc734474d79ae54f6 (patch) | |
| tree | 2a1535ac041a969c246a4b3f5ea44770e4bb92c1 | |
| parent | feat(tabs): add configurable indicators for sync, fullscreen & floating (diff) | |
| download | sl0pstatus-2f5f8fdbd9d0c78732c6293bc734474d79ae54f6.tar.gz sl0pstatus-2f5f8fdbd9d0c78732c6293bc734474d79ae54f6.tar.xz sl0pstatus-2f5f8fdbd9d0c78732c6293bc734474d79ae54f6.zip | |
feat: add format_center
| -rw-r--r-- | README.md | 13 | ||||
| -rw-r--r-- | plugin-dev-workspace.kdl | 7 | ||||
| -rw-r--r-- | src/config.rs | 122 |
3 files changed, 101 insertions, 41 deletions
@@ -88,9 +88,10 @@ layout { pane size=1 borderless=true { plugin location="file:target/wasm32-wasi/debug/zjstatus.wasm" { - format_left "{mode} #[fg=#89B4FA,bold]{session} {tabs}" - format_right "{command_git_branch} {datetime}" - format_space "" + format_left "{mode} #[fg=#89B4FA,bold]{session}" + format_center "{tabs}" + format_right "{command_git_branch} {datetime}" + format_space "" border_enabled "false" border_char "─" @@ -118,7 +119,7 @@ layout { } ``` -In order to start using zjstatus you need to specify the widgets you'd like to use under the `format_left` and/or `format_right` +In order to start using zjstatus you need to specify the widgets you'd like to use under the `format_left` and/or `format_center` and/or `format_right` configuration. Formatting can be done with `#[..]`, while widgets and properties are surrounded by `{..}`. The blank space between the left and the right part can be colored with `format_space`. @@ -260,7 +261,7 @@ mode_default_to_mode "tmux" **Handle** `{session}` Print the current session name. This module cannot be configured. For formatting, please put the Formatting -sequence right before the handle in `format_left` or `format_right`. +sequence right before the handle in `format_left`, `format_center` or `format_right`. #### swap layout @@ -268,7 +269,7 @@ sequence right before the handle in `format_left` or `format_right`. **Click behavior** Switch to the next swap layout Print the active swap layout. This module cannot be configured. For formatting, please put the Formatting -sequence right before the handle in `format_left` or `format_right`. +sequence right before the handle in `format_left`, `format_center` or `format_right`. #### tabs diff --git a/plugin-dev-workspace.kdl b/plugin-dev-workspace.kdl index 068aa15..58131cc 100644 --- a/plugin-dev-workspace.kdl +++ b/plugin-dev-workspace.kdl @@ -5,8 +5,9 @@ layout { pane size=2 borderless=true { plugin location="file:target/wasm32-wasi/debug/zjstatus.wasm" { - format_left "{mode} #[fg=#89B4FA,bg=#181825,bold]{session} {tabs} {command_0} {command_1} {command_git_branch} {command_3}" - format_right "{datetime}" + format_left "{mode} #[fg=#89B4FA,bg=#181825,bold]{session} {tabs} " + format_center "{command_0} {command_1} {command_git_branch} {command_3}" + format_right "{tabs} {datetime}" format_space "#[bg=#181825]" // foo @@ -24,7 +25,7 @@ layout { tab_normal "#[fg=#6C7086,bg=#181825] {index} {name} {floating_indicator} " tab_normal_fullscreen "#[fg=#6C7086,bg=#181825] {index} {name} [] " tab_normal_sync "#[fg=#6C7086,bg=#181825] {index} {name} <> " - tab_active "#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} {floating_total_count} {floating_indicator} {sync_indicator} {fullscreen_indicator}" + tab_active "#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} {floating_total_count}{floating_indicator}{sync_indicator}{fullscreen_indicator}" tab_separator "#[fg=#6C7086,bg=#181825] | " tab_floating_indicator "F" tab_sync_indicator "S" diff --git a/src/config.rs b/src/config.rs index 8d187fd..7037f0f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -48,6 +48,8 @@ pub fn event_mask_from_widget_name(name: &str) -> u8 { pub struct ModuleConfig { pub left_parts_config: String, pub left_parts: Vec<FormattedPart>, + pub center_parts_config: String, + pub center_parts: Vec<FormattedPart>, pub right_parts_config: String, pub right_parts: Vec<FormattedPart>, pub format_space: FormattedPart, @@ -77,6 +79,11 @@ impl ModuleConfig { None => "", }; + let center_parts_config = match config.get("format_center") { + Some(conf) => conf, + None => "", + }; + let border_config = match parse_border_config(config.clone()) { Some(bc) => bc, None => BorderConfig::default(), @@ -85,6 +92,8 @@ impl ModuleConfig { Self { left_parts_config: left_parts_config.to_owned(), left_parts: parts_from_config(Some(&left_parts_config.to_owned())), + center_parts_config: center_parts_config.to_owned(), + center_parts: parts_from_config(Some(¢er_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), @@ -108,15 +117,43 @@ impl ModuleConfig { Mouse::Release(_, y) => y, }; - let widget_string_left = self - .left_parts - .iter() - .fold(String::new(), |a, b| a + &b.content); + let output_left = self.left_parts.iter_mut().fold("".to_owned(), |acc, part| { + format!( + "{}{}", + acc, + part.format_string_with_widgets(&widget_map, &state) + ) + }); + + let mut offset = console::measure_text_width(&output_left); - let left_len = - self.process_widget_click(click_pos, widget_string_left, widget_map.clone(), &state, 0); + self.process_widget_click(click_pos, &self.left_parts, &widget_map, &state, 0); - if click_pos <= left_len { + if click_pos <= offset { + return; + } + + let output_center = self + .center_parts + .iter_mut() + .fold("".to_owned(), |acc, part| { + format!( + "{}{}", + acc, + part.format_string_with_widgets(&widget_map, &state) + ) + }); + + offset += console::measure_text_width(&self.get_spacer_left( + &output_left, + &output_center, + state.cols, + )); + + offset += + self.process_widget_click(click_pos, &self.center_parts, &widget_map, &state, offset); + + if click_pos <= offset { return; } @@ -131,34 +168,25 @@ impl ModuleConfig { ) }); - let widget_spacer_len = console::measure_text_width(&self.get_spacer( - &" ".repeat(left_len), + offset += console::measure_text_width(&self.get_spacer_right( &output_right, + &output_center, state.cols, )); - let widget_string_right = self - .right_parts - .iter() - .fold(String::new(), |a, b| a + &b.content); - - self.process_widget_click( - click_pos, - widget_string_right, - widget_map, - &state, - left_len + widget_spacer_len, - ); + self.process_widget_click(click_pos, &self.right_parts, &widget_map, &state, offset); } fn process_widget_click( &self, click_pos: usize, - widget_string: String, - widget_map: BTreeMap<String, Arc<dyn Widget>>, + widgets: &[FormattedPart], + widget_map: &BTreeMap<String, Arc<dyn Widget>>, state: &ZellijState, offset: usize, ) -> usize { + let widget_string = widgets.iter().fold(String::new(), |a, b| a + &b.content); + let mut rendered_output = widget_string.clone(); let tokens: Vec<String> = widget_map.keys().map(|k| format!("{{{k}}}")).collect(); @@ -194,7 +222,7 @@ impl ModuleConfig { continue; } - wid.process_click(state, click_pos - pos + offset); + wid.process_click(state, click_pos - (pos + offset)); } console::measure_text_width(&rendered_output) @@ -212,6 +240,16 @@ impl ModuleConfig { ) }); + let output_center = self + .center_parts + .iter_mut() + .fold("".to_owned(), |acc, part| { + format!( + "{acc}{}", + part.format_string_with_widgets(&widget_map, &state) + ) + }); + let output_right = self .right_parts .iter_mut() @@ -234,30 +272,50 @@ impl ModuleConfig { } return format!( - "{}{}{}{}{}", + "{}{}{}{}{}{}{}", border_top, output_left, - self.get_spacer(&output_left, &output_right, state.cols), + self.get_spacer_left(&output_left, &output_center, state.cols), + output_center, + self.get_spacer_right(&output_right, &output_center, state.cols), output_right, border_bottom, ); } format!( - "{}{}{}", + "{}{}{}{}{}", output_left, - self.get_spacer(&output_left, &output_right, state.cols), + self.get_spacer_left(&output_left, &output_center, state.cols), + output_center, + self.get_spacer_right(&output_right, &output_center, state.cols), output_right, ) } - fn get_spacer(&self, output_left: &str, output_right: &str, cols: usize) -> String { - let text_count = - console::measure_text_width(output_left) + console::measure_text_width(output_right); + fn get_spacer_left(&self, output_left: &str, output_center: &str, cols: usize) -> String { + let text_count = console::measure_text_width(output_left) + + (console::measure_text_width(output_center) as f32 / 2.0).floor() as usize; + + let center_pos = (cols as f32 / 2.0).floor() as usize; + + // verify we are able to count the difference, since zellij sometimes drops a col + // count of 0 on tab creation + let space_count = center_pos.saturating_sub(text_count); + + eprintln!("space_count: {:?}", space_count); + self.format_space.format_string(&" ".repeat(space_count)) + } + + fn get_spacer_right(&self, output_right: &str, output_center: &str, cols: usize) -> String { + let text_count = console::measure_text_width(output_right) + + (console::measure_text_width(output_center) as f32 / 2.0).ceil() as usize; + + let center_pos = (cols as f32 / 2.0).ceil() as usize; // verify we are able to count the difference, since zellij sometimes drops a col // count of 0 on tab creation - let space_count = cols.saturating_sub(text_count); + let space_count = center_pos.saturating_sub(text_count); self.format_space.format_string(&" ".repeat(space_count)) } |
