mirror of
https://github.com/rust-lang/cargo
synced 2024-11-05 18:50:39 +00:00
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:
parent
4080ed40a0
commit
e2c5c2f0dd
1 changed files with 27 additions and 0 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue