Skip to content

Commit febf4e8

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

File tree

16 files changed

+437
-531
lines changed

16 files changed

+437
-531
lines changed

Cargo.lock

Lines changed: 1 addition & 11 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ sharded-slab = "0.1.1"
7979
strsim = "0.10"
8080
tar = "0.4.26"
8181
tempfile.workspace = true
82-
term = "=0.5.1" # FIXME(issue #1818, #1826, and friends)
82+
termcolor = "1.2"
8383
thiserror.workspace = true
8484
threadpool = "1"
8585
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: 66 additions & 53 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;
@@ -27,7 +26,7 @@ use crate::{Cfg, Notification};
2726
pub(crate) const WARN_COMPLETE_PROFILE: &str = "downloading with complete profile isn't recommended unless you are a developer of the rust language";
2827

2928
pub(crate) fn confirm(question: &str, default: bool) -> Result<bool> {
30-
write!(process().stdout(), "{question} ")?;
29+
write!(process().stdout().lock(), "{question} ")?;
3130
let _ = std::io::stdout().flush();
3231
let input = read_line()?;
3332

@@ -38,7 +37,7 @@ pub(crate) fn confirm(question: &str, default: bool) -> Result<bool> {
3837
_ => false,
3938
};
4039

41-
writeln!(process().stdout())?;
40+
writeln!(process().stdout().lock())?;
4241

4342
Ok(r)
4443
}
@@ -50,11 +49,14 @@ pub(crate) enum Confirm {
5049
}
5150

5251
pub(crate) fn confirm_advanced() -> Result<Confirm> {
53-
writeln!(process().stdout())?;
54-
writeln!(process().stdout(), "1) Proceed with installation (default)")?;
55-
writeln!(process().stdout(), "2) Customize installation")?;
56-
writeln!(process().stdout(), "3) Cancel installation")?;
57-
write!(process().stdout(), ">")?;
52+
writeln!(process().stdout().lock())?;
53+
writeln!(
54+
process().stdout().lock(),
55+
"1) Proceed with installation (default)"
56+
)?;
57+
writeln!(process().stdout().lock(), "2) Customize installation")?;
58+
writeln!(process().stdout().lock(), "3) Cancel installation")?;
59+
write!(process().stdout().lock(), ">")?;
5860

5961
let _ = std::io::stdout().flush();
6062
let input = read_line()?;
@@ -65,17 +67,17 @@ pub(crate) fn confirm_advanced() -> Result<Confirm> {
6567
_ => Confirm::No,
6668
};
6769

68-
writeln!(process().stdout())?;
70+
writeln!(process().stdout().lock())?;
6971

7072
Ok(r)
7173
}
7274

