diff options
author | Robert Walter <26892280+RobWalt@users.noreply.github.com> | 2022-02-08 03:01:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-08 11:01:54 +0900 |
commit | 341a9f4b9153cdae50bc48343b4a29ce728f280f (patch) | |
tree | 5de0177983f1e8ceb4f126775164d52f503f1485 | |
parent | ff8616280ba0e3efd8dc661263c07be930ee2a15 (diff) |
Solve Issue #1010 (#1045)
* solve issue #1010 and made code easily extensible
* Make description bold to fit + added minimal documentation
* chore(assets): update plugins
* Only change hints on reneamPane Mode, old hints were fine in every other case
* fixing some wording to get more green e2e tests
* add the opt version of the plugin
Co-authored-by: jaeheonji <atx6419@gmail.com>
-rwxr-xr-x | assets/plugins/status-bar.wasm | bin | 539935 -> 648453 bytes | |||
-rw-r--r-- | default-plugins/status-bar/src/second_line.rs | 195 |
2 files changed, 137 insertions, 58 deletions
diff --git a/assets/plugins/status-bar.wasm b/assets/plugins/status-bar.wasm Binary files differindex 64f9920fd..03a0cad8d 100755 --- a/assets/plugins/status-bar.wasm +++ b/assets/plugins/status-bar.wasm diff --git a/default-plugins/status-bar/src/second_line.rs b/default-plugins/status-bar/src/second_line.rs index 13e847b99..abd2f8c3f 100644 --- a/default-plugins/status-bar/src/second_line.rs +++ b/default-plugins/status-bar/src/second_line.rs @@ -10,6 +10,19 @@ use crate::{ LinePart, MORE_MSG, }; +#[derive(Clone, Copy)] +enum StatusBarTextColor { + White, + Green, + Orange, +} + +#[derive(Clone, Copy)] +enum StatusBarTextBoldness { + Bold, + NotBold, +} + fn full_length_shortcut( is_first_shortcut: bool, letter: &str, @@ -100,55 +113,120 @@ fn locked_interface_indication(palette: Palette) -> LinePart { } } -fn select_pane_shortcut(is_first_shortcut: bool, palette: Palette) -> LinePart { - let shortcut = "ENTER"; - let description = "Select pane"; - let separator = if is_first_shortcut { " " } else { " / " }; +fn show_extra_hints( + palette: Palette, + text_with_style: Vec<(&str, StatusBarTextColor, StatusBarTextBoldness)>, +) -> LinePart { + use StatusBarTextBoldness::*; + use StatusBarTextColor::*; + // get the colors let white_color = match palette.white { PaletteColor::Rgb((r, g, b)) => RGB(r, g, b), PaletteColor::EightBit(color) => Fixed(color), }; + let green_color = match palette.green { + PaletteColor::Rgb((r, g, b)) => RGB(r, g, b), + PaletteColor::EightBit(color) => Fixed(color), + }; let orange_color = match palette.orange { PaletteColor::Rgb((r, g, b)) => RGB(r, g, b), PaletteColor::EightBit(color) => Fixed(color), }; - let separator = Style::new().fg(white_color).paint(separator); - let shortcut_len = shortcut.chars().count() + 3; // 2 for <>'s around shortcut, 1 for the space - let shortcut_left_separator = Style::new().fg(white_color).paint("<"); - let shortcut = Style::new().fg(orange_color).bold().paint(shortcut); - let shortcut_right_separator = Style::new().fg(white_color).paint("> "); - let description_len = description.chars().count(); - let description = Style::new().fg(white_color).bold().paint(description); - let len = shortcut_len + description_len + separator.chars().count(); + // calculate length of tipp + let len = text_with_style + .iter() + .fold(0, |len_sum, (text, _, _)| len_sum + text.chars().count()); + // apply the styles defined above + let styled_text = text_with_style + .into_iter() + .map(|(text, color, is_bold)| { + let color = match color { + White => white_color, + Green => green_color, + Orange => orange_color, + }; + match is_bold { + Bold => Style::new().fg(color).bold().paint(text), + NotBold => Style::new().fg(color).paint(text), + } + }) + .collect::<Vec<_>>(); LinePart { - part: ANSIStrings(&[ - separator, - shortcut_left_separator, - shortcut, - shortcut_right_separator, - description, - ]) - .to_string(), + part: ANSIStrings(&styled_text[..]).to_string(), len, } } +/// Creates hints for usage of Pane Mode +fn confirm_pane_selection(palette: Palette) -> LinePart { + use StatusBarTextBoldness::*; + use StatusBarTextColor::*; + let text_with_style = [ + (" / ", White, NotBold), + ("<ENTER>", Green, Bold), + (" Select pane", White, Bold), + ]; + show_extra_hints(palette, text_with_style.to_vec()) +} + +/// Creates hints for usage of Rename Mode in Pane Mode +fn select_pane_shortcut(palette: Palette) -> LinePart { + use StatusBarTextBoldness::*; + use StatusBarTextColor::*; + let text_with_style = [ + (" / ", White, NotBold), + ("Alt", Orange, Bold), + (" + ", White, NotBold), + ("<", Green, Bold), + ("[]", Green, Bold), + (" or ", White, NotBold), + ("hjkl", Green, Bold), + (">", Green, Bold), + (" Select pane", White, Bold), + ]; + show_extra_hints(palette, text_with_style.to_vec()) +} + +fn full_shortcut_list_nonstandard_mode( + extra_hint_producing_function: fn(Palette) -> LinePart, +) -> impl FnOnce(&ModeInfo) -> LinePart { + move |help| { + let mut line_part = LinePart::default(); + for (i, (letter, description)) in help.keybinds.iter().enumerate() { + let shortcut = full_length_shortcut(i == 0, letter, description, help.palette); + line_part.len += shortcut.len; + line_part.part = format!("{}{}", line_part.part, shortcut,); + } + let select_pane_shortcut = extra_hint_producing_function(help.palette); + line_part.len += select_pane_shortcut.len; + line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); + line_part + } +} + fn full_shortcut_list(help: &ModeInfo, tip: TipFn) -> LinePart { match help.mode { InputMode::Normal => tip(help.palette), InputMode::Locked => locked_interface_indication(help.palette), - _ => { - let mut line_part = LinePart::default(); - for (i, (letter, description)) in help.keybinds.iter().enumerate() { - let shortcut = full_length_shortcut(i == 0, letter, description, help.palette); - line_part.len += shortcut.len; - line_part.part = format!("{}{}", line_part.part, shortcut,); - } - let select_pane_shortcut = select_pane_shortcut(help.keybinds.is_empty(), help.palette); - line_part.len += select_pane_shortcut.len; - line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); - line_part + InputMode::RenamePane => full_shortcut_list_nonstandard_mode(select_pane_shortcut)(help), + _ => full_shortcut_list_nonstandard_mode(confirm_pane_selection)(help), + } +} + +fn shortened_shortcut_list_nonstandard_mode( + extra_hint_producing_function: fn(Palette) -> LinePart, +) -> impl FnOnce(&ModeInfo) -> LinePart { + move |help| { + let mut line_part = LinePart::default(); + for (i, (letter, description)) in help.keybinds.iter().enumerate() { + let shortcut = first_word_shortcut(i == 0, letter, description, help.palette); + line_part.len += shortcut.len; + line_part.part = format!("{}{}", line_part.part, shortcut,); } + let select_pane_shortcut = extra_hint_producing_function(help.palette); + line_part.len += select_pane_shortcut.len; + line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); + line_part } } @@ -156,18 +234,35 @@ fn shortened_shortcut_list(help: &ModeInfo, tip: TipFn) -> LinePart { match help.mode { InputMode::Normal => tip(help.palette), InputMode::Locked => locked_interface_indication(help.palette), - _ => { - let mut line_part = LinePart::default(); - for (i, (letter, description)) in help.keybinds.iter().enumerate() { - let shortcut = first_word_shortcut(i == 0, letter, description, help.palette); - line_part.len += shortcut.len; - line_part.part = format!("{}{}", line_part.part, shortcut,); + InputMode::RenamePane => { + shortened_shortcut_list_nonstandard_mode(select_pane_shortcut)(help) + } + _ => shortened_shortcut_list_nonstandard_mode(confirm_pane_selection)(help), + } +} + +fn best_effort_shortcut_list_nonstandard_mode( + extra_hint_producing_function: fn(Palette) -> LinePart, +) -> impl FnOnce(&ModeInfo, usize) -> LinePart { + move |help, max_len| { + let mut line_part = LinePart::default(); + for (i, (letter, description)) in help.keybinds.iter().enumerate() { + let shortcut = first_word_shortcut(i == 0, letter, description, help.palette); + if line_part.len + shortcut.len + MORE_MSG.chars().count() > max_len { + // TODO: better + line_part.part = format!("{}{}", line_part.part, MORE_MSG); + line_part.len += MORE_MSG.chars().count(); + break; } - let select_pane_shortcut = select_pane_shortcut(help.keybinds.is_empty(), help.palette); + line_part.len += shortcut.len; + line_part.part = format!("{}{}", line_part.part, shortcut); + } + let select_pane_shortcut = extra_hint_producing_function(help.palette); + if line_part.len + select_pane_shortcut.len <= max_len { line_part.len += select_pane_shortcut.len; line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); - line_part } + line_part } } @@ -189,26 +284,10 @@ fn best_effort_shortcut_list(help: &ModeInfo, tip: TipFn, max_len: usize) -> Lin LinePart::default() } } - _ => { - let mut line_part = LinePart::default(); - for (i, (letter, description)) in help.keybinds.iter().enumerate() { - let shortcut = first_word_shortcut(i == 0, letter, description, help.palette); - if line_part.len + shortcut.len + MORE_MSG.chars().count() > max_len { - // TODO: better - line_part.part = format!("{}{}", line_part.part, MORE_MSG); - line_part.len += MORE_MSG.chars().count(); - break; - } - line_part.len += shortcut.len; - line_part.part = format!("{}{}", line_part.part, shortcut); - } - let select_pane_shortcut = select_pane_shortcut(help.keybinds.is_empty(), help.palette); - if line_part.len + select_pane_shortcut.len <= max_len { - line_part.len += select_pane_shortcut.len; - line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); - } - line_part + InputMode::RenamePane => { + best_effort_shortcut_list_nonstandard_mode(select_pane_shortcut)(help, max_len) } + _ => best_effort_shortcut_list_nonstandard_mode(confirm_pane_selection)(help, max_len), } } |