Skip to content

Commit c5b9258

Browse files
committed
Auto merge of #6233 - dtolnay:erase, r=alexcrichton
Use ANSI escape code to clear line This eliminates the trailing spaces that Cargo used to use for clearing a line of output. Trailing spaces are problematic because they take up space on the next line when reducing the width of a terminal.
2 parents efb7972 + d534875 commit c5b9258

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/cargo/core/shell.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ impl Shell {
120120
self.err.as_write()
121121
}
122122

123+
/// Erase from cursor to end of line.
124+
pub fn err_erase_line(&mut self) {
125+
if let ShellOut::Stream { tty: true, .. } = self.err {
126+
imp::err_erase_line(self);
127+
}
128+
}
129+
123130
/// Shortcut to right-align and color green a status message.
124131
pub fn status<T, U>(&mut self, status: T, message: U) -> CargoResult<()>
125132
where
@@ -332,6 +339,8 @@ mod imp {
332339

333340
use libc;
334341

342+
use super::Shell;
343+
335344
pub fn stderr_width() -> Option<usize> {
336345
unsafe {
337346
let mut winsize: libc::winsize = mem::zeroed();
@@ -345,10 +354,19 @@ mod imp {
345354
}
346355
}
347356
}
357+
358+
pub fn err_erase_line(shell: &mut Shell) {
359+
// This is the "EL - Erase in Line" sequence. It clears from the cursor
360+
// to the end of line.
361+
// https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
362+
let _ = shell.err().write_all(b"\x1B[K");
363+
}
348364
}
349365

350366
#[cfg(all(unix, not(any(target_os = "linux", target_os = "macos"))))]
351367
mod imp {
368+
pub(super) use super::default_err_erase_line as err_erase_line;
369+
352370
pub fn stderr_width() -> Option<usize> {
353371
None
354372
}
@@ -366,6 +384,8 @@ mod imp {
366384
use self::winapi::um::wincon::*;
367385
use self::winapi::um::winnt::*;
368386

387+
pub(super) use super::default_err_erase_line as err_erase_line;
388+
369389
pub fn stderr_width() -> Option<usize> {
370390
unsafe {
371391
let stdout = GetStdHandle(STD_ERROR_HANDLE);
@@ -408,3 +428,14 @@ mod imp {
408428
}
409429
}
410430
}
431+
432+
#[cfg(any(
433+
all(unix, not(any(target_os = "linux", target_os = "macos"))),
434+
windows,
435+
))]
436+
fn default_err_erase_line(shell: &mut Shell) {
437+
if let Some(max_width) = imp::stderr_width() {
438+
let blank = " ".repeat(max_width);
439+
drop(write!(shell.err(), "{}\r", blank));
440+
}
441+
}

src/cargo/util/progress.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ impl<'cfg> State<'cfg> {
196196
}
197197

198198
fn clear(&mut self) {
199-
self.try_update_max_width();
200-
let blank = " ".repeat(self.format.max_width);
201-
drop(write!(self.config.shell().err(), "{}\r", blank));
199+
self.config.shell().err_erase_line();
202200
}
203201

204202
fn try_update_max_width(&mut self) {

0 commit comments

Comments
 (0)