Skip to content

Commit 9d754ed

Browse files
committed
Auto merge of #10425 - epage:search, r=alexcrichton
feat(search): Highlight search term This supersedes #10116. For the requested colored-output tests, this followed the pattern of the `fix` tests which just detects whether colored output is used or not. The `cache_messages` actually verify the output is colored but that is because it can just compare to a rustc call's output. Getting the colored output correct by hand in a test (with all of the resets) is a bit messy and would be brittle. This was done in an exercise in exploring ways to generalize colored output support in preparation for `cargo-add` doing some colored output as well. I converted all output calls to use this approach, even if coloring wasn't used, for consistency. I considered coloring the overflow message but decided to hold off on that for now (either a warning-yellow or a hint-gray). Fixes #9918
2 parents f75d4ea + 1eb8913 commit 9d754ed

3 files changed

Lines changed: 72 additions & 10 deletions

File tree

src/cargo/core/shell.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ impl Shell {
333333
}
334334
}
335335

336+
/// Write a styled fragment
337+
///
338+
/// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output.
339+
pub fn write_stdout(
340+
&mut self,
341+
fragment: impl fmt::Display,
342+
color: &ColorSpec,
343+
) -> CargoResult<()> {
344+
self.output.write_stdout(fragment, color)
345+
}
346+
336347
/// Prints a message to stderr and translates ANSI escape code into console colors.
337348
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
338349
if self.needs_clear {
@@ -423,6 +434,22 @@ impl ShellOut {
423434
Ok(())
424435
}
425436

437+
/// Write a styled fragment
438+
fn write_stdout(&mut self, fragment: impl fmt::Display, color: &ColorSpec) -> CargoResult<()> {
439+
match *self {
440+
ShellOut::Stream { ref mut stdout, .. } => {
441+
stdout.reset()?;
442+
stdout.set_color(&color)?;
443+
write!(stdout, "{}", fragment)?;
444+
stdout.reset()?;
445+
}
446+
ShellOut::Write(ref mut w) => {
447+
write!(w, "{}", fragment)?;
448+
}
449+
}
450+
Ok(())
451+
}
452+
426453
/// Gets stdout as a `io::Write`.
427454
fn stdout(&mut self) -> &mut dyn Write {
428455
match *self {

src/cargo/ops/registry.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ 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;
17+
use termcolor::ColorSpec;
1618

1719
use crate::core::dependency::DepKind;
1820
use crate::core::manifest::ManifestMetadata;
@@ -955,15 +957,26 @@ pub fn search(
955957
}
956958
None => name,
957959
};
958-
drop_println!(config, "{}", line);
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());
959970
}
960971

961972
let search_max_limit = 100;
962973
if total_crates > limit && limit < search_max_limit {
963-
drop_println!(
964-
config,
965-
"... and {} crates more (use --limit N to see more)",
966-
total_crates - limit
974+
let _ = config.shell().write_stdout(
975+
format_args!(
976+
"... and {} crates more (use --limit N to see more)\n",
977+
total_crates - limit
978+
),
979+
&ColorSpec::new(),
967980
);
968981
} else if total_crates > limit && limit >= search_max_limit {
969982
let extra = if source_id.is_default_registry() {
@@ -974,11 +987,9 @@ pub fn search(
974987
} else {
975988
String::new()
976989
};
977-
drop_println!(
978-
config,
979-
"... and {} crates more{}",
980-
total_crates - limit,
981-
extra
990+
let _ = config.shell().write_stdout(
991+
format_args!("... and {} crates more{}\n", total_crates - limit, extra),
992+
&ColorSpec::new(),
982993
);
983994
}
984995

tests/testsuite/search.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,27 @@ fn multiple_query_params() {
190190
.with_stdout_contains(SEARCH_RESULTS)
191191
.run();
192192
}
193+
194+
#[cargo_test]
195+
fn ignore_quiet() {
196+
setup();
197+
set_cargo_config();
198+
199+
cargo_process("search -q postgres")
200+
.with_stdout_contains(SEARCH_RESULTS)
201+
.run();
202+
}
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)