Skip to content

Commit 256ea28

Browse files
epagedjc
authored andcommitted
refactor: Replace termcolor with anstream
1 parent b5d7bbf commit 256ea28

File tree

4 files changed

+34
-40
lines changed

4 files changed

+34
-40
lines changed

Cargo.lock

Lines changed: 1 addition & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ test = ["dep:snapbox", "dep:walkdir"]
4141

4242
# Sorted by alphabetic order
4343
[dependencies]
44+
anstream = "0.6.20"
4445
anstyle = "1.0.11"
45-
anstyle-termcolor = "1.1.4"
4646
anyhow = "1.0.69"
4747
cfg-if = "1.0"
4848
chrono = { version = "0.4", default-features = false, features = ["std"] }
@@ -87,7 +87,6 @@ sharded-slab = "0.1.1"
8787
strsim = "0.11"
8888
tar = "0.4.26"
8989
tempfile = "3.8"
90-
termcolor = "1.2"
9190
thiserror = "2"
9291
threadpool = "1"
9392
tokio = { version = "1.26.0", default-features = false, features = ["macros", "rt-multi-thread", "sync"] }

src/cli/rustup_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
time::Duration,
1111
};
1212

13+
use anstream::ColorChoice;
1314
use anstyle::Style;
1415
use anyhow::{Context, Error, Result, anyhow};
1516
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum, builder::PossibleValue};
@@ -18,7 +19,6 @@ use console::style;
1819
use futures_util::stream::StreamExt;
1920
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
2021
use itertools::Itertools;
21-
use termcolor::ColorChoice;
2222
use tokio::sync::Semaphore;
2323
use tracing::{info, trace, warn};
2424
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};

src/process/terminal_source.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ use std::{
99
sync::{Arc, Mutex, MutexGuard},
1010
};
1111

12-
use anstyle::Style;
13-
use anstyle_termcolor::to_termcolor_spec;
14-
use termcolor::ColorChoice;
15-
use termcolor::{StandardStream, StandardStreamLock, WriteColor};
12+
use anstream::{AutoStream, ColorChoice};
13+
use anstyle::{Reset, Style};
1614

1715
use super::Process;
1816
#[cfg(feature = "test")]
@@ -60,8 +58,8 @@ impl ColorableTerminal {
6058
_ => ColorChoice::Never,
6159
};
6260
let inner = match stream {
63-
StreamSelector::Stdout => TerminalInner::StandardStream(StandardStream::stdout(choice)),
64-
StreamSelector::Stderr => TerminalInner::StandardStream(StandardStream::stderr(choice)),
61+
StreamSelector::Stdout => TerminalInner::Stdout(AutoStream::new(io::stdout(), choice)),
62+
StreamSelector::Stderr => TerminalInner::Stderr(AutoStream::new(io::stderr(), choice)),
6563
#[cfg(feature = "test")]
6664
StreamSelector::TestWriter(w) => TerminalInner::TestWriter(w, choice),
6765
#[cfg(all(test, feature = "test"))]
@@ -95,9 +93,13 @@ impl ColorableTerminal {
9593
addr_of_mut!((*ptr).guard).write((*ptr).inner.lock().unwrap());
9694
// let locked = match *guard {....}
9795
addr_of_mut!((*ptr).locked).write(match (*ptr).guard.deref_mut() {
98-
TerminalInner::StandardStream(s) => {
99-
let locked = s.lock();
100-
TerminalInnerLocked::StandardStream(locked)
96+
TerminalInner::Stdout(_) => {
97+
let locked = io::stdout().lock();
98+
TerminalInnerLocked::Stdout(AutoStream::new(locked, self.color_choice))
99+
}
100+
TerminalInner::Stderr(_) => {
101+
let locked = io::stderr().lock();
102+
TerminalInnerLocked::Stderr(AutoStream::new(locked, self.color_choice))
101103
}
102104
#[cfg(feature = "test")]
103105
TerminalInner::TestWriter(w, _) => TerminalInnerLocked::TestWriter(w.lock()),
@@ -109,15 +111,24 @@ impl ColorableTerminal {
109111

110112
pub fn style(&mut self, new: &Style) -> io::Result<()> {
111113
match self.inner.lock().unwrap().deref_mut() {
112-
TerminalInner::StandardStream(s) => s.set_color(&to_termcolor_spec(*new)),
114+
TerminalInner::Stdout(s) => {
115+
write!(s, "{Reset}{new}")
116+
}
117+
TerminalInner::Stderr(s) => {
118+
write!(s, "{Reset}{new}")
119+
}
113120
#[cfg(feature = "test")]
114121
TerminalInner::TestWriter(_, _) => Ok(()),
115122
}
116123
}
117-
118124
pub fn reset(&mut self) -> io::Result<()> {
119125
match self.inner.lock().unwrap().deref_mut() {
120-
TerminalInner::StandardStream(s) => s.reset(),
126+
TerminalInner::Stdout(s) => {
127+
write!(s, "{Reset}")
128+
}
129+
TerminalInner::Stderr(s) => {
130+
write!(s, "{Reset}")
131+
}
121132
#[cfg(feature = "test")]
122133
TerminalInner::TestWriter(_, _) => Ok(()),
123134
}
@@ -268,15 +279,17 @@ impl io::Write for ColorableTerminalLocked {
268279
}
269280

270281
enum TerminalInnerLocked {
271-
StandardStream(StandardStreamLock<'static>),
282+
Stdout(AutoStream<io::StdoutLock<'static>>),
283+
Stderr(AutoStream<io::StderrLock<'static>>),
272284
#[cfg(feature = "test")]
273285
TestWriter(TestWriterLock<'static>),
274286
}
275287

276288
impl TerminalInnerLocked {
277289
fn as_write(&mut self) -> &mut dyn io::Write {
278290
match self {
279-
TerminalInnerLocked::StandardStream(s) => s,
291+
TerminalInnerLocked::Stdout(s) => s,
292+
TerminalInnerLocked::Stderr(s) => s,
280293
#[cfg(feature = "test")]
281294
TerminalInnerLocked::TestWriter(w) => w,
282295
}
@@ -285,7 +298,8 @@ impl TerminalInnerLocked {
285298

286299
/// Internal state for ColorableTerminal
287300
enum TerminalInner {
288-
StandardStream(StandardStream),
301+
Stdout(AutoStream<io::Stdout>),
302+
Stderr(AutoStream<io::Stderr>),
289303
#[cfg(feature = "test")]
290304
#[allow(dead_code)] // ColorChoice only read in test code
291305
TestWriter(TestWriter, ColorChoice),
@@ -294,7 +308,8 @@ enum TerminalInner {
294308
impl TerminalInner {
295309
fn as_write(&mut self) -> &mut dyn io::Write {
296310
match self {
297-
TerminalInner::StandardStream(s) => s,
311+
TerminalInner::Stdout(s) => s,
312+
TerminalInner::Stderr(s) => s,
298313
#[cfg(feature = "test")]
299314
TerminalInner::TestWriter(w, _) => w,
300315
}

0 commit comments

Comments
 (0)