From 5b49ad5bd5d78a8425f7162b6387c2cad6e2ef10 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 27 Nov 2019 04:09:30 +0800 Subject: Use a simple progress bar instead of indicatif --- Cargo.lock | 26 -------- crates/ra_cli/Cargo.toml | 1 - crates/ra_cli/src/analysis_stats.rs | 17 ++--- crates/ra_cli/src/main.rs | 1 + crates/ra_cli/src/progress_bar.rs | 130 ++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 37 deletions(-) create mode 100644 crates/ra_cli/src/progress_bar.rs diff --git a/Cargo.lock b/Cargo.lock index 1c6161504..f751be2e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,7 +226,6 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -472,17 +471,6 @@ dependencies = [ "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "indicatif" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "console 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "number_prefix 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "inotify" version = "0.6.1" @@ -759,11 +747,6 @@ dependencies = [ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "number_prefix" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "once_cell" version = "1.2.0" @@ -945,7 +928,6 @@ name = "ra_cli" version = "0.1.0" dependencies = [ "flexi_logger 0.14.5 (registry+https://github.com/rust-lang/crates.io-index)", - "indicatif 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", "pico-args 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "ra_batch 0.1.0", "ra_db 0.1.0", @@ -1718,11 +1700,6 @@ name = "unicode-segmentation" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unicode-width" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "unicode-xid" version = "0.2.0" @@ -1899,7 +1876,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum hermit-abi 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "307c3c9f937f38e3534b1d6447ecf090cafcc9744e4a6360e8b037b2cf5af120" "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" "checksum indexmap 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d7b3ea5827fcb9d4fda14bf4da5f136f0db2ae9c8f4bd4e2d1c6fde4e6db2" -"checksum indicatif 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8572bccfb0665e70b7faf44ee28841b8e0823450cd4ad562a76b5a3c4bf48487" "checksum inotify 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40b54539f3910d6f84fbf9a643efd6e3aa6e4f001426c0329576128255994718" "checksum inotify-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e74a1aa87c59aeff6ef2cc2fa62d41bc43f54952f55652656b18a02fd5e356c0" "checksum insta 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d499dc062e841590a67230d853bce62d0abeb91304927871670b7c55c461349" @@ -1932,7 +1908,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" "checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4" "checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72" -"checksum number_prefix 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a" "checksum once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "891f486f630e5c5a4916c7e16c4b24a53e78c860b646e9f8e005e4f16847bfed" "checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063" "checksum parking_lot 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc" @@ -2008,7 +1983,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b561e267b2326bb4cebfc0ef9e68355c7abe6c6f522aeac2f5bf95d56c59bdcf" "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" -"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75b414f6c464c879d7f9babf951f23bc3743fb7313c081b2e6ca719067ea9d61" "checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" diff --git a/crates/ra_cli/Cargo.toml b/crates/ra_cli/Cargo.toml index c9d3bdb77..3bb475997 100644 --- a/crates/ra_cli/Cargo.toml +++ b/crates/ra_cli/Cargo.toml @@ -8,7 +8,6 @@ publish = false [dependencies] pico-args = "0.3.0" flexi_logger = "0.14.0" -indicatif = "0.13.0" ra_syntax = { path = "../ra_syntax" } ra_ide_api = { path = "../ra_ide_api" } diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index c4eb28245..8bf3863bb 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs @@ -6,7 +6,7 @@ use ra_db::SourceDatabaseExt; use ra_hir::{AssocItem, Crate, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk}; use ra_syntax::AstNode; -use crate::{Result, Verbosity}; +use crate::{progress_bar::ProgressBar, Result, Verbosity}; pub fn run( verbosity: Verbosity, @@ -75,17 +75,14 @@ pub fn run( println!("Item Collection: {:?}, {}", analysis_time.elapsed(), ra_prof::memory_usage()); let inference_time = Instant::now(); - let bar = match verbosity { - Verbosity::Verbose | Verbosity::Normal => indicatif::ProgressBar::with_draw_target( - funcs.len() as u64, - indicatif::ProgressDrawTarget::stderr_nohz(), - ), - Verbosity::Quiet => indicatif::ProgressBar::hidden(), + let mut bar = match verbosity { + Verbosity::Verbose | Verbosity::Normal => ProgressBar::new(funcs.len() as u64), + Verbosity::Quiet => ProgressBar::hidden(), }; - bar.set_style( - indicatif::ProgressStyle::default_bar().template("{wide_bar} {pos}/{len}\n{msg}"), - ); + // bar.set_style( + // indicatif::ProgressStyle::default_bar().template("{wide_bar} {pos}/{len}\n{msg}"), + // ); bar.tick(); let mut num_exprs = 0; let mut num_exprs_unknown = 0; diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index a31fd5d6a..c23d92ec2 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -3,6 +3,7 @@ mod analysis_stats; mod analysis_bench; mod help; +mod progress_bar; use std::{error::Error, fmt::Write, io::Read}; diff --git a/crates/ra_cli/src/progress_bar.rs b/crates/ra_cli/src/progress_bar.rs new file mode 100644 index 000000000..95bfcf4d0 --- /dev/null +++ b/crates/ra_cli/src/progress_bar.rs @@ -0,0 +1,130 @@ +//! A simple progress bar +//! +//! A single thread non-optimized progress bar +use std::io::Write; + +/// A Simple ASCII Progress Bar +pub struct ProgressBar { + curr: f32, + text: String, + anim: usize, + hidden: bool, + + len: u64, + pos: u64, + msg: String, +} + +impl ProgressBar { + const ANIMATION: &'static str = r#"|/-\"#; + const BLOCK_COUNT: usize = 20; + + pub fn new(len: u64) -> ProgressBar { + ProgressBar { + curr: 0.0, + text: String::new(), + anim: 0, + hidden: false, + len, + pos: 0, + msg: String::new(), + } + } + + pub fn hidden() -> ProgressBar { + ProgressBar { + curr: 0.0, + text: String::new(), + anim: 0, + hidden: true, + len: 0, + pos: 0, + msg: String::new(), + } + } + + pub fn set_message(&mut self, msg: &str) { + self.msg = msg.to_string(); + self.tick(); + } + + pub fn println>(&mut self, msg: I) { + self.clear(); + println!("{}", msg.into()); + self.tick(); + } + + pub fn inc(&mut self, delta: u64) { + self.pos += delta; + if self.len == 0 { + self.set_value(0.0) + } else { + self.set_value((self.pos as f32) / (self.len as f32)) + } + self.tick(); + } + + pub fn finish_and_clear(&mut self) { + self.clear(); + } + + pub fn tick(&mut self) { + if self.hidden { + return; + } + + let progress_block: usize = (self.curr * Self::BLOCK_COUNT as f32) as usize; + let percent = (self.curr * 100.0) as u32; + let text = format!( + "[{}{}] {:3>}% {} {}", + "#".repeat(progress_block), + "-".repeat(Self::BLOCK_COUNT - progress_block), + percent, + Self::ANIMATION.chars().nth(self.anim).unwrap(), + self.msg, + ); + + self.anim = (self.anim + 1) % Self::ANIMATION.len(); + self.update_text(&text); + } + + fn update_text(&mut self, text: &str) { + // Get length of common portion + let mut common_prefix_length = 0; + let common_length = usize::min(self.text.len(), text.len()); + + while common_prefix_length < common_length + && text.chars().nth(common_prefix_length).unwrap() + == self.text.chars().nth(common_prefix_length).unwrap() + { + common_prefix_length += 1; + } + + // Backtrack to the first differing character + let mut output = String::new(); + output += &'\x08'.to_string().repeat(self.text.len() - common_prefix_length); + // Output new suffix + output += &text[common_prefix_length..text.len()]; + + // If the new text is shorter than the old one: delete overlapping characters + if let Some(overlap_count) = self.text.len().checked_sub(text.len()) { + if overlap_count > 0 { + output += &" ".repeat(overlap_count); + output += &"\x08".repeat(overlap_count); + } + } + + let _ = std::io::stdout().write(output.as_bytes()); + let _ = std::io::stdout().flush(); + self.text = text.to_string(); + } + + fn set_value(&mut self, value: f32) { + self.curr = f32::max(0.0, f32::min(1.0, value)); + } + + fn clear(&mut self) { + print!("{}", "\x08".repeat(self.text.len())); + self.text = String::new(); + } +} -- cgit v1.2.3 From 97f6f141ee06ddfb22f8c02223fa71102b670528 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 27 Nov 2019 04:16:03 +0800 Subject: Remove commented code --- crates/ra_cli/src/analysis_stats.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index 8bf3863bb..122aa0552 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs @@ -80,9 +80,6 @@ pub fn run( Verbosity::Quiet => ProgressBar::hidden(), }; - // bar.set_style( - // indicatif::ProgressStyle::default_bar().template("{wide_bar} {pos}/{len}\n{msg}"), - // ); bar.tick(); let mut num_exprs = 0; let mut num_exprs_unknown = 0; -- cgit v1.2.3 From 27943bead6798cd202d7398f38cecfd3f5840645 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 27 Nov 2019 04:33:40 +0800 Subject: Remove progress bar and add a true counter --- crates/ra_cli/src/analysis_stats.rs | 6 +- crates/ra_cli/src/main.rs | 2 +- crates/ra_cli/src/progress_bar.rs | 130 ----------------------------------- crates/ra_cli/src/progress_report.rs | 113 ++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 134 deletions(-) delete mode 100644 crates/ra_cli/src/progress_bar.rs create mode 100644 crates/ra_cli/src/progress_report.rs diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index 122aa0552..9b1802a5f 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs @@ -6,7 +6,7 @@ use ra_db::SourceDatabaseExt; use ra_hir::{AssocItem, Crate, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk}; use ra_syntax::AstNode; -use crate::{progress_bar::ProgressBar, Result, Verbosity}; +use crate::{progress_report::ProgressReport, Result, Verbosity}; pub fn run( verbosity: Verbosity, @@ -76,8 +76,8 @@ pub fn run( let inference_time = Instant::now(); let mut bar = match verbosity { - Verbosity::Verbose | Verbosity::Normal => ProgressBar::new(funcs.len() as u64), - Verbosity::Quiet => ProgressBar::hidden(), + Verbosity::Verbose | Verbosity::Normal => ProgressReport::new(funcs.len() as u64), + Verbosity::Quiet => ProgressReport::hidden(), }; bar.tick(); diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index c23d92ec2..08f353147 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -3,7 +3,7 @@ mod analysis_stats; mod analysis_bench; mod help; -mod progress_bar; +mod progress_report; use std::{error::Error, fmt::Write, io::Read}; diff --git a/crates/ra_cli/src/progress_bar.rs b/crates/ra_cli/src/progress_bar.rs deleted file mode 100644 index 95bfcf4d0..000000000 --- a/crates/ra_cli/src/progress_bar.rs +++ /dev/null @@ -1,130 +0,0 @@ -//! A simple progress bar -//! -//! A single thread non-optimized progress bar -use std::io::Write; - -/// A Simple ASCII Progress Bar -pub struct ProgressBar { - curr: f32, - text: String, - anim: usize, - hidden: bool, - - len: u64, - pos: u64, - msg: String, -} - -impl ProgressBar { - const ANIMATION: &'static str = r#"|/-\"#; - const BLOCK_COUNT: usize = 20; - - pub fn new(len: u64) -> ProgressBar { - ProgressBar { - curr: 0.0, - text: String::new(), - anim: 0, - hidden: false, - len, - pos: 0, - msg: String::new(), - } - } - - pub fn hidden() -> ProgressBar { - ProgressBar { - curr: 0.0, - text: String::new(), - anim: 0, - hidden: true, - len: 0, - pos: 0, - msg: String::new(), - } - } - - pub fn set_message(&mut self, msg: &str) { - self.msg = msg.to_string(); - self.tick(); - } - - pub fn println>(&mut self, msg: I) { - self.clear(); - println!("{}", msg.into()); - self.tick(); - } - - pub fn inc(&mut self, delta: u64) { - self.pos += delta; - if self.len == 0 { - self.set_value(0.0) - } else { - self.set_value((self.pos as f32) / (self.len as f32)) - } - self.tick(); - } - - pub fn finish_and_clear(&mut self) { - self.clear(); - } - - pub fn tick(&mut self) { - if self.hidden { - return; - } - - let progress_block: usize = (self.curr * Self::BLOCK_COUNT as f32) as usize; - let percent = (self.curr * 100.0) as u32; - let text = format!( - "[{}{}] {:3>}% {} {}", - "#".repeat(progress_block), - "-".repeat(Self::BLOCK_COUNT - progress_block), - percent, - Self::ANIMATION.chars().nth(self.anim).unwrap(), - self.msg, - ); - - self.anim = (self.anim + 1) % Self::ANIMATION.len(); - self.update_text(&text); - } - - fn update_text(&mut self, text: &str) { - // Get length of common portion - let mut common_prefix_length = 0; - let common_length = usize::min(self.text.len(), text.len()); - - while common_prefix_length < common_length - && text.chars().nth(common_prefix_length).unwrap() - == self.text.chars().nth(common_prefix_length).unwrap() - { - common_prefix_length += 1; - } - - // Backtrack to the first differing character - let mut output = String::new(); - output += &'\x08'.to_string().repeat(self.text.len() - common_prefix_length); - // Output new suffix - output += &text[common_prefix_length..text.len()]; - - // If the new text is shorter than the old one: delete overlapping characters - if let Some(overlap_count) = self.text.len().checked_sub(text.len()) { - if overlap_count > 0 { - output += &" ".repeat(overlap_count); - output += &"\x08".repeat(overlap_count); - } - } - - let _ = std::io::stdout().write(output.as_bytes()); - let _ = std::io::stdout().flush(); - self.text = text.to_string(); - } - - fn set_value(&mut self, value: f32) { - self.curr = f32::max(0.0, f32::min(1.0, value)); - } - - fn clear(&mut self) { - print!("{}", "\x08".repeat(self.text.len())); - self.text = String::new(); - } -} diff --git a/crates/ra_cli/src/progress_report.rs b/crates/ra_cli/src/progress_report.rs new file mode 100644 index 000000000..6ce0d4cab --- /dev/null +++ b/crates/ra_cli/src/progress_report.rs @@ -0,0 +1,113 @@ +//! A simple progress bar +//! +//! A single thread non-optimized progress bar +use std::io::Write; + +/// A Simple ASCII Progress Bar +pub struct ProgressReport { + curr: f32, + text: String, + hidden: bool, + + len: u64, + pos: u64, + msg: String, +} + +impl ProgressReport { + pub fn new(len: u64) -> ProgressReport { + ProgressReport { + curr: 0.0, + text: String::new(), + hidden: false, + len, + pos: 0, + msg: String::new(), + } + } + + pub fn hidden() -> ProgressReport { + ProgressReport { + curr: 0.0, + text: String::new(), + hidden: true, + len: 0, + pos: 0, + msg: String::new(), + } + } + + pub fn set_message(&mut self, msg: &str) { + self.msg = msg.to_string(); + self.tick(); + } + + pub fn println>(&mut self, msg: I) { + self.clear(); + println!("{}", msg.into()); + self.tick(); + } + + pub fn inc(&mut self, delta: u64) { + self.pos += delta; + if self.len == 0 { + self.set_value(0.0) + } else { + self.set_value((self.pos as f32) / (self.len as f32)) + } + self.tick(); + } + + pub fn finish_and_clear(&mut self) { + self.clear(); + } + + pub fn tick(&mut self) { + if self.hidden { + return; + } + let percent = (self.curr * 100.0) as u32; + let text = format!("{}/{} {:3>}% {}", self.pos, self.len, percent, self.msg); + self.update_text(&text); + } + + fn update_text(&mut self, text: &str) { + // Get length of common portion + let mut common_prefix_length = 0; + let common_length = usize::min(self.text.len(), text.len()); + + while common_prefix_length < common_length + && text.chars().nth(common_prefix_length).unwrap() + == self.text.chars().nth(common_prefix_length).unwrap() + { + common_prefix_length += 1; + } + + // Backtrack to the first differing character + let mut output = String::new(); + output += &'\x08'.to_string().repeat(self.text.len() - common_prefix_length); + // Output new suffix + output += &text[common_prefix_length..text.len()]; + + // If the new text is shorter than the old one: delete overlapping characters + if let Some(overlap_count) = self.text.len().checked_sub(text.len()) { + if overlap_count > 0 { + output += &" ".repeat(overlap_count); + output += &"\x08".repeat(overlap_count); + } + } + + let _ = std::io::stdout().write(output.as_bytes()); + let _ = std::io::stdout().flush(); + self.text = text.to_string(); + } + + fn set_value(&mut self, value: f32) { + self.curr = f32::max(0.0, f32::min(1.0, value)); + } + + fn clear(&mut self) { + print!("{}{}", " ".repeat(self.text.len()), "\x08".repeat(self.text.len())); + self.text = String::new(); + } +} -- cgit v1.2.3 From f0aaf3b2964a26a08be13dcf04f31d5bfe46b2f3 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 27 Nov 2019 05:01:13 +0800 Subject: Fill all last text to space and return the cursor --- crates/ra_cli/src/progress_report.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/ra_cli/src/progress_report.rs b/crates/ra_cli/src/progress_report.rs index 6ce0d4cab..31867a1e9 100644 --- a/crates/ra_cli/src/progress_report.rs +++ b/crates/ra_cli/src/progress_report.rs @@ -107,7 +107,14 @@ impl ProgressReport { } fn clear(&mut self) { - print!("{}{}", " ".repeat(self.text.len()), "\x08".repeat(self.text.len())); + if self.hidden { + return; + } + + // Fill all last text to space and return the cursor + let spaces = " ".repeat(self.text.len()); + let backspaces = "\x08".repeat(self.text.len()); + print!("{}{}{}", backspaces, spaces, backspaces); self.text = String::new(); } } -- cgit v1.2.3