Skip to content

Commit 74a44d9

Browse files
committed
refactor(shell): Switch termcolor to anstream
1 parent f0fb5d3 commit 74a44d9

File tree

3 files changed

+50
-134
lines changed

3 files changed

+50
-134
lines changed

Cargo.lock

Lines changed: 0 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ license = "MIT OR Apache-2.0"
1818
[workspace.dependencies]
1919
anstream = "0.6.3"
2020
anstyle = "1.0.4"
21-
anstyle-termcolor = "1.1.0"
2221
anyhow = "1.0.75"
2322
base64 = "0.21.3"
2423
bytesize = "1.3"
@@ -41,7 +40,6 @@ curl = "0.4.44"
4140
curl-sys = "0.4.66"
4241
filetime = "0.2.22"
4342
flate2 = { version = "1.0.27", default-features = false, features = ["zlib"] }
44-
fwdansi = "1.1.0"
4543
git2 = "0.18.0"
4644
git2-curl = "0.19.0"
4745
gix = { version = "0.54.1", default-features = false, features = ["blocking-http-transport-curl", "progress-tree", "revision"] }
@@ -91,7 +89,6 @@ snapbox = { version = "0.4.13", features = ["diff", "path"] }
9189
syn = { version = "2.0.29", features = ["extra-traits", "full"] }
9290
tar = { version = "0.4.40", default-features = false }
9391
tempfile = "3.8.0"
94-
termcolor = "1.2.0"
9592
thiserror = "1.0.47"
9693
time = { version = "0.3", features = ["parsing", "formatting", "serde"] }
9794
toml = "0.7.6"
@@ -124,7 +121,6 @@ path = "src/cargo/lib.rs"
124121

125122
[dependencies]
126123
anstream.workspace = true
127-
anstyle-termcolor.workspace = true
128124
anstyle.workspace = true
129125
anyhow.workspace = true
130126
base64.workspace = true
@@ -179,7 +175,6 @@ shell-escape.workspace = true
179175
syn.workspace = true
180176
tar.workspace = true
181177
tempfile.workspace = true
182-
termcolor.workspace = true
183178
time.workspace = true
184179
toml.workspace = true
185180
toml_edit.workspace = true
@@ -194,9 +189,6 @@ walkdir.workspace = true
194189
[target.'cfg(not(windows))'.dependencies]
195190
openssl = { workspace = true, optional = true }
196191

