From 26a6c2a52831ffa4dcacc409c7686e06f948310b Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 1 Aug 2019 19:41:20 +0100 Subject: [PATCH] view: Add @thumbs-contrast option Add an option to surround the hint character with square brackets (for example `[a]` rather than `a`). This is the equivalent to the following `tmux-fingers` configuration option [1] and is useful to make the hint more visible: ``` set -g @fingers-compact-hints 0 ``` [1] - https://github.com/Morantron/tmux-fingers#fingers-compact-hints Signed-off-by: James O. D. Hunt --- README.md | 13 +++++++++++ src/main.rs | 8 +++++++ src/view.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++---- tmux-thumbs.sh | 1 + 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4abac18..dcdbd3c 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ NOTE: for changes to take effect, you'll need to source again your `.tmux.conf` * [@thumbs-hint-bg-color](#thumbs-hint-bg-color) * [@thumbs-hint-fg-color](#thumbs-hint-fg-color) * [@thumbs-select-fg-color](#thumbs-select-fg-color) +* [@thumbs-contrast](#thumbs-contrast) ### @thumbs-key @@ -234,6 +235,18 @@ For example: set -g @thumbs-select-fg-color red ``` +### @thumbs-contrast + +`default: 0` + +Displays hint character in square brackets for extra visibility. + +For example: + +``` +set -g @thumbs-contrast 1 +``` + #### Colors This is the list of available colors: diff --git a/src/main.rs b/src/main.rs index cff32cf..1b182d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,6 +105,12 @@ fn app_args<'a>() -> clap::ArgMatches<'a> { .takes_value(true) .multiple(true), ) + .arg( + Arg::with_name("contrast") + .help("Put square brackets around hint for visibility") + .long("contrast") + .short("c"), + ) .get_matches(); } @@ -114,6 +120,7 @@ fn main() { let position = args.value_of("position").unwrap(); let reverse = args.is_present("reverse"); let unique = args.is_present("unique"); + let contrast = args.is_present("contrast"); let regexp = if let Some(items) = args.values_of("regexp") { items.collect::>() } else { @@ -146,6 +153,7 @@ fn main() { &mut state, reverse, unique, + contrast, position, select_foreground_color, foreground_color, diff --git a/src/view.rs b/src/view.rs index 95ebd4f..9836f75 100644 --- a/src/view.rs +++ b/src/view.rs @@ -9,6 +9,7 @@ pub struct View<'a> { skip: usize, reverse: bool, unique: bool, + contrast: bool, position: &'a str, select_foreground_color: Color, foreground_color: Color, @@ -22,6 +23,7 @@ impl<'a> View<'a> { state: &'a mut state::State<'a>, reverse: bool, unique: bool, + contrast: bool, position: &'a str, select_foreground_color: Color, foreground_color: Color, @@ -34,6 +36,7 @@ impl<'a> View<'a> { skip: 0, reverse: reverse, unique: unique, + contrast: contrast, position: position, select_foreground_color: select_foreground_color, foreground_color: foreground_color, @@ -55,6 +58,16 @@ impl<'a> View<'a> { } } + fn make_hint_text(&self, hint: &str) -> String { + let text = if self.contrast { + format!("[{}]", hint).to_string() + } else { + hint.to_string() + }; + + text + } + pub fn present(&mut self) -> Option<(String, bool)> { let mut rustbox = match RustBox::init(Default::default()) { Result::Ok(v) => v, @@ -83,13 +96,15 @@ impl<'a> View<'a> { let clean = line.trim_end_matches(|c: char| c.is_whitespace()); if clean.len() > 0 { + let text = self.make_hint_text(line); + rustbox.print( 0, index, rustbox::RB_NORMAL, Color::White, Color::Black, - line, + &text, ); } } @@ -108,6 +123,7 @@ impl<'a> View<'a> { let prefix = &line[0..mat.x as usize]; let extra = prefix.len() - prefix.chars().count(); let offset = (mat.x as usize) - extra; + let text = self.make_hint_text(mat.text); rustbox.print( offset, @@ -115,23 +131,25 @@ impl<'a> View<'a> { rustbox::RB_NORMAL, selected_color, self.background_color, - mat.text, + &text, ); if let Some(ref hint) = mat.hint { let extra_position = if self.position == "left" { 0 } else { - mat.text.len() - mat.hint.clone().unwrap().len() + text.len() - mat.hint.clone().unwrap().len() }; + let text = self.make_hint_text(hint.as_str()); + rustbox.print( offset + extra_position, mat.y as usize, rustbox::RB_BOLD, self.hint_foreground_color, self.hint_background_color, - hint.as_str(), + &text, ); } } @@ -187,3 +205,39 @@ impl<'a> View<'a> { None } } + +#[cfg(test)] +mod tests { + use super::*; + + fn split(output: &str) -> Vec<&str> { + output.split("\n").collect::>() + } + + #[test] + fn hint_text() { + let lines = split("lorem 127.0.0.1 lorem"); + let custom = [].to_vec(); + let mut state = state::State::new(&lines, "abcd", &custom); + let mut view = View { + state: &mut state, + skip: 0, + reverse: false, + unique: false, + contrast: false, + position: &"", + select_foreground_color: rustbox::Color::Default, + foreground_color: rustbox::Color::Default, + background_color: rustbox::Color::Default, + hint_background_color: rustbox::Color::Default, + hint_foreground_color: rustbox::Color::Default, + }; + + let result = view.make_hint_text("a"); + assert_eq!(result, "a".to_string()); + + view.contrast = true; + let result = view.make_hint_text("a"); + assert_eq!(result, "[a]".to_string()); + } +} diff --git a/tmux-thumbs.sh b/tmux-thumbs.sh index f066639..42dc558 100755 --- a/tmux-thumbs.sh +++ b/tmux-thumbs.sh @@ -42,6 +42,7 @@ PARAMS[8]=$(option select-fg-color) PARAMS[9]=$(option command) PARAMS[10]=$(option upcase-command) PARAMS[11]=$(multi regexp) +PARAMS[12]=$(boolean contrast) CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" TARGET_RELEASE="/target/release/"