From 57a177e2f946390559a1f17787c5a15d23ac3393 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 10 Sep 2020 12:39:07 +0200 Subject: Show progress bar in fill output This patch uses the progressing crate to display a progress bar for the fill command if the output is printed to a TTY. --- src/commands.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/commands.rs') diff --git a/src/commands.rs b/src/commands.rs index 9af1853..ced976c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -23,6 +23,7 @@ use nitrokey::GetPasswordSafe; use crate::args; use crate::config; +use crate::output; use crate::pinentry; use crate::Context; @@ -482,20 +483,20 @@ pub fn fill(ctx: &mut Context<'_>) -> anyhow::Result<()> { device.fill_sd_card(&pin).context("Failed to fill SD card") })?; - let mut last_progress = 0; - loop { + let mut progress_bar = output::ProgressBar::new(); + progress_bar.draw(ctx)?; + + while !progress_bar.is_finished() { + thread::sleep(time::Duration::from_secs(1)); + let status = device .get_operation_status() .context("Failed to query operation status")?; match status { - nitrokey::OperationStatus::Ongoing(progress) => { - if last_progress != progress { - println!(ctx, "{}/100", progress)?; - } - last_progress = progress; - } - nitrokey::OperationStatus::Idle => break, + nitrokey::OperationStatus::Ongoing(progress) => progress_bar.update(progress)?, + nitrokey::OperationStatus::Idle => progress_bar.finish(), }; + progress_bar.draw(ctx)?; } Ok(()) -- cgit v1.2.1