Skip to content

Commit

Permalink
refactor(shell): Switch termcolor to anstream
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 28, 2023
1 parent 87ad274 commit f12a63c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 123 deletions.
36 changes: 2 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
anstream = "0.6.0"
anstream = "0.6.1"
anstyle = "1.0.4"
anstyle-termcolor = "1.1.0"
anyhow = "1.0.75"
base64 = "0.21.3"
bytesize = "1.3"
Expand All @@ -41,7 +40,6 @@ curl = "0.4.44"
curl-sys = "0.4.66"
filetime = "0.2.22"
flate2 = { version = "1.0.27", default-features = false, features = ["zlib"] }
fwdansi = "1.1.0"
git2 = "0.18.0"
git2-curl = "0.19.0"
gix = { version = "0.54.1", default-features = false, features = ["blocking-http-transport-curl", "progress-tree", "revision"] }
Expand Down Expand Up @@ -91,7 +89,6 @@ snapbox = { version = "0.4.13", features = ["diff", "path"] }
syn = { version = "2.0.29", features = ["extra-traits", "full"] }
tar = { version = "0.4.40", default-features = false }
tempfile = "3.8.0"
termcolor = "1.2.0"
thiserror = "1.0.47"
time = { version = "0.3", features = ["parsing", "formatting", "serde"] }
toml = "0.7.6"
Expand Down Expand Up @@ -124,7 +121,6 @@ path = "src/cargo/lib.rs"

[dependencies]
anstream.workspace = true
anstyle-termcolor.workspace = true
anstyle.workspace = true
anyhow.workspace = true
base64.workspace = true
Expand Down Expand Up @@ -179,7 +175,6 @@ shell-escape.workspace = true
syn.workspace = true
tar.workspace = true
tempfile.workspace = true
termcolor.workspace = true
time.workspace = true
toml.workspace = true
toml_edit.workspace = true
Expand All @@ -194,9 +189,6 @@ walkdir.workspace = true
[target.'cfg(not(windows))'.dependencies]
openssl = { workspace = true, optional = true }

[target.'cfg(windows)'.dependencies]
fwdansi.workspace = true

