Skip to content

Commit 506536c

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 506536c

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/cli/download_tracker.rs

Lines changed: 28 additions & 8 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,34 @@ 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 =
32+
MultiProgress::with_draw_target(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+
});
2440
Self {
2541
display_progress,
2642
stdout_is_a_tty: process.stdout().is_a_tty(process),
43+
use_colors,
44+
multi_progress_bars,
2745
progress_bar: ProgressBar::hidden(),
2846
}
2947
}
@@ -55,9 +73,11 @@ impl DownloadTracker {
5573
pub(crate) fn content_length_received(&mut self, content_len: u64) {
5674
self.progress_bar.set_length(content_len);
5775
self.progress_bar.set_style(
58-
ProgressStyle::with_template(
59-
"[{elapsed_precise}] [{bar:40.blue}] {bytes}/{total_bytes} (ETA: {eta})",
60-
)
76+
ProgressStyle::with_template(if self.use_colors {
77+
"[{elapsed_precise}] [{bar:40.blue}] {bytes}/{total_bytes} (ETA: {eta})"
78+
} else {
79+
"[{elapsed_precise}] [{bar:40}] {bytes}/{total_bytes} (ETA: {eta})"
80+
})
6181
.unwrap()
6282
.tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ "),
6383
);
@@ -70,15 +90,15 @@ impl DownloadTracker {
7090
&& self.stdout_is_a_tty
7191
&& self.progress_bar.elapsed() >= Duration::from_secs(1)
7292
{
73-
self.progress_bar
74-
.set_draw_target(ProgressDrawTarget::stdout());
93+
self.multi_progress_bars.add(self.progress_bar.clone());
7594
}
7695
self.progress_bar.inc(len as u64);
7796
}
7897

7998
/// Notifies self that the download has finished.
8099
pub(crate) fn download_finished(&mut self) {
81100
self.progress_bar.finish_and_clear();
101+
self.multi_progress_bars.remove(&self.progress_bar);
82102
self.progress_bar = ProgressBar::hidden();
83103
}
84104
}

0 commit comments

Comments
 (0)