Skip to content

Commit 0cd5415

Browse files
committed
Replace term with termcolor
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent 4b29b79 commit 0cd5415

File tree

14 files changed

+293
-425
lines changed

14 files changed

+293
-425
lines changed

Cargo.lock

Lines changed: 2 additions & 17 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 & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ version = "1.26.0"
1616

1717
[features]
1818
curl-backend = ["download/curl-backend"]
19-
default = [
20-
"curl-backend",
21-
"reqwest-backend",
22-
"reqwest-default-tls",
23-
"reqwest-rustls-tls",
24-
]
19+
default = ["curl-backend", "reqwest-backend", "reqwest-default-tls", "reqwest-rustls-tls"]
2520

2621
reqwest-backend = ["download/reqwest-backend"]
2722
vendored-openssl = ['openssl/vendored']
@@ -33,13 +28,7 @@ reqwest-rustls-tls = ["download/reqwest-rustls-tls"]
3328
no-self-update = []
3429

3530
# Include an Opentelemetry sink for tracing events
36-
otel = [
37-
"dep:opentelemetry-otlp",
38-
"dep:tracing-opentelemetry",
39-
"dep:tracing-subscriber",
40-
"dep:opentelemetry",
41-
"dep:tokio",
42-
]
31+
otel = ["dep:opentelemetry-otlp", "dep:tracing-opentelemetry", "dep:tracing-subscriber", "dep:opentelemetry", "dep:tokio"]
4332

4433
# Sorted by alphabetic order
4534
[dependencies]
@@ -78,8 +67,7 @@ sharded-slab = "0.1.1"
7867
strsim = "0.10"
7968
tar = "0.4.26"
8069
tempfile.workspace = true
81-
# FIXME(issue #1818, #1826, and friends)
82-
term = "=0.5.1"
70+
termcolor = "1.2"
8371
thiserror.workspace = true
8472
threadpool = "1"
8573
tokio = { workspace = true, optional = true }
@@ -93,6 +81,7 @@ url.workspace = true
9381
wait-timeout = "0.2"
9482
xz2 = "0.1.3"
9583
zstd = "0.12"
84+
atty = "0.2.14"
9685

9786
[dependencies.retry]
9887
default-features = false

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
@@ -9,10 +9,9 @@ use std::{cmp, env};
99
use anyhow::{anyhow, Context, Result};
1010
use git_testament::{git_testament, render_testament};
1111
use lazy_static::lazy_static;
12-
use term2::Terminal;
1312

1413
use super::self_update;
15-
use super::term2;
14+
use super::term;
1615
use crate::dist::notifications as dist_notifications;
1716
use crate::process;
1817
use crate::toolchain::DistributableToolchain;
@@ -195,7 +194,7 @@ fn show_channel_updates(cfg: &Cfg, toolchains: Vec<(String, Result<UpdateStatus>
195194
match result {
196195
Ok(UpdateStatus::Installed) => {
197196
banner = "installed";
198-
color = Some(term2::color::GREEN);
197+
color = Some(term::Color::Green);
199198
}
200199
Ok(UpdateStatus::Updated(v)) => {
201200
if name == "rustup" {
@@ -205,7 +204,7 @@ fn show_channel_updates(cfg: &Cfg, toolchains: Vec<(String, Result<UpdateStatus>
205204
previous_version = Some(v);
206205
}
207206
banner = "updated";
208-
color = Some(term2::color::GREEN);
207+
color = Some(term::Color::Green);
209208
}
210209
Ok(UpdateStatus::Unchanged) => {
211210
if name == "rustup" {
@@ -216,7 +215,7 @@ fn show_channel_updates(cfg: &Cfg, toolchains: Vec<(String, Result<UpdateStatus>
216215
}
217216
Err(_) => {
218217
banner = "update failed";
219-
color = Some(term2::color::RED);
218+
color = Some(term::Color::Red);
220219
}
221220
}
222221

@@ -225,7 +224,7 @@ fn show_channel_updates(cfg: &Cfg, toolchains: Vec<(String, Result<UpdateStatus>
225224
Ok((name, banner, width, color, version, previous_version))
226225
});
227226

228-
let mut t = term2::stdout();
227+
let mut t = term::stdout();
229228

230229
let data: Vec<_> = data.collect::<Result<_>>()?;
231230
let max_width = data
@@ -236,7 +235,7 @@ fn show_channel_updates(cfg: &Cfg, toolchains: Vec<(String, Result<UpdateStatus>
236235
let padding = max_width - width;
237236
let padding: String = " ".repeat(padding);
238237
let _ = write!(t, " {padding}");
239-
let _ = t.attr(term2::Attr::Bold);
238+
let _ = t.attr(term::Attr::Bold);
240239
if let Some(color) = color {
241240
let _ = t.fg(color);
242241
}
@@ -343,7 +342,7 @@ where
343342
}
344343

345344
pub(crate) fn list_targets(toolchain: &Toolchain<'_>) -> Result<utils::ExitCode> {
346-
let mut t = term2::stdout();
345+
let mut t = term::stdout();
347346
let distributable = DistributableToolchain::new_for_components(toolchain)?;
348347
let components = distributable.list_components()?;
349348
for component in components {
@@ -354,7 +353,7 @@ pub(crate) fn list_targets(toolchain: &Toolchain<'_>) -> Result<utils::ExitCode>
354353
.as_ref()
355354
.expect("rust-std should have a target");
356355
if component.installed {
357-
let _ = t.attr(term2::Attr::Bold);
356+
let _ = t.attr(term::Attr::Bold);
358357
let _ = writeln!(t, "{target} (installed)");
359358
let _ = t.reset();
360359
} else if component.available {
@@ -367,7 +366,7 @@ pub(crate) fn list_targets(toolchain: &Toolchain<'_>) -> Result<utils::ExitCode>
367366
}
368367

369368
pub(crate) fn list_installed_targets(toolchain: &Toolchain<'_>) -> Result<utils::ExitCode> {
370-
let mut t = term2::stdout();
369+
let mut t = term::stdout();
371370
let distributable = DistributableToolchain::new_for_components(toolchain)?;
372371
let components = distributable.list_components()?;
373372
for component in components {
@@ -386,13 +385,13 @@ pub(crate) fn list_installed_targets(toolchain: &Toolchain<'_>) -> Result<utils:
386385
}
387386

388387
pub(crate) fn list_components(toolchain: &Toolchain<'_>) -> Result<utils::ExitCode> {
389-
let mut t = term2::stdout();
388+
let mut t = term::stdout();
390389
let distributable = DistributableToolchain::new_for_components(toolchain)?;
391390
let components = distributable.list_components()?;
392391
for component in components {
393392
let name = component.name;
394393
if component.installed {
395-
t.attr(term2::Attr::Bold)?;
394+
t.attr(term::Attr::Bold)?;
396395
writeln!(t, "{name} (installed)")?;
397396
t.reset()?;
398397
} else if component.available {
@@ -404,7 +403,7 @@ pub(crate) fn list_components(toolchain: &Toolchain<'_>) -> Result<utils::ExitCo
404403
}
405404

406405
pub(crate) fn list_installed_components(toolchain: &Toolchain<'_>) -> Result<utils::ExitCode> {
407-
let mut t = term2::stdout();
406+
let mut t = term::stdout();
408407
let distributable = DistributableToolchain::new_for_components(toolchain)?;
409408
let components = distributable.list_components()?;
410409
for component in components {

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)