[target.'cfg(windows)'.dependencies.windows-sys]
workspace = true
features = [
Expand Down
131 changes: 51 additions & 80 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::fmt;
use std::io::prelude::*;
use std::io::IsTerminal;

use anstream::AutoStream;
use anstream::Buffer;
use anstyle::Style;
use anstyle_termcolor::to_termcolor_spec;
use termcolor::{self, BufferWriter, StandardStream, WriteColor};

use crate::util::errors::CargoResult;
use crate::util::style::*;
Expand Down Expand Up @@ -86,10 +86,8 @@ enum ShellOut {
/// corresponding stream. The non-buffered fields should be used when you
/// do not want content to be buffered.
Stream {
stdout: StandardStream,
buffered_stdout: BufferWriter,
stderr: StandardStream,
buffered_stderr: BufferWriter,
stdout: AutoStream<std::io::Stdout>,
stderr: AutoStream<std::io::Stderr>,
stderr_tty: bool,
color_choice: ColorChoice,
},
Expand All @@ -111,15 +109,13 @@ impl Shell {
/// output.
pub fn new() -> Shell {
let auto_clr = ColorChoice::CargoAuto;
let stdout_choice = auto_clr.to_termcolor_color_choice(Stream::Stdout);
let stderr_choice = auto_clr.to_termcolor_color_choice(Stream::Stderr);
let stdout_choice = auto_clr.to_anstream_color_choice();
let stderr_choice = auto_clr.to_anstream_color_choice();
Shell {
output: ShellOut::Stream {
stdout: StandardStream::stdout(stdout_choice),
buffered_stdout: BufferWriter::stdout(stdout_choice),
stderr: StandardStream::stderr(stderr_choice),
buffered_stderr: BufferWriter::stderr(stderr_choice),
color_choice: ColorChoice::CargoAuto,
stdout: AutoStream::new(std::io::stdout(), stdout_choice),
stderr: AutoStream::new(std::io::stderr(), stderr_choice),
color_choice: auto_clr,
stderr_tty: std::io::stderr().is_terminal(),
},
verbosity: Verbosity::Verbose,
Expand Down Expand Up @@ -297,9 +293,7 @@ impl Shell {
pub fn set_color_choice(&mut self, color: Option<&str>) -> CargoResult<()> {
if let ShellOut::Stream {
ref mut stdout,
ref mut buffered_stdout,
ref mut stderr,
ref mut buffered_stderr,
ref mut color_choice,
..
} = self.output
Expand All @@ -317,12 +311,10 @@ impl Shell {
),
};
*color_choice = cfg;
let stdout_choice = cfg.to_termcolor_color_choice(Stream::Stdout);
let stderr_choice = cfg.to_termcolor_color_choice(Stream::Stderr);
*stdout = StandardStream::stdout(stdout_choice);
*buffered_stdout = BufferWriter::stdout(stdout_choice);
*stderr = StandardStream::stderr(stderr_choice);
*buffered_stderr = BufferWriter::stderr(stderr_choice);
let stdout_choice = cfg.to_anstream_color_choice();
let stderr_choice = cfg.to_anstream_color_choice();
*stdout = AutoStream::new(std::io::stdout(), stdout_choice);
*stderr = AutoStream::new(std::io::stderr(), stderr_choice);
}
Ok(())
}
Expand All @@ -342,14 +334,14 @@ impl Shell {
pub fn err_supports_color(&self) -> bool {
match &self.output {
ShellOut::Write(_) => false,
ShellOut::Stream { stderr, .. } => stderr.supports_color(),
ShellOut::Stream { stderr, .. } => supports_color(stderr.current_choice()),
}
}

pub fn out_supports_color(&self) -> bool {
match &self.output {
ShellOut::Write(_) => false,
ShellOut::Stream { stdout, .. } => stdout.supports_color(),
ShellOut::Stream { stdout, .. } => supports_color(stdout.current_choice()),
}
}

Expand Down Expand Up @@ -426,26 +418,22 @@ impl ShellOut {
justified: bool,
) -> CargoResult<()> {
match *self {
ShellOut::Stream {
ref mut buffered_stderr,
..
} => {
let mut buffer = buffered_stderr.buffer();
buffer.reset()?;
buffer.set_color(&to_termcolor_spec(*style))?;
ShellOut::Stream { ref mut stderr, .. } => {
let style = style.render();
let bold = (anstyle::Style::new() | anstyle::Effects::BOLD).render();
let reset = anstyle::Reset.render();

let mut buffer = Buffer::new();
if justified {
write!(buffer, "{:>12}", status)?;
write!(&mut buffer, "{style}{status:>12}{reset}")?;
} else {
write!(buffer, "{}", status)?;
buffer.set_color(termcolor::ColorSpec::new().set_bold(true))?;
write!(buffer, ":")?;
write!(&mut buffer, "{style}{status}{reset}{bold}:{reset}")?;
}
buffer.reset()?;
match message {
Some(message) => writeln!(buffer, " {}", message)?,
Some(message) => writeln!(buffer, " {message}")?,
None => write!(buffer, " ")?,
}
buffered_stderr.print(&buffer)?;
stderr.write_all(buffer.as_bytes())?;
}
ShellOut::Write(ref mut w) => {
if justified {
Expand All @@ -463,18 +451,15 @@ impl ShellOut {
}

/// Write a styled fragment
fn write_stdout(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> {
fn write_stdout(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> {
match *self {
ShellOut::Stream {
ref mut buffered_stdout,
..
} => {
let mut buffer = buffered_stdout.buffer();
buffer.reset()?;
buffer.set_color(&to_termcolor_spec(*color))?;
write!(buffer, "{}", fragment)?;
buffer.reset()?;
buffered_stdout.print(&buffer)?;
ShellOut::Stream { ref mut stdout, .. } => {
let style = style.render();
let reset = anstyle::Reset.render();

let mut buffer = Buffer::new();
write!(buffer, "{style}{}{reset}", fragment)?;
stdout.write_all(buffer.as_bytes())?;
}
ShellOut::Write(ref mut w) => {
write!(w, "{}", fragment)?;
Expand All @@ -484,18 +469,15 @@ impl ShellOut {
}

/// Write a styled fragment
fn write_stderr(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> {
fn write_stderr(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> {
match *self {
ShellOut::Stream {
ref mut buffered_stderr,
..
} => {
let mut buffer = buffered_stderr.buffer();
buffer.reset()?;
buffer.set_color(&to_termcolor_spec(*color))?;
write!(buffer, "{}", fragment)?;
buffer.reset()?;
buffered_stderr.print(&buffer)?;
ShellOut::Stream { ref mut stderr, .. } => {
let style = style.render();
let reset = anstyle::Reset.render();

let mut buffer = Buffer::new();
write!(buffer, "{style}{}{reset}", fragment)?;
stderr.write_all(buffer.as_bytes())?;
}
ShellOut::Write(ref mut w) => {
write!(w, "{}", fragment)?;
Expand Down Expand Up @@ -523,32 +505,21 @@ impl ShellOut {

impl ColorChoice {
/// Converts our color choice to termcolor's version.
fn to_termcolor_color_choice(self, stream: Stream) -> termcolor::ColorChoice {
fn to_anstream_color_choice(self) -> anstream::ColorChoice {
match self {
ColorChoice::Always => termcolor::ColorChoice::Always,
ColorChoice::Never => termcolor::ColorChoice::Never,
ColorChoice::CargoAuto => {
if stream.is_terminal() {
termcolor::ColorChoice::Auto
} else {
termcolor::ColorChoice::Never
}
}
ColorChoice::Always => anstream::ColorChoice::Always,
ColorChoice::Never => anstream::ColorChoice::Never,
ColorChoice::CargoAuto => anstream::ColorChoice::Auto,
}
}
}

enum Stream {
Stdout,
Stderr,
}

impl Stream {
fn is_terminal(self) -> bool {
match self {
Self::Stdout => std::io::stdout().is_terminal(),
Self::Stderr => std::io::stderr().is_terminal(),
}
fn supports_color(choice: anstream::ColorChoice) -> bool {
match choice {
anstream::ColorChoice::Always
| anstream::ColorChoice::AlwaysAnsi
| anstream::ColorChoice::Auto => true,
anstream::ColorChoice::Never => false,
}
}

Expand Down

0 comments on commit f12a63c

Please sign in to comment.