diff options
author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-02-08 13:34:41 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-08 13:34:41 +0000 |
commit | 8460210202af4e48ea55c997bf2739a6f0570e4a (patch) | |
tree | b65b7ec55da171d9246be0d2ef77a7e39ecf45cb | |
parent | 199365310248eecedf8fbad579fc0cdc2e47bbe7 (diff) |
feat: add progress bars to sync and store init (#1684)
Replace lots of logging with some progress bars. This looks much nicer
I'd like to move it out of the atuin-client crate and into the atuin
crate. But first, I want to decouple a lot of the record moving, so it
can wait until that's done.
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | atuin-client/Cargo.toml | 1 | ||||
-rw-r--r-- | atuin-client/src/history/store.rs | 17 | ||||
-rw-r--r-- | atuin-client/src/record/sync.rs | 34 |
4 files changed, 36 insertions, 17 deletions
@@ -237,6 +237,7 @@ dependencies = [ "futures", "generic-array", "hex", + "indicatif", "interim", "itertools", "lazy_static", diff --git a/atuin-client/Cargo.toml b/atuin-client/Cargo.toml index 991c594e..9d1e8768 100644 --- a/atuin-client/Cargo.toml +++ b/atuin-client/Cargo.toml @@ -67,6 +67,7 @@ urlencoding = { version = "2.1.0", optional = true } reqwest = { workspace = true, optional = true } hex = { version = "0.4", optional = true } sha2 = { version = "0.10", optional = true } +indicatif = "0.17.7" [dev-dependencies] tokio = { version = "1", features = ["full"] } diff --git a/atuin-client/src/history/store.rs b/atuin-client/src/history/store.rs index 08c55c51..4a4cb1ab 100644 --- a/atuin-client/src/history/store.rs +++ b/atuin-client/src/history/store.rs @@ -1,6 +1,7 @@ -use std::collections::HashSet; +use std::{collections::HashSet, fmt::Write}; use eyre::{bail, eyre, Result}; +use indicatif::{ProgressBar, ProgressState, ProgressStyle}; use rmp::decode::Bytes; use crate::{ @@ -263,11 +264,17 @@ impl HistoryStore { println!("Fetching history already in store"); let store_ids = self.history_ids().await?; + let pb = ProgressBar::new(history.len() as u64); + pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {human_pos}/{human_len} ({eta})") + .unwrap() + .with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) + .progress_chars("#>-")); + for i in history { - println!("loaded {}", i.id); + debug!("loaded {}", i.id); if store_ids.contains(&i.id) { - println!("skipping {} - already exists", i.id); + debug!("skipping {} - already exists", i.id); continue; } @@ -277,8 +284,12 @@ impl HistoryStore { } else { self.push(i).await?; } + + pb.inc(1); } + pb.finish_with_message("Import complete"); + Ok(()) } } diff --git a/atuin-client/src/record/sync.rs b/atuin-client/src/record/sync.rs index 6c80b0b8..e3644371 100644 --- a/atuin-client/src/record/sync.rs +++ b/atuin-client/src/record/sync.rs @@ -1,5 +1,5 @@ // do a sync :O -use std::cmp::Ordering; +use std::{cmp::Ordering, fmt::Write}; use eyre::Result; use thiserror::Error; @@ -8,6 +8,7 @@ use super::store::Store; use crate::{api_client::Client, settings::Settings}; use atuin_common::record::{Diff, HostId, RecordId, RecordIdx, RecordStatus}; +use indicatif::{ProgressBar, ProgressState, ProgressStyle}; #[derive(Error, Debug)] pub enum SyncError { @@ -165,6 +166,12 @@ async fn sync_upload( let upload_page_size = 100; let mut progress = 0; + let pb = ProgressBar::new(expected); + pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {human_pos}/{human_len} ({eta})") + .unwrap() + .with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) + .progress_chars("#>-")); + println!( "Uploading {} records to {}/{}", expected, @@ -189,12 +196,7 @@ async fn sync_upload( SyncError::RemoteRequestError { msg: e.to_string() } })?; - println!( - "uploaded {} to remote, progress {}/{}", - page.len(), - progress, - expected - ); + pb.set_position(progress); progress += page.len() as u64; if progress >= expected { @@ -202,6 +204,8 @@ async fn sync_upload( } } + pb.finish_with_message("Uploaded records"); + Ok(progress as i64) } @@ -226,6 +230,12 @@ async fn sync_download( tag ); + let pb = ProgressBar::new(expected); + pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {human_pos}/{human_len} ({eta})") + .unwrap() + .with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) + .progress_chars("#>-")); + // preload with the first entry if remote does not know of this store loop { let page = client @@ -238,15 +248,9 @@ async fn sync_download( .await .map_err(|e| SyncError::LocalStoreError { msg: e.to_string() })?; - println!( - "downloaded {} records from remote, progress {}/{}", - page.len(), - progress, - expected - ); - ret.extend(page.iter().map(|f| f.id)); + pb.set_position(progress); progress += page.len() as u64; if progress >= expected { @@ -254,6 +258,8 @@ async fn sync_download( } } + pb.finish_with_message("Downloaded records"); + Ok(ret) } |