feat(shell): General output support

This provides a more general approach for writing output.  This will
respect the users choice for output support but the user must check the
verbosity if its relevant to their output.
This commit is contained in:
Ed Page 2022-02-25 19:22:17 -06:00
parent 4080ed40a0
commit e2c5c2f0dd

View file

@ -333,6 +333,17 @@ impl Shell {
}
}
/// Write a styled fragment
///
/// Caller is responsible for deciding whether [`Shell::verbosity`] is affects output.
pub fn write_stdout(
&mut self,
fragment: impl fmt::Display,
color: &ColorSpec,
) -> CargoResult<()> {
self.output.write_stdout(fragment, color)
}
/// Prints a message to stderr and translates ANSI escape code into console colors.
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
if self.needs_clear {
@ -423,6 +434,22 @@ impl ShellOut {
Ok(())
}
/// Write a styled fragment
fn write_stdout(&mut self, fragment: impl fmt::Display, color: &ColorSpec) -> CargoResult<()> {
match *self {
ShellOut::Stream { ref mut stdout, .. } => {
stdout.reset()?;
stdout.set_color(&color)?;
write!(stdout, "{}", fragment)?;
stdout.reset()?;
}
ShellOut::Write(ref mut w) => {
write!(w, "{}", fragment)?;
}
}
Ok(())
}
/// Gets stdout as a `io::Write`.
fn stdout(&mut self) -> &mut dyn Write {
match *self {