diff --git a/Cargo.toml b/Cargo.toml index 076b9aa..1b146e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ zip = { version = "0.6", default-features = false, features = ["time"], optional either = { version = "1", optional = true } reqwest = { version = "0.11", default-features = false, features = ["blocking", "json"] } hyper = "0.14" -indicatif = "0.16" +indicatif = "0.17" quick-xml = "0.22" regex = "1" log = "0.4" diff --git a/src/backends/github.rs b/src/backends/github.rs index df3dcc5..d5807ff 100644 --- a/src/backends/github.rs +++ b/src/backends/github.rs @@ -4,7 +4,6 @@ GitHub releases use std::env::{self, consts::EXE_SUFFIX}; use std::path::{Path, PathBuf}; -use indicatif::ProgressStyle; use reqwest::{self, header}; use crate::backends::find_rel_next_link; @@ -12,6 +11,7 @@ use crate::{ errors::*, get_target, update::{Release, ReleaseAsset, ReleaseUpdate}, + DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE, }; impl ReleaseAsset { @@ -238,7 +238,8 @@ pub struct UpdateBuilder { no_confirm: bool, current_version: Option, target_version: Option, - progress_style: Option, + progress_template: String, + progress_chars: String, auth_token: Option, custom_url: Option, } @@ -345,9 +346,14 @@ impl UpdateBuilder { self } - /// Toggle download progress bar, defaults to `off`. - pub fn set_progress_style(&mut self, progress_style: ProgressStyle) -> &mut Self { - self.progress_style = Some(progress_style); + /// Set download progress style. + pub fn set_progress_style( + &mut self, + progress_template: String, + progress_chars: String, + ) -> &mut Self { + self.progress_template = progress_template; + self.progress_chars = progress_chars; self } @@ -419,7 +425,8 @@ impl UpdateBuilder { }, target_version: self.target_version.as_ref().map(|v| v.to_owned()), show_download_progress: self.show_download_progress, - progress_style: self.progress_style.clone(), + progress_template: self.progress_template.clone(), + progress_chars: self.progress_chars.clone(), show_output: self.show_output, no_confirm: self.no_confirm, auth_token: self.auth_token.clone(), @@ -442,7 +449,8 @@ pub struct Update { show_download_progress: bool, show_output: bool, no_confirm: bool, - progress_style: Option, + progress_template: String, + progress_chars: String, auth_token: Option, custom_url: Option, } @@ -543,8 +551,12 @@ impl ReleaseUpdate for Update { self.no_confirm } - fn progress_style(&self) -> Option { - self.progress_style.clone() + fn progress_template(&self) -> String { + self.progress_template.to_owned() + } + + fn progress_chars(&self) -> String { + self.progress_chars.to_owned() } fn auth_token(&self) -> Option { @@ -566,7 +578,8 @@ impl Default for UpdateBuilder { no_confirm: false, current_version: None, target_version: None, - progress_style: None, + progress_template: DEFAULT_PROGRESS_TEMPLATE.to_string(), + progress_chars: DEFAULT_PROGRESS_CHARS.to_string(), auth_token: None, custom_url: None, } diff --git a/src/backends/gitlab.rs b/src/backends/gitlab.rs index 656d23b..6e4f89a 100644 --- a/src/backends/gitlab.rs +++ b/src/backends/gitlab.rs @@ -4,7 +4,6 @@ Gitlab releases use std::env::{self, consts::EXE_SUFFIX}; use std::path::{Path, PathBuf}; -use indicatif::ProgressStyle; use reqwest::{self, header}; use crate::backends::find_rel_next_link; @@ -12,6 +11,7 @@ use crate::{ errors::*, get_target, update::{Release, ReleaseAsset, ReleaseUpdate}, + DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE, }; impl ReleaseAsset { @@ -222,7 +222,8 @@ pub struct UpdateBuilder { no_confirm: bool, current_version: Option, target_version: Option, - progress_style: Option, + progress_template: String, + progress_chars: String, auth_token: Option, } @@ -328,9 +329,14 @@ impl UpdateBuilder { self } - /// Toggle download progress bar, defaults to `off`. - pub fn set_progress_style(&mut self, progress_style: ProgressStyle) -> &mut Self { - self.progress_style = Some(progress_style); + /// Set download progress style. + pub fn set_progress_style( + &mut self, + progress_template: String, + progress_chars: String, + ) -> &mut Self { + self.progress_template = progress_template; + self.progress_chars = progress_chars; self } @@ -402,7 +408,8 @@ impl UpdateBuilder { }, target_version: self.target_version.as_ref().map(|v| v.to_owned()), show_download_progress: self.show_download_progress, - progress_style: self.progress_style.clone(), + progress_template: self.progress_template.clone(), + progress_chars: self.progress_chars.clone(), show_output: self.show_output, no_confirm: self.no_confirm, auth_token: self.auth_token.clone(), @@ -424,7 +431,8 @@ pub struct Update { show_download_progress: bool, show_output: bool, no_confirm: bool, - progress_style: Option, + progress_template: String, + progress_chars: String, auth_token: Option, } impl Update { @@ -515,8 +523,12 @@ impl ReleaseUpdate for Update { self.no_confirm } - fn progress_style(&self) -> Option { - self.progress_style.clone() + fn progress_template(&self) -> String { + self.progress_template.to_owned() + } + + fn progress_chars(&self) -> String { + self.progress_chars.to_owned() } fn auth_token(&self) -> Option { @@ -538,7 +550,8 @@ impl Default for UpdateBuilder { no_confirm: false, current_version: None, target_version: None, - progress_style: None, + progress_template: DEFAULT_PROGRESS_TEMPLATE.to_string(), + progress_chars: DEFAULT_PROGRESS_CHARS.to_string(), auth_token: None, } } diff --git a/src/backends/s3.rs b/src/backends/s3.rs index 502815e..e0e8f37 100644 --- a/src/backends/s3.rs +++ b/src/backends/s3.rs @@ -6,8 +6,8 @@ use crate::{ get_target, update::{Release, ReleaseAsset, ReleaseUpdate}, version::bump_is_greater, + DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE, }; -use indicatif::ProgressStyle; use quick_xml::events::Event; use quick_xml::Reader; use regex::Regex; @@ -154,7 +154,8 @@ pub struct UpdateBuilder { no_confirm: bool, current_version: Option, target_version: Option, - progress_style: Option, + progress_template: String, + progress_chars: String, auth_token: Option, } @@ -174,7 +175,8 @@ impl Default for UpdateBuilder { no_confirm: false, current_version: None, target_version: None, - progress_style: None, + progress_template: DEFAULT_PROGRESS_TEMPLATE.to_string(), + progress_chars: DEFAULT_PROGRESS_CHARS.to_string(), auth_token: None, } } @@ -292,9 +294,14 @@ impl UpdateBuilder { self } - /// Toggle download progress bar, defaults to `off`. - pub fn set_progress_style(&mut self, progress_style: ProgressStyle) -> &mut Self { - self.progress_style = Some(progress_style); + /// Set download progress style. + pub fn set_progress_style( + &mut self, + progress_template: String, + progress_chars: String, + ) -> &mut Self { + self.progress_template = progress_template; + self.progress_chars = progress_chars; self } @@ -358,7 +365,8 @@ impl UpdateBuilder { }, target_version: self.target_version.as_ref().map(|v| v.to_owned()), show_download_progress: self.show_download_progress, - progress_style: self.progress_style.clone(), + progress_template: self.progress_template.clone(), + progress_chars: self.progress_chars.clone(), show_output: self.show_output, no_confirm: self.no_confirm, auth_token: self.auth_token.clone(), @@ -382,7 +390,8 @@ pub struct Update { show_download_progress: bool, show_output: bool, no_confirm: bool, - progress_style: Option, + progress_template: String, + progress_chars: String, auth_token: Option, } @@ -477,8 +486,12 @@ impl ReleaseUpdate for Update { self.no_confirm } - fn progress_style(&self) -> Option { - self.progress_style.clone() + fn progress_template(&self) -> String { + self.progress_template.to_owned() + } + + fn progress_chars(&self) -> String { + self.progress_chars.to_owned() } fn auth_token(&self) -> Option { diff --git a/src/lib.rs b/src/lib.rs index 35742ba..fbfa3d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,6 +150,10 @@ pub mod errors; pub mod update; pub mod version; +pub const DEFAULT_PROGRESS_TEMPLATE: &str = + "[{elapsed_precise}] [{bar:40}] {bytes}/{total_bytes} ({eta}) {msg}"; +pub const DEFAULT_PROGRESS_CHARS: &str = "=>-"; + use errors::*; /// Get the current target triple. @@ -607,7 +611,8 @@ pub struct Download { show_progress: bool, url: String, headers: reqwest::header::HeaderMap, - progress_style: ProgressStyle, + progress_template: String, + progress_chars: String, } impl Download { /// Specify download url @@ -616,9 +621,8 @@ impl Download { show_progress: false, url: url.to_owned(), headers: reqwest::header::HeaderMap::new(), - progress_style: ProgressStyle::default_bar() - .template("[{elapsed_precise}] [{bar:40}] {bytes}/{total_bytes} ({eta}) {msg}") - .progress_chars("=>-"), + progress_template: DEFAULT_PROGRESS_TEMPLATE.to_string(), + progress_chars: DEFAULT_PROGRESS_CHARS.to_string(), } } @@ -629,8 +633,13 @@ impl Download { } /// Set the progress style - pub fn set_progress_style(&mut self, progress_style: ProgressStyle) -> &mut Self { - self.progress_style = progress_style; + pub fn set_progress_style( + &mut self, + progress_template: String, + progress_chars: String, + ) -> &mut Self { + self.progress_template = progress_template; + self.progress_chars = progress_chars; self } @@ -699,7 +708,12 @@ impl Download { let mut downloaded = 0; let mut bar = if show_progress { let pb = ProgressBar::new(size); - pb.set_style(self.progress_style.clone()); + pb.set_style( + ProgressStyle::default_bar() + .template(&self.progress_template) + .expect("set ProgressStyle template failed") + .progress_chars(&self.progress_chars), + ); Some(pb) } else { diff --git a/src/update.rs b/src/update.rs index 5dbd1cc..b720722 100644 --- a/src/update.rs +++ b/src/update.rs @@ -1,4 +1,3 @@ -use indicatif::ProgressStyle; use reqwest::{self, header}; use std::fs; #[cfg(not(windows))] @@ -104,8 +103,11 @@ pub trait ReleaseUpdate { /// Flag indicating if the user shouldn't be prompted to confirm an update fn no_confirm(&self) -> bool; - /// Styling for progress information if `show_download_progress` is set (see `indicatif::ProgressStyle`) - fn progress_style(&self) -> Option; + // message template to use if `show_download_progress` is set (see `indicatif::ProgressStyle`) + fn progress_template(&self) -> String; + + // progress_chars to use if `show_download_progress` is set (see `indicatif::ProgressStyle`) + fn progress_chars(&self) -> String; /// Authorisation token for communicating with backend fn auth_token(&self) -> Option; @@ -218,9 +220,8 @@ pub trait ReleaseUpdate { download.set_headers(headers); download.show_progress(self.show_download_progress()); - if let Some(ref progress_style) = self.progress_style() { - download.set_progress_style(progress_style.clone()); - } + download.progress_template = self.progress_template(); + download.progress_chars = self.progress_chars(); download.download_to(&mut tmp_archive)?;