Skip to content

Commit 1afcaa6

Browse files
committed
Replace term with termcolor
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent 86ed53c commit 1afcaa6

File tree

14 files changed

+293
-404
lines changed

14 files changed

+293
-404
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ otel = [
4242
]
4343

4444
# Exports code dependent on private interfaces for the integration test suite
45-
test = ["dep:once_cell", "dep:walkdir"]
45+
test = ["dep:walkdir"]
4646

4747
# Sorted by alphabetic order
4848
[dependencies]
4949
anyhow.workspace = true
50+
atty = "0.2.14"
5051
cfg-if = "1.0"
5152
chrono = "0.4"
5253
clap = { version = "3", features = ["wrap_help"] }
@@ -62,7 +63,7 @@ home = "0.5.4"
6263
lazy_static.workspace = true
6364
libc = "0.2"
6465
num_cpus = "1.15"
65-
once_cell = { workspace = true, optional = true }
66+
once_cell = { workspace = true }
6667
opener = "0.6.0"
6768
opentelemetry = { workspace = true, optional = true }
6869
opentelemetry-otlp = { workspace = true, optional = true }
@@ -79,7 +80,7 @@ sharded-slab = "0.1.1"
7980
strsim = "0.10"
8081
tar = "0.4.26"
8182
tempfile.workspace = true
82-
term = "=0.5.1" # FIXME(issue #1818, #1826, and friends)
83+
termcolor = "1.2"
8384
thiserror.workspace = true
8485
threadpool = "1"
8586
tokio = { workspace = true, optional = true }

src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ pub mod proxy_mode;
1111
pub mod rustup_mode;
1212
pub mod self_update;
1313
pub mod setup_mode;
14-
mod term2;
14+
pub(crate) mod term;
1515
mod topical_doc;

src/cli/common.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ use std::{cmp, env};
1010
use anyhow::{anyhow, Context, Result};
1111
use git_testament::{git_testament, render_testament};
1212
use lazy_static::lazy_static;
13-
use term2::Terminal;
1413

