Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format the InputMode with fixed width in compact-bar #3857

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 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 default-plugins/compact-bar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
26 changes: 24 additions & 2 deletions default-plugins/compact-bar/src/line.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ansi_term::ANSIStrings;
use strum::IntoEnumIterator;
use unicode_width::UnicodeWidthStr;

use crate::{LinePart, ARROW_SEPARATOR};
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
)
}