Skip to content

Commit

Permalink
refactor(search): Rely on anstream for cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 25, 2023
1 parent 1b9aefb commit 6a390da
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/cargo/ops/registry/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ pub fn search(
.map(|desc| truncate_with_ellipsis(&desc.replace("\n", " "), description_length))
});

let mut shell = config.shell();
let stdout = shell.out();
let good = style::GOOD.render();
let reset = anstyle::Reset.render();

for (name, description) in names.into_iter().zip(descriptions) {
let line = match description {
Some(desc) => {
Expand All @@ -58,22 +63,20 @@ pub fn search(
};
let mut fragments = line.split(query).peekable();
while let Some(fragment) = fragments.next() {
let _ = config.shell().write_stdout(fragment, &style::NOP);
let _ = write!(stdout, "{fragment}");
if fragments.peek().is_some() {
let _ = config.shell().write_stdout(query, &style::GOOD);
let _ = write!(stdout, "{good}{query}{reset}");
}
}
let _ = config.shell().write_stdout("\n", &style::NOP);
let _ = writeln!(stdout);
}

let search_max_limit = 100;
if total_crates > limit && limit < search_max_limit {
let _ = config.shell().write_stdout(
format_args!(
"... and {} crates more (use --limit N to see more)\n",
total_crates - limit
),
&style::NOP,
let _ = writeln!(
stdout,
"... and {} crates more (use --limit N to see more)",
total_crates - limit
);
} else if total_crates > limit && limit >= search_max_limit {
let extra = if source_ids.original.is_crates_io() {
Expand All @@ -82,9 +85,11 @@ pub fn search(
} else {
String::new()
};
let _ = config.shell().write_stdout(
format_args!("... and {} crates more{}\n", total_crates - limit, extra),
&style::NOP,
let _ = writeln!(
stdout,
"... and {} crates more{}",
total_crates - limit,
extra
);
}

Expand Down

0 comments on commit 6a390da

Please sign in to comment.