Skip to content

Commit 93b08e4

Browse files
committed
Use lock
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent 4b3d9e7 commit 93b08e4

File tree

11 files changed

+176
-167
lines changed

11 files changed

+176
-167
lines changed

src/cli/common.rs

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{Cfg, Notification};
2626
pub(crate) const WARN_COMPLETE_PROFILE: &str = "downloading with complete profile isn't recommended unless you are a developer of the rust language";
2727

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

@@ -37,7 +37,7 @@ pub(crate) fn confirm(question: &str, default: bool) -> Result<bool> {
3737
_ => false,
3838
};
3939

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

4242
Ok(r)
4343
}
@@ -49,11 +49,14 @@ pub(crate) enum Confirm {
4949
}
5050

5151
pub(crate) fn confirm_advanced() -> Result<Confirm> {
52-
writeln!(process().stdout())?;
53-
writeln!(process().stdout(), "1) Proceed with installation (default)")?;
54-
writeln!(process().stdout(), "2) Customize installation")?;
55-
writeln!(process().stdout(), "3) Cancel installation")?;
56-
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(), ">")?;
5760

5861
let _ = std::io::stdout().flush();
5962
let input = read_line()?;
@@ -64,17 +67,17 @@ pub(crate) fn confirm_advanced() -> Result<Confirm> {
6467
_ => Confirm::No,
6568
};
6669

67-
writeln!(process().stdout())?;
70+
writeln!(process().stdout().lock())?;
6871

6972
Ok(r)
7073
}
7174

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

77-
writeln!(process().stdout())?;
80+
writeln!(process().stdout().lock())?;
7881

