Skip to content

Commit fe360bb

Browse files
authored
Merge pull request #1834 from rbtcollins/tracker-term-use
Use term2 from download_tracker
2 parents ed65847 + 0839cc7 commit fe360bb

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/cli/download_tracker.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use crate::term2;
12
use rustup::dist::Notification as In;
23
use rustup::utils::tty;
34
use rustup::utils::Notification as Un;
45
use rustup::Notification;
56
use std::collections::VecDeque;
67
use std::fmt;
8+
use std::io::Write;
9+
use term::Terminal;
710
use time::precise_time_s;
811

912
/// Keep track of this many past download amounts
@@ -25,7 +28,10 @@ pub struct DownloadTracker {
2528
/// How many seconds have elapsed since the download started
2629
seconds_elapsed: u32,
2730
/// The terminal we write the information to.
28-
term: Option<Box<term::StdoutTerminal>>,
31+
/// XXX: Could be a term trait, but with #1818 on the horizon that
32+
/// is a pointless change to make - better to let that transition
33+
/// happen and take stock after that.
34+
term: term2::StdoutTerminal,
2935
/// Whether we displayed progress for the download or not.
3036
///
3137
/// If the download is quick enough, we don't have time to
@@ -47,7 +53,7 @@ impl DownloadTracker {
4753
downloaded_last_few_secs: VecDeque::with_capacity(DOWNLOAD_TRACK_COUNT),
4854
seconds_elapsed: 0,
4955
last_sec: None,
50-
term: term::stdout(),
56+
term: term2::stdout(),
5157
displayed_charcount: None,
5258
}
5359
}
@@ -60,7 +66,7 @@ impl DownloadTracker {
6066
true
6167
}
6268
Notification::Install(In::Utils(Un::DownloadDataReceived(data))) => {
63-
if tty::stdout_isatty() && self.term.is_some() {
69+
if tty::stdout_isatty() {
6470
self.data_received(data.len());
6571
}
6672
true
@@ -108,7 +114,7 @@ impl DownloadTracker {
108114
if self.displayed_charcount.is_some() {
109115
// Display the finished state
110116
self.display();
111-
let _ = writeln!(self.term.as_mut().unwrap());
117+
let _ = writeln!(self.term);
112118
}
113119
self.prepare_for_new_download();
114120
}
@@ -134,7 +140,7 @@ impl DownloadTracker {
134140
let speed_h = HumanReadable(speed);
135141

136142
// First, move to the start of the current line and clear it.
137-
let _ = self.term.as_mut().unwrap().carriage_return();
143+
let _ = self.term.carriage_return();
138144
// We'd prefer to use delete_line() but on Windows it seems to
139145
// sometimes do unusual things
140146
// let _ = self.term.as_mut().unwrap().delete_line();
@@ -143,9 +149,9 @@ impl DownloadTracker {
143149
// This is not ideal as very narrow terminals might mess up,
144150
// but it is more likely to succeed until term's windows console
145151
// fixes whatever's up with delete_line().
146-
let _ = write!(self.term.as_mut().unwrap(), "{}", " ".repeat(n));
147-
let _ = self.term.as_mut().unwrap().flush();
148-
let _ = self.term.as_mut().unwrap().carriage_return();
152+
let _ = write!(self.term, "{}", " ".repeat(n));
153+
let _ = self.term.flush();
154+
let _ = self.term.carriage_return();
149155
}
150156

151157
let output = match self.content_len {
@@ -163,9 +169,9 @@ impl DownloadTracker {
163169
None => format!("Total: {} Speed: {}/s", total_h, speed_h),
164170
};
165171

166-
let _ = write!(self.term.as_mut().unwrap(), "{}", output);
172+
let _ = write!(self.term, "{}", output);
167173
// Since stdout is typically line-buffered and we don't print a newline, we manually flush.
168-
let _ = self.term.as_mut().unwrap().flush();
174+
let _ = self.term.flush();
169175
self.displayed_charcount = Some(output.chars().count());
170176
}
171177
}

0 commit comments

Comments
 (0)