From a8d74728a32d2bd2d7d392c2e7308870bd281b46 Mon Sep 17 00:00:00 2001 From: Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> Date: Mon, 28 Nov 2022 19:07:07 +0100 Subject: [PATCH 1/5] Emit progress state escape codes Emit progress state escape codes when drawing progress bar. --- src/cargo/util/progress.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/cargo/util/progress.rs b/src/cargo/util/progress.rs index ac19e874db3..9d34f0c0d7e 100644 --- a/src/cargo/util/progress.rs +++ b/src/cargo/util/progress.rs @@ -39,6 +39,25 @@ struct Format { max_print: usize, } +enum ProgressCode { + None, + Normal(u8), +} + +impl std::fmt::Display for ProgressCode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let progress = match self { + Self::None => 0, + Self::Normal(v) => *v, + }; + let state = match self { + Self::None => 0, + Self::Normal(_) => 1, + }; + write!(f, "\x1b]9;4;{state};{progress}\x1b\\") + } +} + impl<'cfg> Progress<'cfg> { pub fn with_style(name: &str, style: ProgressStyle, cfg: &'cfg Config) -> Progress<'cfg> { // report no progress when -q (for quiet) or TERM=dumb are set @@ -183,6 +202,7 @@ impl Throttle { impl<'cfg> State<'cfg> { fn tick(&mut self, cur: usize, max: usize, msg: &str) -> CargoResult<()> { if self.done { + write!(self.config.shell().err(), "{}", ProgressCode::None)?; return Ok(()); } @@ -195,6 +215,12 @@ impl<'cfg> State<'cfg> { self.try_update_max_width(); if let Some(pbar) = self.format.progress(cur, max) { self.print(&pbar, msg)?; + let prog = (cur as f32) / (max as f32) * 100.0; + write!( + self.config.shell().err(), + "{}", + ProgressCode::Normal(prog as u8) + )?; } Ok(()) } From 7ed089b553d65e2102ab822da9e7c916c4ecc3ed Mon Sep 17 00:00:00 2001 From: Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> Date: Mon, 28 Nov 2022 20:40:03 +0100 Subject: [PATCH 2/5] Reduce flicker --- src/cargo/util/progress.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cargo/util/progress.rs b/src/cargo/util/progress.rs index 9d34f0c0d7e..b18ebdcd57f 100644 --- a/src/cargo/util/progress.rs +++ b/src/cargo/util/progress.rs @@ -31,6 +31,7 @@ struct State<'cfg> { throttle: Throttle, last_line: Option, fixed_width: Option, + last_pbar: u8, } struct Format { @@ -100,6 +101,7 @@ impl<'cfg> Progress<'cfg> { throttle: Throttle::new(), last_line: None, fixed_width: progress_config.width, + last_pbar: 255, }), } } @@ -214,13 +216,12 @@ impl<'cfg> State<'cfg> { // return back to the beginning of the line for the next print. self.try_update_max_width(); if let Some(pbar) = self.format.progress(cur, max) { + let prog = ((cur as f32) / (max as f32) * 100.0) as u8; + if self.last_pbar != prog { + write!(self.config.shell().err(), "{}", ProgressCode::Normal(prog))?; + self.last_pbar = prog; + } self.print(&pbar, msg)?; - let prog = (cur as f32) / (max as f32) * 100.0; - write!( - self.config.shell().err(), - "{}", - ProgressCode::Normal(prog as u8) - )?; } Ok(()) } From 8e76c7e65ac64581314731c15fbd032cd294740b Mon Sep 17 00:00:00 2001 From: Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> Date: Wed, 21 Dec 2022 13:40:30 +0100 Subject: [PATCH 3/5] Apply review suggestions --- src/cargo/util/progress.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/progress.rs b/src/cargo/util/progress.rs index b18ebdcd57f..4521db3c0d7 100644 --- a/src/cargo/util/progress.rs +++ b/src/cargo/util/progress.rs @@ -40,6 +40,9 @@ struct Format { max_print: usize, } +/// Which escape code to use for progress reporting. +/// +/// There is more codes, but we only use these two. enum ProgressCode { None, Normal(u8), @@ -101,7 +104,7 @@ impl<'cfg> Progress<'cfg> { throttle: Throttle::new(), last_line: None, fixed_width: progress_config.width, - last_pbar: 255, + last_pbar: u8::MAX, }), } } From b60dc0d838faebe2ec9d775467a3e4ce142c1fd1 Mon Sep 17 00:00:00 2001 From: Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> Date: Wed, 21 Dec 2022 14:38:07 +0100 Subject: [PATCH 4/5] Signal end of progress when cleaning --- src/cargo/util/progress.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/cargo/util/progress.rs b/src/cargo/util/progress.rs index 4521db3c0d7..94a61731b14 100644 --- a/src/cargo/util/progress.rs +++ b/src/cargo/util/progress.rs @@ -41,7 +41,7 @@ struct Format { } /// Which escape code to use for progress reporting. -/// +/// /// There is more codes, but we only use these two. enum ProgressCode { None, @@ -50,13 +50,9 @@ enum ProgressCode { impl std::fmt::Display for ProgressCode { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let progress = match self { - Self::None => 0, - Self::Normal(v) => *v, - }; - let state = match self { - Self::None => 0, - Self::Normal(_) => 1, + let (state, progress) = match self { + Self::None => (0, 0), + Self::Normal(v) => (1, *v), }; write!(f, "\x1b]9;4;{state};{progress}\x1b\\") } @@ -262,6 +258,8 @@ impl<'cfg> State<'cfg> { if self.last_line.is_some() && !self.config.shell().is_cleared() { self.config.shell().err_erase_line(); self.last_line = None; + let _ = write!(self.config.shell().err(), "{}", ProgressCode::None); + self.last_pbar = u8::MAX; } } From 6fa572eece99bff71e0fc474975ac7cbaf3c8862 Mon Sep 17 00:00:00 2001 From: Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> Date: Wed, 21 Dec 2022 18:48:29 +0100 Subject: [PATCH 5/5] Restrict to `cfg(windows)` only --- src/cargo/util/progress.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/cargo/util/progress.rs b/src/cargo/util/progress.rs index 94a61731b14..5ef95159881 100644 --- a/src/cargo/util/progress.rs +++ b/src/cargo/util/progress.rs @@ -31,6 +31,7 @@ struct State<'cfg> { throttle: Throttle, last_line: Option, fixed_width: Option, + #[cfg(windows)] // Windows is the only currently supported platform last_pbar: u8, } @@ -43,11 +44,13 @@ struct Format { /// Which escape code to use for progress reporting. /// /// There is more codes, but we only use these two. +#[cfg(windows)] // Windows is the only currently supported platform enum ProgressCode { None, Normal(u8), } +#[cfg(windows)] // Windows is the only currently supported platform impl std::fmt::Display for ProgressCode { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let (state, progress) = match self { @@ -100,6 +103,7 @@ impl<'cfg> Progress<'cfg> { throttle: Throttle::new(), last_line: None, fixed_width: progress_config.width, + #[cfg(windows)] // Windows is the only currently supported platform last_pbar: u8::MAX, }), } @@ -203,6 +207,7 @@ impl Throttle { impl<'cfg> State<'cfg> { fn tick(&mut self, cur: usize, max: usize, msg: &str) -> CargoResult<()> { if self.done { + #[cfg(windows)] // Windows is the only currently supported platform write!(self.config.shell().err(), "{}", ProgressCode::None)?; return Ok(()); } @@ -215,10 +220,13 @@ impl<'cfg> State<'cfg> { // return back to the beginning of the line for the next print. self.try_update_max_width(); if let Some(pbar) = self.format.progress(cur, max) { - let prog = ((cur as f32) / (max as f32) * 100.0) as u8; - if self.last_pbar != prog { - write!(self.config.shell().err(), "{}", ProgressCode::Normal(prog))?; - self.last_pbar = prog; + #[cfg(windows)] // Windows is the only currently supported platform + { + let prog = ((cur as f32) / (max as f32) * 100.0) as u8; + if self.last_pbar != prog { + write!(self.config.shell().err(), "{}", ProgressCode::Normal(prog))?; + self.last_pbar = prog; + } } self.print(&pbar, msg)?; } @@ -258,8 +266,11 @@ impl<'cfg> State<'cfg> { if self.last_line.is_some() && !self.config.shell().is_cleared() { self.config.shell().err_erase_line(); self.last_line = None; - let _ = write!(self.config.shell().err(), "{}", ProgressCode::None); - self.last_pbar = u8::MAX; + #[cfg(windows)] // Windows is the only currently supported platform + { + let _ = write!(self.config.shell().err(), "{}", ProgressCode::None); + self.last_pbar = u8::MAX; + } } }