197-
[target.'cfg(windows)'.dependencies]
198-
fwdansi.workspace = true
199-
200192
[target.'cfg(windows)'.dependencies.windows-sys]
201193
workspace = true
202194
features = [

src/cargo/core/shell.rs

Lines changed: 50 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use std::fmt;
22
use std::io::prelude::*;
33
use std::io::IsTerminal;
44

5+
use anstream::AutoStream;
56
use anstyle::Style;
6-
use anstyle_termcolor::to_termcolor_spec;
7-
use termcolor::{self, BufferWriter, StandardStream, WriteColor};
87

98
use crate::util::errors::CargoResult;
109
use crate::util::style::*;
@@ -86,10 +85,8 @@ enum ShellOut {
8685
/// corresponding stream. The non-buffered fields should be used when you
8786
/// do not want content to be buffered.
8887
Stream {
89-
stdout: StandardStream,
90-
buffered_stdout: BufferWriter,
91-
stderr: StandardStream,
92-
buffered_stderr: BufferWriter,
88+
stdout: AutoStream<std::io::Stdout>,
89+
stderr: AutoStream<std::io::Stderr>,
9390
stderr_tty: bool,
9491
color_choice: ColorChoice,
9592
},
@@ -111,15 +108,13 @@ impl Shell {
111108
/// output.
112109
pub fn new() -> Shell {
113110
let auto_clr = ColorChoice::CargoAuto;
114-
let stdout_choice = auto_clr.to_termcolor_color_choice(Stream::Stdout);
115-
let stderr_choice = auto_clr.to_termcolor_color_choice(Stream::Stderr);
111+
let stdout_choice = auto_clr.to_anstream_color_choice();
112+
let stderr_choice = auto_clr.to_anstream_color_choice();
116113
Shell {
117114
output: ShellOut::Stream {
118-
stdout: StandardStream::stdout(stdout_choice),
119-
buffered_stdout: BufferWriter::stdout(stdout_choice),
120-
stderr: StandardStream::stderr(stderr_choice),
121-
buffered_stderr: BufferWriter::stderr(stderr_choice),
122-
color_choice: ColorChoice::CargoAuto,
115+
stdout: AutoStream::new(std::io::stdout(), stdout_choice),
116+
stderr: AutoStream::new(std::io::stderr(), stderr_choice),
117+
color_choice: auto_clr,
123118
stderr_tty: std::io::stderr().is_terminal(),
124119
},
125120
verbosity: Verbosity::Verbose,
@@ -297,9 +292,7 @@ impl Shell {
297292
pub fn set_color_choice(&mut self, color: Option<&str>) -> CargoResult<()> {
298293
if let ShellOut::Stream {
299294
ref mut stdout,
300-
ref mut buffered_stdout,
301295
ref mut stderr,
302-
ref mut buffered_stderr,
303296
ref mut color_choice,
304297
..
305298
} = self.output
@@ -317,12 +310,10 @@ impl Shell {
317310
),
318311
};
319312
*color_choice = cfg;
320-
let stdout_choice = cfg.to_termcolor_color_choice(Stream::Stdout);
321-
let stderr_choice = cfg.to_termcolor_color_choice(Stream::Stderr);
322-
*stdout = StandardStream::stdout(stdout_choice);
323-
*buffered_stdout = BufferWriter::stdout(stdout_choice);
324-
*stderr = StandardStream::stderr(stderr_choice);
325-
*buffered_stderr = BufferWriter::stderr(stderr_choice);
313+
let stdout_choice = cfg.to_anstream_color_choice();
314+
let stderr_choice = cfg.to_anstream_color_choice();
315+
*stdout = AutoStream::new(std::io::stdout(), stdout_choice);
316+
*stderr = AutoStream::new(std::io::stderr(), stderr_choice);
326317
}
327318
Ok(())
328319
}
@@ -342,14 +333,14 @@ impl Shell {
342333
pub fn err_supports_color(&self) -> bool {
343334
match &self.output {
344335
ShellOut::Write(_) => false,
345-
ShellOut::Stream { stderr, .. } => stderr.supports_color(),
336+
ShellOut::Stream { stderr, .. } => supports_color(stderr.current_choice()),
346337
}
347338
}
348339

349340
pub fn out_supports_color(&self) -> bool {
350341
match &self.output {
351342
ShellOut::Write(_) => false,
352-
ShellOut::Stream { stdout, .. } => stdout.supports_color(),
343+
ShellOut::Stream { stdout, .. } => supports_color(stdout.current_choice()),
353344
}
354345
}
355346

@@ -372,13 +363,6 @@ impl Shell {
372363
if self.needs_clear {
373364
self.err_erase_line();
374365
}
375-
#[cfg(windows)]
376-
{
377-
if let ShellOut::Stream { stderr, .. } = &mut self.output {
378-
::fwdansi::write_ansi(stderr, message)?;
379-
return Ok(());
380-
}
381-
}
382366
self.err().write_all(message)?;
383367
Ok(())
384368
}
@@ -388,13 +372,6 @@ impl Shell {
388372
if self.needs_clear {
389373
self.err_erase_line();
390374
}
391-
#[cfg(windows)]
392-
{
393-
if let ShellOut::Stream { stdout, .. } = &mut self.output {
394-
::fwdansi::write_ansi(stdout, message)?;
395-
return Ok(());
396-
}
397-
}
398375
self.out().write_all(message)?;
399376
Ok(())
400377
}
@@ -426,26 +403,22 @@ impl ShellOut {
426403
justified: bool,
427404
) -> CargoResult<()> {
428405
match *self {
429-
ShellOut::Stream {
430-
ref mut buffered_stderr,
431-
..
432-
} => {
433-
let mut buffer = buffered_stderr.buffer();
434-
buffer.reset()?;
435-
buffer.set_color(&to_termcolor_spec(*style))?;
406+
ShellOut::Stream { ref mut stderr, .. } => {
407+
let style = style.render();
408+
let bold = (anstyle::Style::new() | anstyle::Effects::BOLD).render();
409+
let reset = anstyle::Reset.render();
410+
411+
let mut buffer = Vec::new();
436412
if justified {
437-
write!(buffer, "{:>12}", status)?;
413+
write!(&mut buffer, "{style}{status:>12}{reset}")?;
438414
} else {
439-
write!(buffer, "{}", status)?;
440-
buffer.set_color(termcolor::ColorSpec::new().set_bold(true))?;
441-
write!(buffer, ":")?;
415+
write!(&mut buffer, "{style}{status}{reset}{bold}:{reset}")?;
442416
}
443-
buffer.reset()?;
444417
match message {
445-
Some(message) => writeln!(buffer, " {}", message)?,
418+
Some(message) => writeln!(buffer, " {message}")?,
446419
None => write!(buffer, " ")?,
447420
}
448-
buffered_stderr.print(&buffer)?;
421+
stderr.write_all(&buffer)?;
449422
}
450423
ShellOut::Write(ref mut w) => {
451424
if justified {
@@ -463,18 +436,15 @@ impl ShellOut {
463436
}
464437

465438
/// Write a styled fragment
466-
fn write_stdout(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> {
439+
fn write_stdout(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> {
467440
match *self {
468-
ShellOut::Stream {
469-
ref mut buffered_stdout,
470-
..
471-
} => {
472-
let mut buffer = buffered_stdout.buffer();
473-
buffer.reset()?;
474-
buffer.set_color(&to_termcolor_spec(*color))?;
475-
write!(buffer, "{}", fragment)?;
476-
buffer.reset()?;
477-
buffered_stdout.print(&buffer)?;
441+
ShellOut::Stream { ref mut stdout, .. } => {
442+
let style = style.render();
443+
let reset = anstyle::Reset.render();
444+
445+
let mut buffer = Vec::new();
446+
write!(buffer, "{style}{}{reset}", fragment)?;
447+
stdout.write_all(&buffer)?;
478448
}
479449
ShellOut::Write(ref mut w) => {
480450
write!(w, "{}", fragment)?;
@@ -484,18 +454,15 @@ impl ShellOut {
484454
}
485455

486456
/// Write a styled fragment
487-
fn write_stderr(&mut self, fragment: impl fmt::Display, color: &Style) -> CargoResult<()> {
457+
fn write_stderr(&mut self, fragment: impl fmt::Display, style: &Style) -> CargoResult<()> {
488458
match *self {
489-
ShellOut::Stream {
490-
ref mut buffered_stderr,
491-
..
492-
} => {
493-
let mut buffer = buffered_stderr.buffer();
494-
buffer.reset()?;
495-
buffer.set_color(&to_termcolor_spec(*color))?;
496-
write!(buffer, "{}", fragment)?;
497-
buffer.reset()?;
498-
buffered_stderr.print(&buffer)?;
459+
ShellOut::Stream { ref mut stderr, .. } => {
460+
let style = style.render();
461+
let reset = anstyle::Reset.render();
462+
463+
let mut buffer = Vec::new();
464+
write!(buffer, "{style}{}{reset}", fragment)?;
465+
stderr.write_all(&buffer)?;
499466
}
500467
ShellOut::Write(ref mut w) => {
501468
write!(w, "{}", fragment)?;
@@ -523,32 +490,21 @@ impl ShellOut {
523490

524491
impl ColorChoice {
525492
/// Converts our color choice to termcolor's version.
526-
fn to_termcolor_color_choice(self, stream: Stream) -> termcolor::ColorChoice {
493+
fn to_anstream_color_choice(self) -> anstream::ColorChoice {
527494
match self {
528-
ColorChoice::Always => termcolor::ColorChoice::Always,
529-
ColorChoice::Never => termcolor::ColorChoice::Never,
530-
ColorChoice::CargoAuto => {
531-
if stream.is_terminal() {
532-
termcolor::ColorChoice::Auto
533-
} else {
534-
termcolor::ColorChoice::Never
535-
}
536-
}
495+
ColorChoice::Always => anstream::ColorChoice::Always,
496+
ColorChoice::Never => anstream::ColorChoice::Never,
497+
ColorChoice::CargoAuto => anstream::ColorChoice::Auto,
537498
}
538499
}
539500
}
540501

541-
enum Stream {
542-
Stdout,
543-
Stderr,
544-
}
545-
546-
impl Stream {
547-
fn is_terminal(self) -> bool {
548-
match self {
549-
Self::Stdout => std::io::stdout().is_terminal(),
550-
Self::Stderr => std::io::stderr().is_terminal(),
551-
}
502+
fn supports_color(choice: anstream::ColorChoice) -> bool {
503+
match choice {
504+
anstream::ColorChoice::Always
505+
| anstream::ColorChoice::AlwaysAnsi
506+
| anstream::ColorChoice::Auto => true,
507+
anstream::ColorChoice::Never => false,
552508
}
553509
}
554510

0 commit comments

Comments
 (0)