7375
pub(crate) fn question_str(question: &str, default: &str) -> Result<String> {
74-
writeln!(process().stdout(), "{question} [{default}]")?;
76+
writeln!(process().stdout().lock(), "{question} [{default}]")?;
7577
let _ = std::io::stdout().flush();
7678
let input = read_line()?;
7779

78-
writeln!(process().stdout())?;
80+
writeln!(process().stdout().lock())?;
7981

8082
if input.is_empty() {
8183
Ok(default.to_string())
@@ -86,12 +88,12 @@ pub(crate) fn question_str(question: &str, default: &str) -> Result<String> {
8688

8789
pub(crate) fn question_bool(question: &str, default: bool) -> Result<bool> {
8890
let default_text = if default { "(Y/n)" } else { "(y/N)" };
89-
writeln!(process().stdout(), "{question} {default_text}")?;
91+
writeln!(process().stdout().lock(), "{question} {default_text}")?;
9092

9193
let _ = std::io::stdout().flush();
9294
let input = read_line()?;
9395

94-
writeln!(process().stdout())?;
96+
writeln!(process().stdout().lock())?;
9597

9698
if input.is_empty() {
9799
Ok(default)
@@ -207,10 +209,10 @@ fn show_channel_updates(
207209
) -> Result<()> {
208210
let data = updates.into_iter().map(|(pkg, result)| {
209211
let (banner, color) = match &result {
210-
Ok(UpdateStatus::Installed) => ("installed", Some(term2::color::GREEN)),
211-
Ok(UpdateStatus::Updated(_)) => ("updated", Some(term2::color::GREEN)),
212+
Ok(UpdateStatus::Installed) => ("installed", Some(term::Color::Green)),
213+
Ok(UpdateStatus::Updated(_)) => ("updated", Some(term::Color::Green)),
212214
Ok(UpdateStatus::Unchanged) => ("unchanged", None),
213-
Err(_) => ("update failed", Some(term2::color::RED)),
215+
Err(_) => ("update failed", Some(term::Color::Red)),
214216
};
215217

216218
let (previous_version, version) = match &pkg {
@@ -248,7 +250,7 @@ fn show_channel_updates(
248250
Ok((pkg, banner, width, color, version, previous_version))
249251
});
250252

251-
let mut t = term2::stdout();
253+
let mut t = process().stdout();
252254

253255
let data: Vec<_> = data.collect::<Result<_>>()?;
254256
let max_width = data
@@ -258,20 +260,20 @@ fn show_channel_updates(
258260
for (pkg, banner, width, color, version, previous_version) in data {
259261
let padding = max_width - width;
260262
let padding: String = " ".repeat(padding);
261-
let _ = write!(t, " {padding}");
262-
let _ = t.attr(term2::Attr::Bold);
263+
let _ = write!(t.lock(), " {padding}");
264+
let _ = t.attr(term::Attr::Bold);
263265
if let Some(color) = color {
264266
let _ = t.fg(color);
265267
}
266-
let _ = write!(t, "{pkg} {banner}");
268+
let _ = write!(t.lock(), "{pkg} {banner}");
267269
let _ = t.reset();
268-
let _ = write!(t, " - {version}");
270+
let _ = write!(t.lock(), " - {version}");
269271
if let Some(previous_version) = previous_version {
270-
let _ = write!(t, " (from {previous_version})");
272+
let _ = write!(t.lock(), " (from {previous_version})");
271273
}
272-
let _ = writeln!(t);
274+
let _ = writeln!(t.lock());
273275
}
274-
let _ = writeln!(t);
276+
let _ = writeln!(t.lock());
275277

276278
Ok(())
277279
}
@@ -289,7 +291,7 @@ pub(crate) fn update_all_channels(
289291

290292
let show_channel_updates = || {
291293
if !toolchains.is_empty() {
292-
writeln!(process().stdout())?;
294+
writeln!(process().stdout().lock())?;
293295

294296
let t = toolchains
295297
.into_iter()
@@ -369,7 +371,7 @@ where
369371
}
370372

371373
pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<utils::ExitCode> {
372-
let mut t = term2::stdout();
374+
let mut t = process().stdout();
373375
let manifestation = distributable.get_manifestation()?;
374376
let config = manifestation.read_config()?.unwrap_or_default();
375377
let manifest = distributable.get_manifest()?;
@@ -382,11 +384,11 @@ pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<
382384
.as_ref()
383385
.expect("rust-std should have a target");
384386
if component.installed {
385-
let _ = t.attr(term2::Attr::Bold);
386-
let _ = writeln!(t, "{target} (installed)");
387+
let _ = t.attr(term::Attr::Bold);
388+
let _ = writeln!(t.lock(), "{target} (installed)");
387389
let _ = t.reset();
388390
} else if component.available {
389-
let _ = writeln!(t, "{target}");
391+
let _ = writeln!(t.lock(), "{target}");
390392
}
391393
}
392394
}
@@ -397,7 +399,9 @@ pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<
397399
pub(crate) fn list_installed_targets(
398400
distributable: DistributableToolchain<'_>,
399401
) -> Result<utils::ExitCode> {
400-
let mut t = term2::stdout();
402+
let t = process().stdout();
403+
let mut t_lock = t.lock();
404+
401405
let manifestation = distributable.get_manifestation()?;
402406
let config = manifestation.read_config()?.unwrap_or_default();
403407
let manifest = distributable.get_manifest()?;
@@ -410,7 +414,7 @@ pub(crate) fn list_installed_targets(
410414
.as_ref()
411415
.expect("rust-std should have a target");
412416
if component.installed {
413-
writeln!(t, "{target}")?;
417+
writeln!(t_lock, "{target}")?;
414418
}
415419
}
416420
}
@@ -420,35 +424,36 @@ pub(crate) fn list_installed_targets(
420424
pub(crate) fn list_components(
421425
distributable: DistributableToolchain<'_>,
422426
) -> Result<utils::ExitCode> {
423-
let mut t = term2::stdout();
427+
let mut t = process().stdout();
428+
424429
let manifestation = distributable.get_manifestation()?;
425430
let config = manifestation.read_config()?.unwrap_or_default();
426431
let manifest = distributable.get_manifest()?;
427432
let components = manifest.query_components(distributable.desc(), &config)?;
428433
for component in components {
429434
let name = component.name;
430435
if component.installed {
431-
t.attr(term2::Attr::Bold)?;
432-
writeln!(t, "{name} (installed)")?;
436+
t.attr(term::Attr::Bold)?;
437+
writeln!(t.lock(), "{name} (installed)")?;
433438
t.reset()?;
434439
} else if component.available {
435-
writeln!(t, "{name}")?;
440+
writeln!(t.lock(), "{name}")?;
436441
}
437442
}
438443

439444
Ok(utils::ExitCode(0))
440445
}
441446

442447
pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_>) -> Result<()> {
443-
let mut t = term2::stdout();
448+
let t = process().stdout();
444449
let manifestation = distributable.get_manifestation()?;
445450
let config = manifestation.read_config()?.unwrap_or_default();
446451
let manifest = distributable.get_manifest()?;
447452
let components = manifest.query_components(distributable.desc(), &config)?;
448453

449454
for component in components {
450455
if component.installed {
451-
writeln!(t, "{}", component.name)?;
456+
writeln!(t.lock(), "{}", component.name)?;
452457
}
453458
}
454459
Ok(())
@@ -473,7 +478,7 @@ fn print_toolchain_path(
473478
String::new()
474479
};
475480
writeln!(
476-
process().stdout(),
481+
process().stdout().lock(),
477482
"{}{}{}{}",
478483
&toolchain,
479484
if_default,
@@ -491,7 +496,7 @@ pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCod
491496
.map(Into::into)
492497
.collect::<Vec<_>>();
493498
if toolchains.is_empty() {
494-
writeln!(process().stdout(), "no installed toolchains")?;
499+
writeln!(process().stdout().lock(), "no installed toolchains")?;
495500
} else {
496501
let def_toolchain_name = cfg.get_default()?.map(|t| (&t).into());
497502
let cwd = utils::current_dir()?;
@@ -529,7 +534,7 @@ pub(crate) fn list_overrides(cfg: &Cfg) -> Result<utils::ExitCode> {
529534
let overrides = cfg.settings_file.with(|s| Ok(s.overrides.clone()))?;
530535

531536
if overrides.is_empty() {
532-
writeln!(process().stdout(), "no overrides")?;
537+
writeln!(process().stdout().lock(), "no overrides")?;
533538
} else {
534539
let mut any_not_exist = false;
535540
for (k, v) in overrides {
@@ -538,15 +543,15 @@ pub(crate) fn list_overrides(cfg: &Cfg) -> Result<utils::ExitCode> {
538543
any_not_exist = true;
539544
}
540545
writeln!(
541-
process().stdout(),
546+
process().stdout().lock(),
542547
"{:<40}\t{:<20}",
543548
utils::format_path_for_display(&k)
544549
+ if dir_exists { "" } else { " (not a directory)" },
545550
v
546551
)?
547552
}
548553
if any_not_exist {
549-
writeln!(process().stdout())?;
554+
writeln!(process().stdout().lock())?;
550555
info!(
551556
"you may remove overrides for non-existent directories with
552557
`rustup override unset --nonexistent`"
@@ -571,43 +576,51 @@ pub(crate) fn version() -> &'static str {
571576
pub(crate) fn dump_testament() -> Result<utils::ExitCode> {
572577
use git_testament::GitModification::*;
573578
writeln!(
574-
process().stdout(),
579+
process().stdout().lock(),
575580
"Rustup version renders as: {}",
576581
version()
577582
)?;
578583
writeln!(
579-
process().stdout(),
584+
process().stdout().lock(),
580585
"Current crate version: {}",
581586
env!("CARGO_PKG_VERSION")
582587
)?;
583588
if TESTAMENT.branch_name.is_some() {
584589
writeln!(
585-
process().stdout(),
590+
process().stdout().lock(),
586591
"Built from branch: {}",
587592
TESTAMENT.branch_name.unwrap()
588593
)?;
589594
} else {
590-
writeln!(process().stdout(), "Branch information missing")?;
595+
writeln!(process().stdout().lock(), "Branch information missing")?;
591596
}
592-
writeln!(process().stdout(), "Commit info: {}", TESTAMENT.commit)?;
597+
writeln!(
598+
process().stdout().lock(),
599+
"Commit info: {}",
600+
TESTAMENT.commit
601+
)?;
593602
if TESTAMENT.modifications.is_empty() {
594-
writeln!(process().stdout(), "Working tree is clean")?;
603+
writeln!(process().stdout().lock(), "Working tree is clean")?;
595604
} else {
596605
for fmod in TESTAMENT.modifications {
597606
match fmod {
598-
Added(f) => writeln!(process().stdout(), "Added: {}", String::from_utf8_lossy(f))?,
607+
Added(f) => writeln!(
608+
process().stdout().lock(),
609+
"Added: {}",
610+
String::from_utf8_lossy(f)
611+
)?,
599612
Removed(f) => writeln!(
600-
process().stdout(),
613+
process().stdout().lock(),
601614
"Removed: {}",
602615
String::from_utf8_lossy(f)
603616
)?,
604617
Modified(f) => writeln!(
605-
process().stdout(),
618+
process().stdout().lock(),
606619
"Modified: {}",
607620
String::from_utf8_lossy(f)
608621
)?,
609622
Untracked(f) => writeln!(
610-
process().stdout(),
623+
process().stdout().lock(),
611624
"Untracked: {}",
612625
String::from_utf8_lossy(f)
613626
)?,

0 commit comments

Comments
 (0)