Migrate to indicatif 0.7 beta

Now there is no need for the ugly hack where we drove the spinner
in a separate thread :)
This commit is contained in:
Zhaofeng Li 2021-11-16 21:01:33 -08:00
parent 7d15d08d6d
commit f716daa3a1
3 changed files with 11 additions and 45 deletions

10
Cargo.lock generated
View file

@ -162,9 +162,7 @@ dependencies = [
"encode_unicode", "encode_unicode",
"libc", "libc",
"once_cell", "once_cell",
"regex",
"terminal_size", "terminal_size",
"unicode-width",
"winapi", "winapi",
] ]
@ -359,9 +357,9 @@ checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed"
[[package]] [[package]]
name = "indicatif" name = "indicatif"
version = "0.15.0" version = "0.17.0-beta.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7baab56125e25686df467fe470785512329883aab42696d661247aca2a2896e4" checksum = "500f7e5a63596852b9bf7583fe86f9ad08e0df9b4eb58d12e9729071cb4952ca"
dependencies = [ dependencies = [
"console 0.15.0", "console 0.15.0",
"lazy_static", "lazy_static",
@ -457,9 +455,9 @@ dependencies = [
[[package]] [[package]]
name = "number_prefix" name = "number_prefix"
version = "0.3.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a" checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]] [[package]]
name = "once_cell" name = "once_cell"

View file

@ -15,7 +15,7 @@ env_logger = "0.8.2"
futures = "0.3.8" futures = "0.3.8"
glob = "0.3.0" glob = "0.3.0"
hostname = "0.3.1" hostname = "0.3.1"
indicatif = "0.15.0" indicatif = "0.17.0-beta.1"
libc = "0.2.81" libc = "0.2.81"
log = "0.4.11" log = "0.4.11"
quit = "1.1.2" quit = "1.1.2"

View file

@ -2,13 +2,9 @@
use std::future::Future; use std::future::Future;
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::Duration;
use atty::Stream; use atty::Stream;
use console::Style; use console::Style;
use futures::join;
use indicatif::{ use indicatif::{
MultiProgress, MultiProgress,
@ -79,7 +75,7 @@ impl Progress {
if let Some(multi) = self.multi.as_ref() { if let Some(multi) = self.multi.as_ref() {
let bar = multi.add(IndicatifBar::new(100)); let bar = multi.add(IndicatifBar::new(100));
let (style, _) = get_spinner_styles(self.label_width); let (style, _) = get_spinner_styles(self.label_width);
bar.set_prefix(&label); bar.set_prefix(label);
bar.set_style(style); bar.set_style(style);
bar.enable_steady_tick(100); bar.enable_steady_tick(100);
@ -93,37 +89,9 @@ impl Progress {
pub async fn run<F: Future, U>(self: Arc<Self>, func: U) -> F::Output pub async fn run<F: Future, U>(self: Arc<Self>, func: U) -> F::Output
where U: FnOnce(Arc<Progress>) -> F where U: FnOnce(Arc<Progress>) -> F
{ {
if let Some(multi) = self.multi.as_ref() { // TODO: Remove this - Previous trick no longer required in indicatif 0.7
let multi = multi.clone();
let finished = Arc::new(AtomicBool::new(false));
let redraw_future = {
let finished = finished.clone();
tokio::task::spawn_blocking(move || {
while !finished.load(Ordering::SeqCst) {
multi.join().unwrap();
thread::sleep(Duration::from_millis(100));
}
multi.join().unwrap();
})
};
let func_future = {
let finished = finished.clone();
async move {
let result = func(self).await;
finished.store(true, Ordering::SeqCst);
// root_bar.finish_and_clear();
result
}
};
let (func_result, _) = join!(func_future, redraw_future);
func_result
} else {
// Plain output. Simple.
func(self.clone()).await func(self.clone()).await
} }
}
fn init_multi() -> MultiProgress { fn init_multi() -> MultiProgress {
let multi = MultiProgress::new(); let multi = MultiProgress::new();
@ -176,7 +144,7 @@ impl TaskProgress {
} }
if let Some(bar) = self.bar.as_ref() { if let Some(bar) = self.bar.as_ref() {
bar.set_message(message); bar.set_message(message.to_owned());
} else { } else {
let style = Style::new().bold(); let style = Style::new().bold();
self.plain_print(style, message); self.plain_print(style, message);
@ -190,7 +158,7 @@ impl TaskProgress {
} }
if let Some(bar) = self.bar.as_ref() { if let Some(bar) = self.bar.as_ref() {
bar.finish_with_message(message); bar.finish_with_message(message.to_owned());
} else { } else {
let style = Style::new().bold().green(); let style = Style::new().bold().green();
self.plain_print(style, message); self.plain_print(style, message);
@ -217,7 +185,7 @@ impl TaskProgress {
if let Some(bar) = self.bar.as_ref() { if let Some(bar) = self.bar.as_ref() {
let (_, fail_style) = get_spinner_styles(self.label_width); let (_, fail_style) = get_spinner_styles(self.label_width);
bar.set_style(fail_style); bar.set_style(fail_style);
bar.abandon_with_message(message); bar.abandon_with_message(message.to_owned());
} else { } else {
let style = Style::new().bold().red(); let style = Style::new().bold().red();
self.plain_print(style, message); self.plain_print(style, message);