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

Preserve colors of text content #148

Open
wants to merge 1 commit into
base: master
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
60 changes: 60 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ clap = "2.34.0"
base64 = "0.13.1"
unicode-width = "0.1.10"
lazy_static = "1.4.0"
strip-ansi-escapes = "0.2.0"

[[bin]]
name = "thumbs"
Expand Down
36 changes: 27 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ NOTE: for changes to take effect, you'll need to source again your `.tmux.conf`
* [@thumbs-multi-bg-color](#thumbs-multi-bg-color)
* [@thumbs-contrast](#thumbs-contrast)
* [@thumbs-osc52](#thumbs-osc52)
* [@thumbs-keep-colors](#thumbs-keep-colors)

### @thumbs-key

Expand Down Expand Up @@ -344,6 +345,18 @@ For example:
set -g @thumbs-osc52 1
```

### @thumbs-keep-colors

`default: 0`

Keep text styling of the input when displaying matches.

For example:

```
set -g @thumbs-keep-colors 1
```

#### Colors

This is the list of predefined colors:
Expand Down Expand Up @@ -433,19 +446,20 @@ cargo install thumbs
And those are all available options:

```
thumbs 0.7.1
thumbs 0.8.0
A lightning fast version copy/pasting like vimium/vimperator

USAGE:
thumbs [FLAGS] [OPTIONS]

FLAGS:
-c, --contrast Put square brackets around hint for visibility
-h, --help Prints help information
-m, --multi Enable multi-selection
-r, --reverse Reverse the order for assigned hints
-u, --unique Don't show duplicated hints for the same match
-V, --version Prints version information
-c, --contrast Put square brackets around hint for visibility
-h, --help Prints help information
--keep-colors Preserve text styling of input
-m, --multi Enable multi-selection
-r, --reverse Reverse the order for assigned hints
-u, --unique Don't show duplicated hints for the same match
-V, --version Prints version information

OPTIONS:
-a, --alphabet <alphabet> Sets the alphabet [default: qwerty]
Expand All @@ -456,12 +470,16 @@ OPTIONS:

--hint-bg-color <hint_background_color> Sets the background color for hints [default: black]
--hint-fg-color <hint_foreground_color> Sets the foregroud color for hints [default: yellow]
--multi-bg-color <multi_background_color>
Sets the background color for a multi selected item [default: black]

--multi-fg-color <multi_foreground_color>
Sets the foreground color for a multi selected item [default: yellow]

-p, --position <position> Hint position [default: left]
-x, --regexp <regexp>... Use this regexp as extra pattern to match
--select-bg-color <select_background_color> Sets the background color for selection [default: black]
--select-fg-color <select_foreground_color> Sets the foreground color for selection [default: blue]
--multi-bg-color <multi_background_color> Sets the background color for a multi selected item [default: black]
--multi-fg-color <multi_foreground_color> Sets the foreground color for a multi selected item [default: cyan]
-t, --target <target> Stores the hint in the specified path
```

Expand Down
20 changes: 19 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
.short("t")
.takes_value(true),
)
.arg(
Arg::with_name("keep-colors")
.help("Preserve text styling of input")
.long("keep-colors"),
)
.get_matches()
}

Expand All @@ -149,6 +154,7 @@ fn main() {
let reverse = args.is_present("reverse");
let unique = args.is_present("unique");
let contrast = args.is_present("contrast");
let keep_colors = args.is_present("keep-colors");
let regexp = if let Some(items) = args.values_of("regexp") {
items.collect::<Vec<_>>()
} else {
Expand All @@ -172,7 +178,19 @@ fn main() {

let lines = output.split('\n').collect::<Vec<&str>>();

let mut state = state::State::new(&lines, alphabet, &regexp);
let mut state;
// strip ansi color codes for match searching
let unescaped: Vec<_> = lines
.iter()
.map(|line| String::from_utf8(strip_ansi_escapes::strip(line)).unwrap())
.collect();
let reference = unescaped.iter().map(|x| &**x).collect();

if keep_colors {
state = state::State::new_extended(&lines, reference, alphabet, &regexp);
} else {
state = state::State::new_extended(&reference, reference.clone(), alphabet, &regexp);
}

let selected = {
let mut viewbox = view::View::new(
Expand Down
20 changes: 18 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,36 @@ impl<'a> PartialEq for Match<'a> {

pub struct State<'a> {
pub lines: &'a Vec<&'a str>,
pub unescaped: Vec<&'a str>,
alphabet: &'a str,
regexp: &'a Vec<&'a str>,
}

impl<'a> State<'a> {
#[cfg(test)]
pub fn new(lines: &'a Vec<&'a str>, alphabet: &'a str, regexp: &'a Vec<&'a str>) -> State<'a> {
State {
unescaped: lines.clone(),
lines,
alphabet,
regexp,
}
}

pub fn new_extended(
lines: &'a Vec<&'a str>,
unescaped: Vec<&'a str>,
alphabet: &'a str,
regexp: &'a Vec<&'a str>,
) -> State<'a> {
State {
lines,
unescaped,
alphabet,
regexp,
}
}

pub fn matches(&self, reverse: bool, unique: bool) -> Vec<Match<'a>> {
let mut matches = Vec::new();

Expand All @@ -91,8 +108,7 @@ impl<'a> State<'a> {
// This order determines the priority of pattern matching
let all_patterns = [exclude_patterns, custom_patterns, patterns].concat();

for (index, line) in self.lines.iter().enumerate() {
let mut chunk: &str = line;
for (index, &(mut chunk)) in self.unescaped.iter().enumerate() {
let mut offset: i32 = 0;

loop {
Expand Down
4 changes: 2 additions & 2 deletions src/swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'a> Swapper<'a> {
let name = captures.get(1).unwrap().as_str();
let value = captures.get(2).unwrap().as_str();

let boolean_params = vec!["reverse", "unique", "contrast"];
let boolean_params = vec!["reverse", "unique", "contrast", "keep-colors"];

if boolean_params.iter().any(|&x| x == name) {
return vec![format!("--{}", name)];
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<'a> Swapper<'a> {
};

let pane_command = format!(
"tmux capture-pane -J -t {active_pane_id} -p{scroll_params} | tail -n {height} | {dir}/target/release/thumbs -f '%U:%H' -t {tmp} {args}; tmux swap-pane -t {active_pane_id}; {zoom_command} tmux wait-for -S {signal}",
"tmux capture-pane -J -et {active_pane_id} -p{scroll_params} | tail -n {height} | {dir}/target/release/thumbs -f '%U:%H' -t {tmp} {args}; tmux swap-pane -t {active_pane_id}; {zoom_command} tmux wait-for -S {signal}",
active_pane_id = active_pane_id,
scroll_params = scroll_params,
height = self.active_pane_height.unwrap_or(i32::MAX),
Expand Down
2 changes: 1 addition & 1 deletion src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'a> View<'a> {
};

// Find long utf sequences and extract it from mat.x
let line = &self.state.lines[mat.y as usize];
let line = &self.state.unescaped[mat.y as usize];
let prefix = &line[0..mat.x as usize];
let extra = prefix.width_cjk() - prefix.chars().count();
let offset = (mat.x as u16) - (extra as u16);
Expand Down