Skip to content

Commit e172502

Browse files
feat(download_tracker): use the custom ColorableTerminal and respect related env vars
With this commit, `RUSTUP_TERM_COLOR` and `RUSTUP_PROGRESS_WHEN` are now being honored.
1 parent bb0ff98 commit e172502

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/cli/download_tracker.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
1+
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
22
use std::time::Duration;
33

44
use crate::dist::Notification as In;
55
use crate::notifications::Notification;
6-
use crate::process::Process;
6+
use crate::process::{Process, terminalsource::ColorChoice};
77
use crate::utils::Notification as Un;
88

99
/// Tracks download progress and displays information about it to a terminal.
@@ -14,16 +14,35 @@ pub(crate) struct DownloadTracker {
1414
/// Whether we display progress or not.
1515
display_progress: bool,
1616
stdout_is_a_tty: bool,
17-
// Progress bar for the download.
17+
/// Whether we display colors or not.
18+
use_colors: bool,
19+
/// MultiProgress bar for the downloads.
20+
multi_progress_bars: MultiProgress,
21+
/// ProgressBar for the current download.
1822
progress_bar: ProgressBar,
1923
}
2024

2125
impl DownloadTracker {
2226
/// Creates a new DownloadTracker.
2327
pub(crate) fn new_with_display_progress(display_progress: bool, process: &Process) -> Self {
28+
let t = process.stdout().terminal(process);
29+
let is_a_tty = t.is_a_tty();
30+
let use_colors = matches!(t.color_choice(), ColorChoice::Auto | ColorChoice::Always);
31+
let multi_progress_bars = MultiProgress::with_draw_target(
32+
match process.var("RUSTUP_TERM_PROGRESS_WHEN") {
33+
Ok(s) if s.eq_ignore_ascii_case("always") => {
34+
ProgressDrawTarget::term_like(Box::new(t))
35+
}
36+
Ok(s) if s.eq_ignore_ascii_case("never") => ProgressDrawTarget::hidden(),
37+
_ if is_a_tty => ProgressDrawTarget::term_like(Box::new(t)),
38+
_ => ProgressDrawTarget::hidden(),
39+
}
40+
);
2441
Self {
2542
display_progress,
2643
stdout_is_a_tty: process.stdout().is_a_tty(process),
44+
use_colors,
45+
multi_progress_bars,
2746
progress_bar: ProgressBar::hidden(),
2847
}
2948
}
@@ -56,7 +75,11 @@ impl DownloadTracker {
5675
self.progress_bar.set_length(content_len);
5776
self.progress_bar.set_style(
5877
ProgressStyle::with_template(
59-
"[{elapsed_precise}] [{bar:40.blue}] {bytes}/{total_bytes} (ETA: {eta})",
78+
if self.use_colors {
79+
"[{elapsed_precise}] [{bar:40.blue}] {bytes}/{total_bytes} (ETA: {eta})"
80+
} else {
81+
"[{elapsed_precise}] [{bar:40}] {bytes}/{total_bytes} (ETA: {eta})"
82+
},
6083
)
6184
.unwrap()
6285
.tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ "),
@@ -70,15 +93,15 @@ impl DownloadTracker {
7093
&& self.stdout_is_a_tty
7194
&& self.progress_bar.elapsed() >= Duration::from_secs(1)
7295
{
73-
self.progress_bar
74-
.set_draw_target(ProgressDrawTarget::stdout());
96+
self.multi_progress_bars.add(self.progress_bar.clone());
7597
}
7698
self.progress_bar.inc(len as u64);
7799
}
78100

79101
/// Notifies self that the download has finished.
80102
pub(crate) fn download_finished(&mut self) {
81103
self.progress_bar.finish_and_clear();
104+
self.multi_progress_bars.remove(&self.progress_bar);
82105
self.progress_bar = ProgressBar::hidden();
83106
}
84107
}

0 commit comments

Comments
 (0)