Skip to content

Commit 6a390da

Browse files
committed
refactor(search): Rely on anstream for cleaner code
1 parent 1b9aefb commit 6a390da

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/cargo/ops/registry/search.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ pub fn search(
4646
.map(|desc| truncate_with_ellipsis(&desc.replace("\n", " "), description_length))
4747
});
4848

49+
let mut shell = config.shell();
50+
let stdout = shell.out();
51+
let good = style::GOOD.render();
52+
let reset = anstyle::Reset.render();
53+
4954
for (name, description) in names.into_iter().zip(descriptions) {
5055
let line = match description {
5156
Some(desc) => {
@@ -58,22 +63,20 @@ pub fn search(
5863
};
5964
let mut fragments = line.split(query).peekable();
6065
while let Some(fragment) = fragments.next() {
61-
let _ = config.shell().write_stdout(fragment, &style::NOP);
66+
let _ = write!(stdout, "{fragment}");
6267
if fragments.peek().is_some() {
63-
let _ = config.shell().write_stdout(query, &style::GOOD);
68+
let _ = write!(stdout, "{good}{query}{reset}");
6469
}
6570
}
66-
let _ = config.shell().write_stdout("\n", &style::NOP);
71+
let _ = writeln!(stdout);
6772
}
6873

6974
let search_max_limit = 100;
7075
if total_crates > limit && limit < search_max_limit {
71-
let _ = config.shell().write_stdout(
72-
format_args!(
73-
"... and {} crates more (use --limit N to see more)\n",
74-
total_crates - limit
75-
),
76-
&style::NOP,
76+
let _ = writeln!(
77+
stdout,
78+
"... and {} crates more (use --limit N to see more)",
79+
total_crates - limit
7780
);
7881
} else if total_crates > limit && limit >= search_max_limit {
7982
let extra = if source_ids.original.is_crates_io() {
@@ -82,9 +85,11 @@ pub fn search(
8285
} else {
8386
String::new()
8487
};
85-
let _ = config.shell().write_stdout(
86-
format_args!("... and {} crates more{}\n", total_crates - limit, extra),
87-
&style::NOP,
88+
let _ = writeln!(
89+
stdout,
90+
"... and {} crates more{}",
91+
total_crates - limit,
92+
extra
8893
);
8994
}
9095

0 commit comments

Comments
 (0)