Skip to content

Commit a970bfc

Browse files
committed
fix(add): Restore highlights lost when switching to Shell
1 parent 6ec86fc commit a970bfc

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/cargo/core/shell.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ impl Shell {
344344
self.output.write_stdout(fragment, color)
345345
}
346346

347+
/// Write a styled fragment
348+
///
349+
/// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output.
350+
pub fn write_stderr(
351+
&mut self,
352+
fragment: impl fmt::Display,
353+
color: &ColorSpec,
354+
) -> CargoResult<()> {
355+
self.output.write_stderr(fragment, color)
356+
}
357+
347358
/// Prints a message to stderr and translates ANSI escape code into console colors.
348359
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
349360
if self.needs_clear {
@@ -450,6 +461,22 @@ impl ShellOut {
450461
Ok(())
451462
}
452463

464+
/// Write a styled fragment
465+
fn write_stderr(&mut self, fragment: impl fmt::Display, color: &ColorSpec) -> CargoResult<()> {
466+
match *self {
467+
ShellOut::Stream { ref mut stderr, .. } => {
468+
stderr.reset()?;
469+
stderr.set_color(&color)?;
470+
write!(stderr, "{}", fragment)?;
471+
stderr.reset()?;
472+
}
473+
ShellOut::Write(ref mut w) => {
474+
write!(w, "{}", fragment)?;
475+
}
476+
}
477+
Ok(())
478+
}
479+
453480
/// Gets stdout as a `io::Write`.
454481
fn stdout(&mut self) -> &mut dyn Write {
455482
match *self {

src/cargo/ops/cargo_add/mod.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use std::path::Path;
1010

1111
use cargo_util::paths;
1212
use indexmap::IndexSet;
13+
use termcolor::Color::Green;
14+
use termcolor::Color::Red;
15+
use termcolor::ColorSpec;
1316
use toml_edit::Item as TomlItem;
1417

1518
use crate::core::dependency::DepKind;
@@ -544,6 +547,10 @@ fn populate_available_features(
544547
fn print_msg(shell: &mut Shell, dep: &Dependency, section: &[String]) -> CargoResult<()> {
545548
use std::fmt::Write;
546549

550+
if matches!(shell.verbosity(), crate::core::shell::Verbosity::Quiet) {
551+
return Ok(());
552+
}
553+
547554
let mut message = String::new();
548555
write!(message, "{}", dep.name)?;
549556
match dep.source() {
@@ -573,6 +580,7 @@ fn print_msg(shell: &mut Shell, dep: &Dependency, section: &[String]) -> CargoRe
573580
};
574581
write!(message, " {section}")?;
575582
write!(message, ".")?;
583+
shell.status("Adding", message)?;
576584

577585
let mut activated: IndexSet<_> = dep.features.iter().flatten().map(|s| s.as_str()).collect();
578586
if dep.default_features().unwrap_or(true) {
@@ -604,20 +612,20 @@ fn print_msg(shell: &mut Shell, dep: &Dependency, section: &[String]) -> CargoRe
604612
.collect::<Vec<_>>();
605613
deactivated.sort();
606614
if !activated.is_empty() || !deactivated.is_empty() {
607-
writeln!(message)?;
608-
write!(message, "{:>13}Features:", " ")?;
615+
let prefix = format!("{:>13}", " ");
616+
shell.write_stderr(format_args!("{}Features:\n", prefix), &ColorSpec::new())?;
609617
for feat in activated {
610-
writeln!(message)?;
611-
write!(message, "{:>13}+ {}", " ", feat)?;
618+
shell.write_stderr(&prefix, &ColorSpec::new())?;
619+
shell.write_stderr('+', &ColorSpec::new().set_bold(true).set_fg(Some(Green)))?;
620+
shell.write_stderr(format_args!(" {}\n", feat), &ColorSpec::new())?;
612621
}
613622
for feat in deactivated {
614-
writeln!(message)?;
615-
write!(message, "{:>13}- {}", " ", feat)?;
623+
shell.write_stderr(&prefix, &ColorSpec::new())?;
624+
shell.write_stderr('-', &ColorSpec::new().set_bold(true).set_fg(Some(Red)))?;
625+
shell.write_stderr(format_args!(" {}\n", feat), &ColorSpec::new())?;
616626
}
617627
}
618628

619-
shell.status("Adding", message)?;
620-
621629
Ok(())
622630
}
623631

0 commit comments

Comments
 (0)