Skip to content

Commit 1eb8913

Browse files
committed
feat(search): Highlight search term
This supersedes #10116 Fixes #9918
1 parent 370c395 commit 1eb8913

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/cargo/ops/registry.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crates_io::{self, NewCrate, NewCrateDependency, Registry};
1313
use curl::easy::{Easy, InfoType, SslOpt, SslVersion};
1414
use log::{log, Level};
1515
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
16+
use termcolor::Color::Green;
1617
use termcolor::ColorSpec;
1718

1819
use crate::core::dependency::DepKind;
@@ -956,9 +957,16 @@ pub fn search(
956957
}
957958
None => name,
958959
};
959-
let _ = config
960-
.shell()
961-
.write_stdout(format_args!("{}\n", line), &ColorSpec::new());
960+
let mut fragments = line.split(query).peekable();
961+
while let Some(fragment) = fragments.next() {
962+
let _ = config.shell().write_stdout(fragment, &ColorSpec::new());
963+
if fragments.peek().is_some() {
964+
let _ = config
965+
.shell()
966+
.write_stdout(query, &ColorSpec::new().set_bold(true).set_fg(Some(Green)));
967+
}
968+
}
969+
let _ = config.shell().write_stdout("\n", &ColorSpec::new());
962970
}
963971

964972
let search_max_limit = 100;

tests/testsuite/search.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,17 @@ fn ignore_quiet() {
200200
.with_stdout_contains(SEARCH_RESULTS)
201201
.run();
202202
}
203+
204+
#[cargo_test]
205+
fn colored_results() {
206+
setup();
207+
set_cargo_config();
208+
209+
cargo_process("search --color=never postgres")
210+
.with_stdout_does_not_contain("[..]\x1b[[..]")
211+
.run();
212+
213+
cargo_process("search --color=always postgres")
214+
.with_stdout_contains("[..]\x1b[[..]")
215+
.run();
216+
}

0 commit comments

Comments
 (0)