Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to indicatif 0.17 #93

Merged
merged 9 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 23 additions & 10 deletions src/backends/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ 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;
use crate::{
errors::*,
get_target,
update::{Release, ReleaseAsset, ReleaseUpdate},
DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE,
};

impl ReleaseAsset {
Expand Down Expand Up @@ -238,7 +238,8 @@ pub struct UpdateBuilder {
no_confirm: bool,
current_version: Option<String>,
target_version: Option<String>,
progress_style: Option<ProgressStyle>,
progress_template: String,
progress_chars: String,
auth_token: Option<String>,
custom_url: Option<String>,
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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(),
Expand All @@ -442,7 +449,8 @@ pub struct Update {
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
progress_style: Option<ProgressStyle>,
progress_template: String,
progress_chars: String,
auth_token: Option<String>,
custom_url: Option<String>,
}
Expand Down Expand Up @@ -543,8 +551,12 @@ impl ReleaseUpdate for Update {
self.no_confirm
}

fn progress_style(&self) -> Option<ProgressStyle> {
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<String> {
Expand All @@ -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,
}
Expand Down
33 changes: 23 additions & 10 deletions src/backends/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ 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;
use crate::{
errors::*,
get_target,
update::{Release, ReleaseAsset, ReleaseUpdate},
DEFAULT_PROGRESS_CHARS, DEFAULT_PROGRESS_TEMPLATE,
};

impl ReleaseAsset {
Expand Down Expand Up @@ -222,7 +222,8 @@ pub struct UpdateBuilder {
no_confirm: bool,
current_version: Option<String>,
target_version: Option<String>,
progress_style: Option<ProgressStyle>,
progress_template: String,
progress_chars: String,
auth_token: Option<String>,
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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(),
Expand All @@ -424,7 +431,8 @@ pub struct Update {
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
progress_style: Option<ProgressStyle>,
progress_template: String,
progress_chars: String,
auth_token: Option<String>,
}
impl Update {
Expand Down Expand Up @@ -515,8 +523,12 @@ impl ReleaseUpdate for Update {
self.no_confirm
}

fn progress_style(&self) -> Option<ProgressStyle> {
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<String> {
Expand All @@ -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,
}
}
Expand Down
33 changes: 23 additions & 10 deletions src/backends/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -154,7 +154,8 @@ pub struct UpdateBuilder {
no_confirm: bool,
current_version: Option<String>,
target_version: Option<String>,
progress_style: Option<ProgressStyle>,
progress_template: String,
progress_chars: String,
auth_token: Option<String>,
}

Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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(),
Expand All @@ -382,7 +390,8 @@ pub struct Update {
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
progress_style: Option<ProgressStyle>,
progress_template: String,
progress_chars: String,
auth_token: Option<String>,
}

Expand Down Expand Up @@ -477,8 +486,12 @@ impl ReleaseUpdate for Update {
self.no_confirm
}

fn progress_style(&self) -> Option<ProgressStyle> {
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<String> {
Expand Down
28 changes: 21 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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(),
}
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use indicatif::ProgressStyle;
use reqwest::{self, header};
use std::fs;
#[cfg(not(windows))]
Expand Down Expand Up @@ -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<ProgressStyle>;
// 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<String>;
Expand Down Expand Up @@ -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)?;

Expand Down