1514
use super::self_update;
16-
use super::term2;
15+
use super::term;
1716
use crate::utils::notifications as util_notifications;
1817
use crate::utils::notify::NotificationLevel;
1918
use crate::utils::utils;
@@ -207,10 +206,10 @@ fn show_channel_updates(
207206
) -> Result<()> {
208207
let data = updates.into_iter().map(|(pkg, result)| {
209208
let (banner, color) = match &result {
210-
Ok(UpdateStatus::Installed) => ("installed", Some(term2::color::GREEN)),
211-
Ok(UpdateStatus::Updated(_)) => ("updated", Some(term2::color::GREEN)),
209+
Ok(UpdateStatus::Installed) => ("installed", Some(term::Color::Green)),
210+
Ok(UpdateStatus::Updated(_)) => ("updated", Some(term::Color::Green)),
212211
Ok(UpdateStatus::Unchanged) => ("unchanged", None),
213-
Err(_) => ("update failed", Some(term2::color::RED)),
212+
Err(_) => ("update failed", Some(term::Color::Red)),
214213
};
215214

216215
let (previous_version, version) = match &pkg {
@@ -248,7 +247,7 @@ fn show_channel_updates(
248247
Ok((pkg, banner, width, color, version, previous_version))
249248
});
250249

251-
let mut t = term2::stdout();
250+
let mut t = term::stdout();
252251

253252
let data: Vec<_> = data.collect::<Result<_>>()?;
254253
let max_width = data
@@ -259,7 +258,7 @@ fn show_channel_updates(
259258
let padding = max_width - width;
260259
let padding: String = " ".repeat(padding);
261260
let _ = write!(t, " {padding}");
262-
let _ = t.attr(term2::Attr::Bold);
261+
let _ = t.attr(term::Attr::Bold);
263262
if let Some(color) = color {
264263
let _ = t.fg(color);
265264
}
@@ -369,7 +368,7 @@ where
369368
}
370369

371370
pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<utils::ExitCode> {
372-
let mut t = term2::stdout();
371+
let mut t = term::stdout();
373372
let manifestation = distributable.get_manifestation()?;
374373
let config = manifestation.read_config()?.unwrap_or_default();
375374
let manifest = distributable.get_manifest()?;
@@ -382,7 +381,7 @@ pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<
382381
.as_ref()
383382
.expect("rust-std should have a target");
384383
if component.installed {
385-
let _ = t.attr(term2::Attr::Bold);
384+
let _ = t.attr(term::Attr::Bold);
386385
let _ = writeln!(t, "{target} (installed)");
387386
let _ = t.reset();
388387
} else if component.available {
@@ -397,7 +396,7 @@ pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<
397396
pub(crate) fn list_installed_targets(
398397
distributable: DistributableToolchain<'_>,
399398
) -> Result<utils::ExitCode> {
400-
let mut t = term2::stdout();
399+
let mut t = term::stdout();
401400
let manifestation = distributable.get_manifestation()?;
402401
let config = manifestation.read_config()?.unwrap_or_default();
403402
let manifest = distributable.get_manifest()?;
@@ -420,15 +419,15 @@ pub(crate) fn list_installed_targets(
420419
pub(crate) fn list_components(
421420
distributable: DistributableToolchain<'_>,
422421
) -> Result<utils::ExitCode> {
423-
let mut t = term2::stdout();
422+
let mut t = term::stdout();
424423
let manifestation = distributable.get_manifestation()?;
425424
let config = manifestation.read_config()?.unwrap_or_default();
426425
let manifest = distributable.get_manifest()?;
427426
let components = manifest.query_components(distributable.desc(), &config)?;
428427
for component in components {
429428
let name = component.name;
430429
if component.installed {
431-
t.attr(term2::Attr::Bold)?;
430+
t.attr(term::Attr::Bold)?;
432431
writeln!(t, "{name} (installed)")?;
433432
t.reset()?;
434433
} else if component.available {
@@ -440,7 +439,7 @@ pub(crate) fn list_components(
440439
}
441440

442441
pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_>) -> Result<()> {
443-
let mut t = term2::stdout();
442+
let mut t = term::stdout();
444443
let manifestation = distributable.get_manifestation()?;
445444
let config = manifestation.read_config()?.unwrap_or_default();
446445
let manifest = distributable.get_manifest()?;

src/cli/download_tracker.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use std::collections::VecDeque;
22
use std::fmt;
3-
use std::io::Write;
43
use std::time::{Duration, Instant};
54

6-
use term::Terminal;
7-
8-
use super::term2;
5+
use super::term;
96
use crate::dist::Notification as In;
107
use crate::utils::tty;
118
use crate::utils::units::{Size, Unit, UnitMode};
@@ -31,11 +28,7 @@ pub(crate) struct DownloadTracker {
3128
last_sec: Option<Instant>,
3229
/// Time stamp of the start of the download
3330
start_sec: Option<Instant>,
34-
/// The terminal we write the information to.
35-
/// XXX: Could be a term trait, but with #1818 on the horizon that
36-
/// is a pointless change to make - better to let that transition
37-
/// happen and take stock after that.
38-
term: term2::StdoutTerminal,
31+
term: Box<dyn term::Terminal>,
3932
/// Whether we displayed progress for the download or not.
4033
///
4134
/// If the download is quick enough, we don't have time to
@@ -61,7 +54,7 @@ impl DownloadTracker {
6154
downloaded_last_few_secs: VecDeque::with_capacity(DOWNLOAD_TRACK_COUNT),
6255
start_sec: None,
6356
last_sec: None,
64-
term: term2::stdout(),
57+
term: term::stdout(),
6558
displayed_charcount: None,
6659
units: vec![Unit::B],
6760
display_progress: true,

src/cli/log.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::fmt;
22
use std::io::Write;
3-
use term2::Terminal;
43

5-
use super::term2;
4+
use super::term;
65
use crate::process;
76

87
macro_rules! warn {
@@ -24,38 +23,38 @@ macro_rules! debug {
2423
}
2524

2625
pub(crate) fn warn_fmt(args: fmt::Arguments<'_>) {
27-
let mut t = term2::stderr();
28-
let _ = t.fg(term2::color::YELLOW);
29-
let _ = t.attr(term2::Attr::Bold);
26+
let mut t = term::stderr();
27+
let _ = t.fg(term::Color::Yellow);
28+
let _ = t.attr(term::Attr::Bold);
3029
let _ = write!(t, "warning: ");
3130
let _ = t.reset();
3231
let _ = t.write_fmt(args);
3332
let _ = writeln!(t);
3433
}
3534

3635
pub(crate) fn err_fmt(args: fmt::Arguments<'_>) {
37-
let mut t = term2::stderr();
38-
let _ = t.fg(term2::color::RED);
39-
let _ = t.attr(term2::Attr::Bold);
36+
let mut t = term::stderr();
37+
let _ = t.fg(term::Color::Red);
38+
let _ = t.attr(term::Attr::Bold);
4039
let _ = write!(t, "error: ");
4140
let _ = t.reset();
4241
let _ = t.write_fmt(args);
4342
let _ = writeln!(t);
4443
}
4544

4645
pub(crate) fn info_fmt(args: fmt::Arguments<'_>) {
47-
let mut t = term2::stderr();
48-
let _ = t.attr(term2::Attr::Bold);
46+
let mut t = term::stderr();
47+
let _ = t.attr(term::Attr::Bold);
4948
let _ = write!(t, "info: ");
5049
let _ = t.reset();
5150
let _ = t.write_fmt(args);
5251
let _ = writeln!(t);
5352
}
5453

5554
pub(crate) fn verbose_fmt(args: fmt::Arguments<'_>) {
56-
let mut t = term2::stderr();
57-
let _ = t.fg(term2::color::MAGENTA);
58-
let _ = t.attr(term2::Attr::Bold);
55+
let mut t = term::stderr();
56+
let _ = t.fg(term::Color::Magenta);
57+
let _ = t.attr(term::Attr::Bold);
5958
let _ = write!(t, "verbose: ");
6059
let _ = t.reset();
6160
let _ = t.write_fmt(args);
@@ -64,9 +63,9 @@ pub(crate) fn verbose_fmt(args: fmt::Arguments<'_>) {
6463

6564
pub(crate) fn debug_fmt(args: fmt::Arguments<'_>) {
6665
if process().var("RUSTUP_DEBUG").is_ok() {
67-
let mut t = term2::stderr();
68-
let _ = t.fg(term2::color::BLUE);
69-
let _ = t.attr(term2::Attr::Bold);
66+
let mut t = term::stderr();
67+
let _ = t.fg(term::Color::Blue);
68+
let _ = t.attr(term::Attr::Bold);
7069
let _ = write!(t, "debug: ");
7170
let _ = t.reset();
7271
let _ = t.write_fmt(args);

src/cli/markdown.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Write Markdown to the terminal
2-
3-
use std::io;
4-
52
use pulldown_cmark::{Event, Tag};
63

7-
use super::term2::{color, Attr, Terminal};
4+
use super::term::{Attr, Color, Terminal};
85

96
// Handles the wrapping of text written to the console
10-
struct LineWrapper<'a, T: Terminal> {
7+
struct LineWrapper<'a> {
118
indent: u32,
129
margin: u32,
1310
pos: u32,
14-
w: &'a mut T,
11+
w: &'a mut dyn Terminal,
1512
}
1613

17-
impl<'a, T: Terminal + 'a> LineWrapper<'a, T> {
14+
impl<'a> LineWrapper<'a> {
1815
// Just write a newline
1916
fn write_line(&mut self) {
2017
let _ = writeln!(self.w);
@@ -81,7 +78,7 @@ impl<'a, T: Terminal + 'a> LineWrapper<'a, T> {
8178
}
8279
}
8380
// Constructor
84-
fn new(w: &'a mut T, indent: u32, margin: u32) -> Self {
81+
fn new(w: &'a mut dyn Terminal, indent: u32, margin: u32) -> Self {
8582
LineWrapper {
8683
indent,
8784
margin,
@@ -92,14 +89,14 @@ impl<'a, T: Terminal + 'a> LineWrapper<'a, T> {
9289
}
9390

9491
// Handles the formatting of text
95-
struct LineFormatter<'a, T: Terminal + io::Write> {
92+
struct LineFormatter<'a> {
9693
is_code_block: bool,
97-
wrapper: LineWrapper<'a, T>,
94+
wrapper: LineWrapper<'a>,
9895
attrs: Vec<Attr>,
9996
}
10097

101-
impl<'a, T: Terminal + io::Write + 'a> LineFormatter<'a, T> {
102-
fn new(w: &'a mut T, indent: u32, margin: u32) -> Self {
98+
impl<'a> LineFormatter<'a> {
99+
fn new(w: &'a mut dyn Terminal, indent: u32, margin: u32) -> Self {
103100
LineFormatter {
104101
is_code_block: false,
105102
wrapper: LineWrapper::new(w, indent, margin),
@@ -145,7 +142,7 @@ impl<'a, T: Terminal + io::Write + 'a> LineFormatter<'a, T> {
145142
self.wrapper.write_line();
146143
}
147144
Tag::Emphasis => {
148-
self.push_attr(Attr::ForegroundColor(color::RED));
145+
self.push_attr(Attr::ForegroundColor(Color::Red));
149146
}
150147
Tag::Strong => {}
151148
Tag::Strikethrough => {}
@@ -221,7 +218,7 @@ impl<'a, T: Terminal + io::Write + 'a> LineFormatter<'a, T> {
221218
}
222219
}
223220

224-
pub(crate) fn md<'a, S: AsRef<str>, T: Terminal + io::Write + 'a>(t: &'a mut T, content: S) {
221+
pub(crate) fn md<S: AsRef<str>>(t: &mut dyn Terminal, content: S) {
225222
let mut f = LineFormatter::new(t, 0, 79);
226223
let parser = pulldown_cmark::Parser::new(content.as_ref());
227224
for event in parser {

0 commit comments

Comments
 (0)