7982
if input.is_empty() {
8083
Ok(default.to_string())
@@ -85,12 +88,12 @@ pub(crate) fn question_str(question: &str, default: &str) -> Result<String> {
8588

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

9093
let _ = std::io::stdout().flush();
9194
let input = read_line()?;
9295

93-
writeln!(process().stdout())?;
96+
writeln!(process().stdout().lock())?;
9497

9598
if input.is_empty() {
9699
Ok(default)
@@ -257,20 +260,20 @@ fn show_channel_updates(
257260
for (pkg, banner, width, color, version, previous_version) in data {
258261
let padding = max_width - width;
259262
let padding: String = " ".repeat(padding);
260-
let _ = write!(t, " {padding}");
263+
let _ = write!(t.lock(), " {padding}");
261264
let _ = t.attr(term::Attr::Bold);
262265
if let Some(color) = color {
263266
let _ = t.fg(color);
264267
}
265-
let _ = write!(t, "{pkg} {banner}");
268+
let _ = write!(t.lock(), "{pkg} {banner}");
266269
let _ = t.reset();
267-
let _ = write!(t, " - {version}");
270+
let _ = write!(t.lock(), " - {version}");
268271
if let Some(previous_version) = previous_version {
269-
let _ = write!(t, " (from {previous_version})");
272+
let _ = write!(t.lock(), " (from {previous_version})");
270273
}
271-
let _ = writeln!(t);
274+
let _ = writeln!(t.lock());
272275
}
273-
let _ = writeln!(t);
276+
let _ = writeln!(t.lock());
274277

275278
Ok(())
276279
}
@@ -288,7 +291,7 @@ pub(crate) fn update_all_channels(
288291

289292
let show_channel_updates = || {
290293
if !toolchains.is_empty() {
291-
writeln!(process().stdout())?;
294+
writeln!(process().stdout().lock())?;
292295

293296
let t = toolchains
294297
.into_iter()
@@ -382,10 +385,10 @@ pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<
382385
.expect("rust-std should have a target");
383386
if component.installed {
384387
let _ = t.attr(term::Attr::Bold);
385-
let _ = writeln!(t, "{target} (installed)");
388+
let _ = writeln!(t.lock(), "{target} (installed)");
386389
let _ = t.reset();
387390
} else if component.available {
388-
let _ = writeln!(t, "{target}");
391+
let _ = writeln!(t.lock(), "{target}");
389392
}
390393
}
391394
}
@@ -396,7 +399,9 @@ pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<
396399
pub(crate) fn list_installed_targets(
397400
distributable: DistributableToolchain<'_>,
398401
) -> Result<utils::ExitCode> {
399-
let mut t = term::stdout();
402+
let t = term::stdout();
403+
let mut t_lock = t.lock();
404+
400405
let manifestation = distributable.get_manifestation()?;
401406
let config = manifestation.read_config()?.unwrap_or_default();
402407
let manifest = distributable.get_manifest()?;
@@ -409,7 +414,7 @@ pub(crate) fn list_installed_targets(
409414
.as_ref()
410415
.expect("rust-std should have a target");
411416
if component.installed {
412-
writeln!(t, "{target}")?;
417+
writeln!(t_lock, "{target}")?;
413418
}
414419
}
415420
}
@@ -420,6 +425,7 @@ pub(crate) fn list_components(
420425
distributable: DistributableToolchain<'_>,
421426
) -> Result<utils::ExitCode> {
422427
let mut t = term::stdout();
428+
423429
let manifestation = distributable.get_manifestation()?;
424430
let config = manifestation.read_config()?.unwrap_or_default();
425431
let manifest = distributable.get_manifest()?;
@@ -428,26 +434,26 @@ pub(crate) fn list_components(
428434
let name = component.name;
429435
if component.installed {
430436
t.attr(term::Attr::Bold)?;
431-
writeln!(t, "{name} (installed)")?;
437+
writeln!(t.lock(), "{name} (installed)")?;
432438
t.reset()?;
433439
} else if component.available {
434-
writeln!(t, "{name}")?;
440+
writeln!(t.lock(), "{name}")?;
435441
}
436442
}
437443

438444
Ok(utils::ExitCode(0))
439445
}
440446

441447
pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_>) -> Result<()> {
442-
let mut t = term::stdout();
448+
let t = term::stdout();
443449
let manifestation = distributable.get_manifestation()?;
444450
let config = manifestation.read_config()?.unwrap_or_default();
445451
let manifest = distributable.get_manifest()?;
446452
let components = manifest.query_components(distributable.desc(), &config)?;
447453

448454
for component in components {
449455
if component.installed {
450-
writeln!(t, "{}", component.name)?;
456+
writeln!(t.lock(), "{}", component.name)?;
451457
}
452458
}
453459
Ok(())
@@ -472,7 +478,7 @@ fn print_toolchain_path(
472478
String::new()
473479
};
474480
writeln!(
475-
process().stdout(),
481+
process().stdout().lock(),
476482
"{}{}{}{}",
477483
&toolchain,
478484
if_default,
@@ -490,7 +496,7 @@ pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCod
490496
.map(Into::into)
491497
.collect::<Vec<_>>();
492498
if toolchains.is_empty() {
493-
writeln!(process().stdout(), "no installed toolchains")?;
499+
writeln!(process().stdout().lock(), "no installed toolchains")?;
494500
} else {
495501
let def_toolchain_name = cfg.get_default()?.map(|t| (&t).into());
496502
let cwd = utils::current_dir()?;
@@ -528,7 +534,7 @@ pub(crate) fn list_overrides(cfg: &Cfg) -> Result<utils::ExitCode> {
528534
let overrides = cfg.settings_file.with(|s| Ok(s.overrides.clone()))?;
529535

530536
if overrides.is_empty() {
531-
writeln!(process().stdout(), "no overrides")?;
537+
writeln!(process().stdout().lock(), "no overrides")?;
532538
} else {
533539
let mut any_not_exist = false;
534540
for (k, v) in overrides {
@@ -537,15 +543,15 @@ pub(crate) fn list_overrides(cfg: &Cfg) -> Result<utils::ExitCode> {
537543
any_not_exist = true;
538544
}
539545
writeln!(
540-
process().stdout(),
546+
process().stdout().lock(),
541547
"{:<40}\t{:<20}",
542548
utils::format_path_for_display(&k)
543549
+ if dir_exists { "" } else { " (not a directory)" },
544550
v
545551
)?
546552
}
547553
if any_not_exist {
548-
writeln!(process().stdout())?;
554+
writeln!(process().stdout().lock())?;
549555
info!(
550556
"you may remove overrides for non-existent directories with
551557
`rustup override unset --nonexistent`"
@@ -570,43 +576,51 @@ pub(crate) fn version() -> &'static str {
570576
pub(crate) fn dump_testament() -> Result<utils::ExitCode> {
571577
use git_testament::GitModification::*;
572578
writeln!(
573-
process().stdout(),
579+
process().stdout().lock(),
574580
"Rustup version renders as: {}",
575581
version()
576582
)?;
577583
writeln!(
578-
process().stdout(),
584+
process().stdout().lock(),
579585
"Current crate version: {}",
580586
env!("CARGO_PKG_VERSION")
581587
)?;
582588
if TESTAMENT.branch_name.is_some() {
583589
writeln!(
584-
process().stdout(),
590+
process().stdout().lock(),
585591
"Built from branch: {}",
586592
TESTAMENT.branch_name.unwrap()
587593
)?;
588594
} else {
589-
writeln!(process().stdout(), "Branch information missing")?;
595+
writeln!(process().stdout().lock(), "Branch information missing")?;
590596
}
591-
writeln!(process().stdout(), "Commit info: {}", TESTAMENT.commit)?;
597+
writeln!(
598+
process().stdout().lock(),
599+
"Commit info: {}",
600+
TESTAMENT.commit
601+
)?;
592602
if TESTAMENT.modifications.is_empty() {
593-
writeln!(process().stdout(), "Working tree is clean")?;
603+
writeln!(process().stdout().lock(), "Working tree is clean")?;
594604
} else {
595605
for fmod in TESTAMENT.modifications {
596606
match fmod {
597-
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+
)?,
598612
Removed(f) => writeln!(
599-
process().stdout(),
613+
process().stdout().lock(),
600614
"Removed: {}",
601615
String::from_utf8_lossy(f)
602616
)?,
603617
Modified(f) => writeln!(
604-
process().stdout(),
618+
process().stdout().lock(),
605619
"Modified: {}",
606620
String::from_utf8_lossy(f)
607621
)?,
608622
Untracked(f) => writeln!(
609-
process().stdout(),
623+
process().stdout().lock(),
610624
"Untracked: {}",
611625
String::from_utf8_lossy(f)
612626
)?,

src/cli/download_tracker.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl DownloadTracker {
132132
if self.displayed_charcount.is_some() {
133133
// Display the finished state
134134
self.display();
135-
let _ = writeln!(self.term);
135+
let _ = writeln!(self.term.lock());
136136
}
137137
self.prepare_for_new_download();
138138
}
@@ -171,8 +171,8 @@ impl DownloadTracker {
171171
// This is not ideal as very narrow terminals might mess up,
172172
// but it is more likely to succeed until term's windows console
173173
// fixes whatever's up with delete_line().
174-
let _ = write!(self.term, "{}", " ".repeat(n));
175-
let _ = self.term.flush();
174+
let _ = write!(self.term.lock(), "{}", " ".repeat(n));
175+
let _ = self.term.lock().flush();
176176
let _ = self.term.carriage_return();
177177
}
178178

@@ -204,9 +204,9 @@ impl DownloadTracker {
204204
),
205205
};
206206

207-
let _ = write!(self.term, "{output}");
207+
let _ = write!(self.term.lock(), "{output}");
208208
// Since stdout is typically line-buffered and we don't print a newline, we manually flush.
209-
let _ = self.term.flush();
209+
let _ = self.term.lock().flush();
210210
self.displayed_charcount = Some(output.chars().count());
211211
}
212212
}

src/cli/log.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,49 @@ pub(crate) fn warn_fmt(args: fmt::Arguments<'_>) {
2626
let mut t = term::stderr();
2727
let _ = t.fg(term::Color::Yellow);
2828
let _ = t.attr(term::Attr::Bold);
29-
let _ = write!(t, "warning: ");
29+
let _ = write!(t.lock(), "warning: ");
3030
let _ = t.reset();
31-
let _ = t.write_fmt(args);
32-
let _ = writeln!(t);
31+
let _ = t.lock().write_fmt(args);
32+
let _ = writeln!(t.lock());
3333
}
3434

3535
pub(crate) fn err_fmt(args: fmt::Arguments<'_>) {
3636
let mut t = term::stderr();
3737
let _ = t.fg(term::Color::Red);
3838
let _ = t.attr(term::Attr::Bold);
39-
let _ = write!(t, "error: ");
39+
let _ = write!(t.lock(), "error: ");
4040
let _ = t.reset();
41-
let _ = t.write_fmt(args);
42-
let _ = writeln!(t);
41+
let _ = t.lock().write_fmt(args);
42+
let _ = writeln!(t.lock());
4343
}
4444

4545
pub(crate) fn info_fmt(args: fmt::Arguments<'_>) {
4646
let mut t = term::stderr();
4747
let _ = t.attr(term::Attr::Bold);
48-
let _ = write!(t, "info: ");
48+
let _ = write!(t.lock(), "info: ");
4949
let _ = t.reset();
50-
let _ = t.write_fmt(args);
51-
let _ = writeln!(t);
50+
let _ = t.lock().write_fmt(args);
51+
let _ = writeln!(t.lock());
5252
}
5353

5454
pub(crate) fn verbose_fmt(args: fmt::Arguments<'_>) {
5555
let mut t = term::stderr();
5656
let _ = t.fg(term::Color::Magenta);
5757
let _ = t.attr(term::Attr::Bold);
58-
let _ = write!(t, "verbose: ");
58+
let _ = write!(t.lock(), "verbose: ");
5959
let _ = t.reset();
60-
let _ = t.write_fmt(args);
61-
let _ = writeln!(t);
60+
let _ = t.lock().write_fmt(args);
61+
let _ = writeln!(t.lock());
6262
}
6363

6464
pub(crate) fn debug_fmt(args: fmt::Arguments<'_>) {
6565
if process().var("RUSTUP_DEBUG").is_ok() {
6666
let mut t = term::stderr();
6767
let _ = t.fg(term::Color::Blue);
6868
let _ = t.attr(term::Attr::Bold);
69-
let _ = write!(t, "debug: ");
69+
let _ = write!(t.lock(), "debug: ");
7070
let _ = t.reset();
71-
let _ = t.write_fmt(args);
72-
let _ = writeln!(t);
71+
let _ = t.lock().write_fmt(args);
72+
let _ = writeln!(t.lock());
7373
}
7474
}

0 commit comments

Comments
 (0)