From e0210b0b0382ae33b763407827ec188ea70f248e Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Fri, 24 Mar 2023 08:26:55 +0100 Subject: [PATCH] Add --no-progress option --- changelog/new.txt | 2 ++ src/commands/helpers.rs | 30 +++++++++++++++++++++--------- src/commands/mod.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/changelog/new.txt b/changelog/new.txt index 8853b0b..6db2e02 100644 --- a/changelog/new.txt +++ b/changelog/new.txt @@ -16,5 +16,7 @@ New features: - Support for extended file attributes has been added. - REST/Rclone backend: Allow to set the request timeout. - Extra or wrong fields in the config file now lead to rustic complaining and aborting. +- New option --no-progress has been added. +- Option --progress-interval can now also be given as command argument and in the config file. - backup: Paths are now sanitized from command arguments and config file before matching and applying the configuration. - check --read-data: progress bar now also shows total bytes to check and ETA. diff --git a/src/commands/helpers.rs b/src/commands/helpers.rs index 2426cce..4266aaf 100644 --- a/src/commands/helpers.rs +++ b/src/commands/helpers.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use std::fmt::Write; use std::process::Command; -use std::str::FromStr; +use std::sync::Mutex; use std::time::Duration; use anyhow::Result; @@ -11,6 +11,7 @@ use comfy_table::{ }; use indicatif::HumanDuration; use indicatif::{ProgressBar, ProgressState, ProgressStyle}; +use lazy_static::lazy_static; use log::*; use rayon::ThreadPoolBuilder; @@ -22,18 +23,23 @@ pub fn bytes(b: u64) -> String { ByteSize(b).to_string_as(true) } +lazy_static! { + pub static ref PROGRESS_INTERVAL: Mutex = Mutex::new(Duration::from_millis(100)); + pub static ref NO_PROGRESS: Mutex = Mutex::new(false); +} + fn progress_intervall() -> Duration { - let env_name = "RUSTIC_PROGRESS_INTERVAL"; - std::env::var(env_name) - .map(|var| { - humantime::Duration::from_str(&var) - .expect("{env_name}: please provide a valid duration") - .into() - }) - .unwrap_or(Duration::from_millis(100)) + *PROGRESS_INTERVAL.lock().unwrap() +} + +fn is_no_progress() -> bool { + *NO_PROGRESS.lock().unwrap() } pub fn progress_spinner(prefix: impl Into>) -> ProgressBar { + if is_no_progress() { + return no_progress(); + } let p = ProgressBar::new(0).with_style( ProgressStyle::default_bar() .template("[{elapsed_precise}] {prefix:30} {spinner}") @@ -45,6 +51,9 @@ pub fn progress_spinner(prefix: impl Into>) -> ProgressBar { } pub fn progress_counter(prefix: impl Into>) -> ProgressBar { + if is_no_progress() { + return no_progress(); + } let p = ProgressBar::new(0).with_style( ProgressStyle::default_bar() .template("[{elapsed_precise}] {prefix:30} {bar:40.cyan/blue} {pos:>10}/{len:10}") @@ -60,6 +69,9 @@ pub fn no_progress() -> ProgressBar { } pub fn progress_bytes(prefix: impl Into>) -> ProgressBar { + if is_no_progress() { + return no_progress(); + } let p = ProgressBar::new(0).with_style( ProgressStyle::default_bar() .with_key("my_eta", |s: &ProgressState, w: &mut dyn Write| diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 4473f19..41227e9 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -75,6 +75,22 @@ struct GlobalOpts { /// Note: warnings and errors are still additionally printed unless they are ignored by --log-level #[clap(long, global = true, env = "RUSTIC_LOG_FILE", value_name = "LOGFILE")] log_file: Option, + + /// Don't show any progress bar + #[clap(long, global = true, env = "RUSTIC_NO_PROGRESS")] + #[merge(strategy=merge::bool::overwrite_false)] + no_progress: bool, + + /// Interval to update progress bars + #[clap( + long, + global = true, + env = "RUSTIC_PROGRESS_INTERVAL", + value_name = "DURATION", + conflicts_with = "no-progress" + )] + #[serde_as(as = "Option")] + progress_interval: Option, } #[derive(Subcommand)] @@ -183,6 +199,16 @@ pub fn execute() -> Result<()> { ])?, } + if opts.no_progress { + let mut no_progress = NO_PROGRESS.lock().unwrap(); + *no_progress = true; + } + + if let Some(duration) = opts.progress_interval { + let mut interval = PROGRESS_INTERVAL.lock().unwrap(); + *interval = *duration; + } + if let Command::SelfUpdate(opts) = args.command { self_update::execute(opts)?; return Ok(());