diff --git a/Cargo.lock b/Cargo.lock index 6a8109eeaa..2c08a405b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -642,6 +642,7 @@ version = "0.1.0" dependencies = [ "ansi_term", "colored", + "strum", "unicode-width", "zellij-tile", "zellij-tile-utils", diff --git a/default-plugins/compact-bar/Cargo.toml b/default-plugins/compact-bar/Cargo.toml index 6f45e2c405..07e9ac0ad6 100644 --- a/default-plugins/compact-bar/Cargo.toml +++ b/default-plugins/compact-bar/Cargo.toml @@ -8,6 +8,7 @@ license = "MIT" [dependencies] colored = "2" ansi_term = "0.12" +strum = "0.20.0" unicode-width = "0.1.8" zellij-tile = { path = "../../zellij-tile" } zellij-tile-utils = { path = "../../zellij-tile-utils" } diff --git a/default-plugins/compact-bar/src/line.rs b/default-plugins/compact-bar/src/line.rs index fa5fd3e932..8e0d8497e8 100644 --- a/default-plugins/compact-bar/src/line.rs +++ b/default-plugins/compact-bar/src/line.rs @@ -1,4 +1,5 @@ use ansi_term::ANSIStrings; +use strum::IntoEnumIterator; use unicode_width::UnicodeWidthStr; use crate::{LinePart, ARROW_SEPARATOR}; @@ -214,8 +215,7 @@ fn tab_line_prefix( }) } } - let mode_part = format!("{:?}", mode).to_uppercase(); - let mode_part_padded = format!(" {} ", mode_part); + let mode_part_padded = format_input_mode(mode); let mode_part_len = mode_part_padded.width(); let mode_part_styled_text = if mode == InputMode::Locked { style!(locked_mode_color, bg_color) @@ -383,3 +383,25 @@ fn swap_layout_status( None => None, } } + +// format the given input mode to be displayed in the tab line +// the mode is centered in the fixe-width space available +fn format_input_mode(mode: InputMode) -> String { + let max_width = InputMode::iter() + .map(|variant| format!("{:?}", variant).len()) + .max() + .map(|max_len| max_len + 2) // add padding(left+right) for the longest mode + .unwrap(); + + let mode_part = format!("{:?}", mode).to_uppercase(); + + let total_padding = max_width - mode_part.len(); + let left_padding = total_padding / 2; + let right_padding = total_padding - left_padding; + + format!( + "{left}{mode_part}{right}", + left = " ".repeat(left_padding), + right = " ".repeat(right_padding) + ) +}