zellij/default-tiles/status-bar/src/main.rs
categorille c74e2ef273
feat(input): new keybindings and persistent mode as default (#181)
* added some comments in the input module

* InputHandler now has InputState instead of separate InputMode and permanent bool

* keybinds are now associated with a Vec<Action> instead of a single Action

* removing "persistent" modes alltogether to reimplement the feature, help bar broken

* locked command mode by default, fixes #161, help bar still broken

* status bar fixed, still a few improvements/bugs to go

* better shortcut help bar contents

* fixed last bits and i think we are good

* modified tests according to new controls, not working

* Revert "modified tests according to new controls, not working"

This reverts commit f2d9421ff0169feb83dbd9b246e59b9244cafc16.

* basic.rs tests now pass, not the rest

* close_pane.rs tests pass, but very slowly?

* compatibility.rs tests pass, very slowly as well

* {layout, {move_focus_*}}.rs all working mildly slowly

* {resize_*}.rs should all work but very slowly and funky, see PR comments

* {tabs,toggle_fullscreen}.rs pass. Test performance issue yet to be fixed

* tests now work, with a hack :| ready for merge

* rustfmt + deleted references to InputState and mode persistency
2021-02-12 14:55:22 +01:00

261 lines
10 KiB
Rust

use colored::*;
use std::fmt::{Display, Error, Formatter};
use zellij_tile::*;
// for more of these, copy paste from: https://en.wikipedia.org/wiki/Box-drawing_character
static ARROW_SEPARATOR: &str = "";
static MORE_MSG: &str = " ... ";
#[derive(Default)]
struct State {}
register_tile!(State);
struct LinePart {
part: String,
len: usize,
}
impl Display for LinePart {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}", self.part)
}
}
fn prefix(help: &Help) -> LinePart {
let prefix_text = " Zellij ";
let part = match &help.mode {
InputMode::Command => {
let prefix = prefix_text.bold().white().on_black();
let separator = ARROW_SEPARATOR.black().on_magenta();
format!("{}{}", prefix, separator)
}
InputMode::Normal => {
let prefix = prefix_text.bold().white().on_black();
let separator = ARROW_SEPARATOR.black().on_green();
format!("{}{}", prefix, separator)
}
_ => {
let prefix = prefix_text.bold().white().on_black();
let separator = ARROW_SEPARATOR.black().on_magenta();
format!("{}{}", prefix, separator)
}
};
let len = prefix_text.chars().count() + ARROW_SEPARATOR.chars().count();
LinePart { part, len }
}
fn key_path(help: &Help) -> LinePart {
let superkey_text = "<Ctrl-g> ";
let (part, len) = match &help.mode {
InputMode::Command => {
let key_path = superkey_text.bold().on_magenta();
let first_separator = ARROW_SEPARATOR.magenta().on_black();
let len = superkey_text.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ ARROW_SEPARATOR.chars().count();
(format!("{}{}", key_path, first_separator), len)
}
InputMode::Resize => {
let mode_shortcut_text = "r ";
let superkey = superkey_text.bold().on_magenta();
let first_superkey_separator = ARROW_SEPARATOR.magenta().on_black();
let second_superkey_separator = ARROW_SEPARATOR.black().on_magenta();
let mode_shortcut = mode_shortcut_text.white().bold().on_magenta();
let mode_shortcut_separator = ARROW_SEPARATOR.magenta().on_black();
let len = superkey_text.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ mode_shortcut_text.chars().count()
+ ARROW_SEPARATOR.chars().count();
(
format!(
"{}{}{}{}{}",
superkey,
first_superkey_separator,
second_superkey_separator,
mode_shortcut,
mode_shortcut_separator
),
len,
)
}
InputMode::Pane => {
let mode_shortcut_text = "p ";
let superkey = superkey_text.bold().on_magenta();
let first_superkey_separator = ARROW_SEPARATOR.magenta().on_black();
let second_superkey_separator = ARROW_SEPARATOR.black().on_magenta();
let mode_shortcut = mode_shortcut_text.white().bold().on_magenta();
let mode_shortcut_separator = ARROW_SEPARATOR.magenta().on_black();
let len = superkey_text.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ mode_shortcut_text.chars().count()
+ ARROW_SEPARATOR.chars().count();
(
format!(
"{}{}{}{}{}",
superkey,
first_superkey_separator,
second_superkey_separator,
mode_shortcut,
mode_shortcut_separator
),
len,
)
}
InputMode::Tab => {
let mode_shortcut_text = "t ";
let superkey = superkey_text.bold().on_magenta();
let first_superkey_separator = ARROW_SEPARATOR.magenta().on_black();
let second_superkey_separator = ARROW_SEPARATOR.black().on_magenta();
let mode_shortcut = mode_shortcut_text.white().bold().on_magenta();
let mode_shortcut_separator = ARROW_SEPARATOR.magenta().on_black();
let len = superkey_text.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ mode_shortcut_text.chars().count()
+ ARROW_SEPARATOR.chars().count();
(
format!(
"{}{}{}{}{}",
superkey,
first_superkey_separator,
second_superkey_separator,
mode_shortcut,
mode_shortcut_separator
),
len,
)
}
InputMode::Scroll => {
let mode_shortcut_text = "s ";
let superkey = superkey_text.bold().on_magenta();
let first_superkey_separator = ARROW_SEPARATOR.magenta().on_black();
let second_superkey_separator = ARROW_SEPARATOR.black().on_magenta();
let mode_shortcut = mode_shortcut_text.white().bold().on_magenta();
let mode_shortcut_separator = ARROW_SEPARATOR.magenta().on_black();
let len = superkey_text.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ ARROW_SEPARATOR.chars().count()
+ mode_shortcut_text.chars().count()
+ ARROW_SEPARATOR.chars().count();
(
format!(
"{}{}{}{}{}",
superkey,
first_superkey_separator,
second_superkey_separator,
mode_shortcut,
mode_shortcut_separator
),
len,
)
}
InputMode::Normal | _ => {
let key_path = superkey_text.on_green();
let separator = ARROW_SEPARATOR.green().on_black();
(
format!("{}{}", key_path, separator),
superkey_text.chars().count() + ARROW_SEPARATOR.chars().count(),
)
}
};
LinePart { part, len }
}
fn keybinds(help: &Help, max_width: usize) -> LinePart {
let mut keybinds = String::new();
let mut len = 0;
let full_keybinds_len =
help.keybinds
.iter()
.enumerate()
.fold(0, |acc, (i, (shortcut, description))| {
let shortcut_length = shortcut.chars().count() + 3; // 2 for <>'s around shortcut, 1 for the space
let description_length = description.chars().count() + 2;
let (_separator, separator_len) = if i > 0 { (" / ", 3) } else { ("", 0) };
acc + shortcut_length + description_length + separator_len
});
if full_keybinds_len < max_width {
for (i, (shortcut, description)) in help.keybinds.iter().enumerate() {
let separator = if i > 0 { " / " } else { "" };
let shortcut_len = shortcut.chars().count();
let shortcut = match help.mode {
InputMode::Normal => shortcut.cyan(),
_ => shortcut.white().bold(),
};
keybinds = format!("{}{}<{}> {}", keybinds, separator, shortcut, description);
len += shortcut_len + separator.chars().count();
}
} else {
for (i, (shortcut, description)) in help.keybinds.iter().enumerate() {
let description_first_word = description.split(' ').next().unwrap_or("");
let current_length = keybinds.chars().count();
let shortcut_length = shortcut.chars().count() + 3; // 2 for <>'s around shortcut, 1 for the space
let description_first_word_length = description_first_word.chars().count();
let (separator, separator_len) = if i > 0 { (" / ", 3) } else { ("", 0) };
let shortcut = match help.mode {
InputMode::Normal => shortcut.cyan(),
_ => shortcut.white().bold(),
};
if current_length
+ shortcut_length
+ description_first_word_length
+ separator_len
+ MORE_MSG.chars().count()
< max_width
{
keybinds = format!(
"{}{}<{}> {}",
keybinds, separator, shortcut, description_first_word
);
len += shortcut_length + description_first_word_length + separator_len;
} else if current_length + shortcut_length + separator_len + MORE_MSG.chars().count()
< max_width
{
keybinds = format!("{}{}<{}>", keybinds, separator, shortcut);
len += shortcut_length + separator_len;
} else {
keybinds = format!("{}{}", keybinds, MORE_MSG);
len += MORE_MSG.chars().count();
break;
}
}
}
LinePart {
part: keybinds,
len,
}
}
impl ZellijTile for State {
fn init(&mut self) {
set_selectable(false);
set_invisible_borders(true);
set_max_height(1);
}
fn draw(&mut self, _rows: usize, cols: usize) {
let help = get_help();
let line_prefix = prefix(&help);
let key_path = key_path(&help);
let line_len_before_keybinds = line_prefix.len + key_path.len;
let status_bar = if line_len_before_keybinds + MORE_MSG.chars().count() < cols {
let keybinds = keybinds(&help, cols - line_len_before_keybinds);
let keybinds = keybinds.part.cyan().on_black();
format!("{}{}{}", line_prefix, key_path, keybinds)
} else if line_len_before_keybinds < cols {
format!("{}{}", line_prefix, key_path)
} else if line_prefix.len < cols {
format!("{}", line_prefix)
} else {
// sorry, too small :(
format!("")
};
// 40m is black background, 0K is so that it fills the rest of the line,
// I could not find a way to do this with colored and did not want to have to
// manually fill the line with spaces to achieve the same
println!("{}\u{1b}[40m\u{1b}[0K", status_bar);
}
}