Skip to content

Commit

Permalink
feat(search): Highlight search term
Browse files Browse the repository at this point in the history
This supersedes #10116

Fixes #9918
  • Loading branch information
epage committed Feb 26, 2022
1 parent 370c395 commit 1eb8913
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crates_io::{self, NewCrate, NewCrateDependency, Registry};
use curl::easy::{Easy, InfoType, SslOpt, SslVersion};
use log::{log, Level};
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use termcolor::Color::Green;
use termcolor::ColorSpec;

use crate::core::dependency::DepKind;
Expand Down Expand Up @@ -956,9 +957,16 @@ pub fn search(
}
None => name,
};
let _ = config
.shell()
.write_stdout(format_args!("{}\n", line), &ColorSpec::new());
let mut fragments = line.split(query).peekable();
while let Some(fragment) = fragments.next() {
let _ = config.shell().write_stdout(fragment, &ColorSpec::new());
if fragments.peek().is_some() {
let _ = config
.shell()
.write_stdout(query, &ColorSpec::new().set_bold(true).set_fg(Some(Green)));
}
}
let _ = config.shell().write_stdout("\n", &ColorSpec::new());
}

let search_max_limit = 100;
Expand Down
14 changes: 14 additions & 0 deletions tests/testsuite/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,17 @@ fn ignore_quiet() {
.with_stdout_contains(SEARCH_RESULTS)
.run();
}

#[cargo_test]
fn colored_results() {
setup();
set_cargo_config();

cargo_process("search --color=never postgres")
.with_stdout_does_not_contain("[..]\x1b[[..]")
.run();

cargo_process("search --color=always postgres")
.with_stdout_contains("[..]\x1b[[..]")
.run();
}

0 comments on commit 1eb8913

Please sign in to comment.