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 <jamesodhunt@gmail.com>
This commit is contained in:
James O. D. Hunt 2019-08-01 19:41:20 +01:00
parent f1123d6059
commit 26a6c2a528
4 changed files with 80 additions and 4 deletions

View File

@ -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-bg-color](#thumbs-hint-bg-color)
* [@thumbs-hint-fg-color](#thumbs-hint-fg-color) * [@thumbs-hint-fg-color](#thumbs-hint-fg-color)
* [@thumbs-select-fg-color](#thumbs-select-fg-color) * [@thumbs-select-fg-color](#thumbs-select-fg-color)
* [@thumbs-contrast](#thumbs-contrast)
### @thumbs-key ### @thumbs-key
@ -234,6 +235,18 @@ For example:
set -g @thumbs-select-fg-color red 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 #### Colors
This is the list of available colors: This is the list of available colors:

View File

@ -105,6 +105,12 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.arg(
Arg::with_name("contrast")
.help("Put square brackets around hint for visibility")
.long("contrast")
.short("c"),
)
.get_matches(); .get_matches();
} }
@ -114,6 +120,7 @@ fn main() {
let position = args.value_of("position").unwrap(); let position = args.value_of("position").unwrap();
let reverse = args.is_present("reverse"); let reverse = args.is_present("reverse");
let unique = args.is_present("unique"); let unique = args.is_present("unique");
let contrast = args.is_present("contrast");
let regexp = if let Some(items) = args.values_of("regexp") { let regexp = if let Some(items) = args.values_of("regexp") {
items.collect::<Vec<_>>() items.collect::<Vec<_>>()
} else { } else {
@ -146,6 +153,7 @@ fn main() {
&mut state, &mut state,
reverse, reverse,
unique, unique,
contrast,
position, position,
select_foreground_color, select_foreground_color,
foreground_color, foreground_color,

View File

@ -9,6 +9,7 @@ pub struct View<'a> {
skip: usize, skip: usize,
reverse: bool, reverse: bool,
unique: bool, unique: bool,
contrast: bool,
position: &'a str, position: &'a str,
select_foreground_color: Color, select_foreground_color: Color,
foreground_color: Color, foreground_color: Color,
@ -22,6 +23,7 @@ impl<'a> View<'a> {
state: &'a mut state::State<'a>, state: &'a mut state::State<'a>,
reverse: bool, reverse: bool,
unique: bool, unique: bool,
contrast: bool,
position: &'a str, position: &'a str,
select_foreground_color: Color, select_foreground_color: Color,
foreground_color: Color, foreground_color: Color,
@ -34,6 +36,7 @@ impl<'a> View<'a> {
skip: 0, skip: 0,
reverse: reverse, reverse: reverse,
unique: unique, unique: unique,
contrast: contrast,
position: position, position: position,
select_foreground_color: select_foreground_color, select_foreground_color: select_foreground_color,
foreground_color: 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)> { pub fn present(&mut self) -> Option<(String, bool)> {
let mut rustbox = match RustBox::init(Default::default()) { let mut rustbox = match RustBox::init(Default::default()) {
Result::Ok(v) => v, Result::Ok(v) => v,
@ -83,13 +96,15 @@ impl<'a> View<'a> {
let clean = line.trim_end_matches(|c: char| c.is_whitespace()); let clean = line.trim_end_matches(|c: char| c.is_whitespace());
if clean.len() > 0 { if clean.len() > 0 {
let text = self.make_hint_text(line);
rustbox.print( rustbox.print(
0, 0,
index, index,
rustbox::RB_NORMAL, rustbox::RB_NORMAL,
Color::White, Color::White,
Color::Black, Color::Black,
line, &text,
); );
} }
} }
@ -108,6 +123,7 @@ impl<'a> View<'a> {
let prefix = &line[0..mat.x as usize]; let prefix = &line[0..mat.x as usize];
let extra = prefix.len() - prefix.chars().count(); let extra = prefix.len() - prefix.chars().count();
let offset = (mat.x as usize) - extra; let offset = (mat.x as usize) - extra;
let text = self.make_hint_text(mat.text);
rustbox.print( rustbox.print(
offset, offset,
@ -115,23 +131,25 @@ impl<'a> View<'a> {
rustbox::RB_NORMAL, rustbox::RB_NORMAL,
selected_color, selected_color,
self.background_color, self.background_color,
mat.text, &text,
); );
if let Some(ref hint) = mat.hint { if let Some(ref hint) = mat.hint {
let extra_position = if self.position == "left" { let extra_position = if self.position == "left" {
0 0
} else { } 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( rustbox.print(
offset + extra_position, offset + extra_position,
mat.y as usize, mat.y as usize,
rustbox::RB_BOLD, rustbox::RB_BOLD,
self.hint_foreground_color, self.hint_foreground_color,
self.hint_background_color, self.hint_background_color,
hint.as_str(), &text,
); );
} }
} }
@ -187,3 +205,39 @@ impl<'a> View<'a> {
None None
} }
} }
#[cfg(test)]
mod tests {
use super::*;
fn split(output: &str) -> Vec<&str> {
output.split("\n").collect::<Vec<&str>>()
}
#[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());
}
}

View File

@ -42,6 +42,7 @@ PARAMS[8]=$(option select-fg-color)
PARAMS[9]=$(option command) PARAMS[9]=$(option command)
PARAMS[10]=$(option upcase-command) PARAMS[10]=$(option upcase-command)
PARAMS[11]=$(multi regexp) PARAMS[11]=$(multi regexp)
PARAMS[12]=$(boolean contrast)
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TARGET_RELEASE="/target/release/" TARGET_RELEASE="/target/release/"