Skip to content

Commit

Permalink
feat: implement format precedence to hide overflown parts
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
dj95 committed May 25, 2024
1 parent c186c82 commit 63b13dd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
18 changes: 14 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
9 changes: 5 additions & 4 deletions plugin-dev-workspace.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -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]  "
Expand Down
84 changes: 83 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 63b13dd

Please sign in to comment.