diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2024-05-25 16:19:48 +0200 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2024-05-25 16:19:48 +0200 |
| commit | 63b13dd6f0e73d9ccba0614ccc2fbda1708c5577 (patch) | |
| tree | 0483ab6deb224ea91110286ab13a3c7a07a7687c | |
| parent | feat(frames): show frames when renaming pane or searching (diff) | |
| download | sl0pstatus-63b13dd6f0e73d9ccba0614ccc2fbda1708c5577.tar.gz sl0pstatus-63b13dd6f0e73d9ccba0614ccc2fbda1708c5577.tar.xz sl0pstatus-63b13dd6f0e73d9ccba0614ccc2fbda1708c5577.zip | |
feat: implement format precedence to hide overflown parts
Fixes #58
| -rw-r--r-- | Cargo.lock | 18 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | plugin-dev-workspace.kdl | 9 | ||||
| -rw-r--r-- | src/config.rs | 84 |
4 files changed, 103 insertions, 9 deletions
@@ -773,7 +773,7 @@ dependencies = [ "clap 4.5.4", "criterion-plot", "is-terminal", - "itertools", + "itertools 0.10.5", "num-traits", "once_cell", "oorandom", @@ -792,7 +792,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", - "itertools", + "itertools 0.10.5", ] [[package]] @@ -1777,6 +1777,15 @@ dependencies = [ ] [[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] name = "itoa" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2601,7 +2610,7 @@ checksum = "119533552c9a7ffacc21e099c24a0ac8bb19c2a2a3f363de84cd9b844feab270" dependencies = [ "bytes 1.6.0", "heck 0.4.1", - "itertools", + "itertools 0.10.5", "lazy_static", "log", "multimap", @@ -2622,7 +2631,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "syn 1.0.109", @@ -4285,6 +4294,7 @@ dependencies = [ "chrono-tz", "console", "criterion", + "itertools 0.13.0", "kdl", "lazy_static", "regex", @@ -30,6 +30,7 @@ tracing-subscriber = "0.3.18" tracing = "0.1.40" kdl = "4.6.0" rstest = "0.19.0" +itertools = "0.13.0" [dev-dependencies] criterion = { version = "0.5.1", default-features = false, features = [ diff --git a/plugin-dev-workspace.kdl b/plugin-dev-workspace.kdl index 06e1947..108b97d 100644 --- a/plugin-dev-workspace.kdl +++ b/plugin-dev-workspace.kdl @@ -5,10 +5,11 @@ 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}" - format_center "{command_0} {command_1} {command_git_branch} {command_3}" - format_right "{notifications}{swap_layout}{datetime}" - format_space "#[bg=#181825]" + format_left "{mode}#[fg=#89B4FA,bg=#181825,bold] {session} {tabs}" + format_center "{command_0} {command_1} {command_git_branch} {command_3}" + format_right "{notifications}{swap_layout}{datetime}" + format_space "#[bg=#181825]" + format_precedence "lcq" notification_format_unread "#[fg=#89B4FA,bg=#181825,blink] #[fg=#89B4FA,bg=#181825] {message} " notification_format_no_notifications "#[fg=#89B4FA,bg=#181825,dim] " diff --git a/src/config.rs b/src/config.rs index ae00e45..a58007c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ -use std::{collections::BTreeMap, sync::Arc}; +use std::{collections::BTreeMap, str::FromStr, sync::Arc}; +use itertools::Itertools; use regex::Regex; use zellij_tile::prelude::*; @@ -24,6 +25,26 @@ pub struct ZellijState { pub cache_mask: u8, } +#[derive(Clone, Debug, Ord, Eq, PartialEq, PartialOrd, Copy)] +pub enum Part { + Left, + Center, + Right, +} + +impl FromStr for Part { + fn from_str(part: &str) -> Result<Self> { + match part { + "l" => Ok(Part::Left), + "c" => Ok(Part::Center), + "r" => Ok(Part::Right), + _ => anyhow::bail!("Invalid part: {}", part), + } + } + + type Err = anyhow::Error; +} + pub enum UpdateEventMask { Always = 0b10000000, Mode = 0b00000001, @@ -57,6 +78,7 @@ pub struct ModuleConfig { pub format_space: FormattedPart, pub hide_frame_for_single_pane: bool, pub border: BorderConfig, + pub format_precedence: Vec<Part>, } impl ModuleConfig { @@ -86,6 +108,14 @@ impl ModuleConfig { None => "", }; + let format_precedence = match config.get("format_precedence") { + Some(conf) => conf + .chars() + .map(|c| Part::from_str(&c.to_string()).expect("Invalid part")) + .collect(), + None => vec![Part::Left, Part::Center, Part::Right], + }; + let border_config = match parse_border_config(config.clone()) { Some(bc) => bc, None => BorderConfig::default(), @@ -101,6 +131,7 @@ impl ModuleConfig { format_space: FormattedPart::from_format_string(format_space_config), hide_frame_for_single_pane, border: border_config, + format_precedence, } } @@ -275,6 +306,9 @@ impl ModuleConfig { ) }); + let (output_left, output_center, output_right) = + self.trim_output(&output_left, &output_center, &output_right, state.cols); + if self.border.enabled { let mut border_top = "".to_owned(); if self.border.enabled && self.border.position == BorderPosition::Top { @@ -328,6 +362,54 @@ impl ModuleConfig { ) } + fn trim_output( + &self, + output_left: &str, + output_center: &str, + output_right: &str, + cols: usize, + ) -> (String, String, String) { + let center_pos = (cols as f32 / 2.0).floor() as usize; + + let mut output = BTreeMap::from([ + (Part::Left, output_left.to_owned()), + (Part::Center, output_center.to_owned()), + (Part::Right, output_right.to_owned()), + ]); + + let combinations = [ + (self.format_precedence[2], self.format_precedence[1]), + (self.format_precedence[1], self.format_precedence[0]), + (self.format_precedence[2], self.format_precedence[0]), + ]; + + for win in combinations.iter() { + let (a, b) = win; + + let part_a = output.get(a).unwrap(); + let part_b = output.get(b).unwrap(); + + let a_count = console::measure_text_width(part_a); + let b_count = console::measure_text_width(part_b); + + let overlap = match (a, b) { + (Part::Left, Part::Right) => a_count + b_count > cols, + (Part::Right, Part::Left) => a_count + b_count > cols, + (Part::Left, Part::Center) => a_count > center_pos - (b_count / 2), + (Part::Center, Part::Left) => b_count > center_pos - (a_count / 2), + (Part::Right, Part::Center) => a_count > center_pos - (b_count / 2), + (Part::Center, Part::Right) => b_count > center_pos - (a_count / 2), + _ => false, + }; + + if overlap { + output.insert(*a, "".to_owned()); + } + } + + output.values().cloned().collect_tuple().unwrap() + } + #[tracing::instrument(skip_all)] fn get_spacer_left(&self, output_left: &str, output_center: &str, cols: usize) -> String { let text_count = console::measure_text_width(output_left) |
