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

[#63] Added tag support in download progress #64

Merged
merged 6 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 6 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ For more details, refer to the official documentation:
let store_directory = config.ensure_store_directory();

let tools: Vec<String> = config.tools.keys().cloned().collect();
let sync_progress = SyncProgress::new(tools);
let tags: Vec<String> = config
.tools
.values()
.map(|config_asset| config_asset.tag.clone().unwrap_or_else(|| "latest".into()))
.collect();
let sync_progress = SyncProgress::new(tools, tags);
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
let installer = Installer::mk(store_directory, sync_progress);

for (tool_name, config_asset) in config.tools.iter() {
Expand Down
4 changes: 2 additions & 2 deletions src/sync/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod tests {
asset_name: "ASSET_NAME",
version: &ToolInfoTag::Latest.to_str_version(),
pb_msg: &ProgressBar::hidden(),
sync_progress: &SyncProgress::new(vec!["tool".to_string()]),
sync_progress: &SyncProgress::new(vec!["tool".to_string()], vec!["latest".to_string()]),
};

assert_eq!(
Expand All @@ -154,7 +154,7 @@ mod tests {
asset_name: "ASSET_NAME",
version: &ToolInfoTag::Specific("SPECIFIC_TAG".to_string()).to_str_version(),
pb_msg: &ProgressBar::hidden(),
sync_progress: &SyncProgress::new(vec!["tool".to_string()]),
sync_progress: &SyncProgress::new(vec!["tool".to_string()], vec!["latest".to_string()]),
};

assert_eq!(
Expand Down
8 changes: 5 additions & 3 deletions src/sync/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl Installer {
}

pub fn install(&self, tool_name: &str, config_asset: &ConfigAsset) {
let pb_msg = self.sync_progress.create_message_bar(tool_name);
let tag: String = config_asset.tag.clone().unwrap_or_else(|| "latest".into());
let pb_msg = self.sync_progress.create_message_bar(tool_name, &tag);

match configure_tool(tool_name, config_asset) {
Tool::Known(tool_info) => match self.sync_single_tool(&tool_info, &pb_msg) {
Expand All @@ -50,11 +51,12 @@ impl Installer {
}
Err(e) => {
self.sync_progress
.failure(pb_msg, tool_name, format!("[error] {}", e));
.failure(pb_msg, tool_name, &tag, format!("[error] {}", e));
}
},
Tool::Error(e) => {
self.sync_progress.failure(pb_msg, tool_name, e.display());
self.sync_progress
.failure(pb_msg, tool_name, &tag, e.display());
}
}
}
Expand Down
74 changes: 67 additions & 7 deletions src/sync/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

pub struct SyncProgress {
max_tool_size: usize,
max_tag_size: usize,
multi_progress: MultiProgress,
}

Expand All @@ -13,36 +14,48 @@ const PROCESS: Emoji<'_, '_> = Emoji("📥 ", ".. ");
impl SyncProgress {
/// Creates new `SyncProgress` from a list of tools.
/// !!! The given `Vec` must be non-empty !!!
pub fn new(tools: Vec<String>) -> SyncProgress {
pub fn new(tools: Vec<String>, tags: Vec<String>) -> SyncProgress {
// unwrap is safe here because 'new' is called with a non-empty vector
let max_tool_size = tools.iter().map(|tool| tool.len()).max().unwrap();

// putting a default of 8 here since tags like v0.10.10 is already 8
let max_tag_size = tags
.iter()
.map(|tag| match tag.len() {
0..=8 => 8,
_ => tag.len(),
})
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
.max()
.unwrap_or(8);

let multi_progress = MultiProgress::new();

SyncProgress {
max_tool_size,
max_tag_size,
multi_progress,
}
}

fn fmt_prefix(&self, emoji: Emoji, tool_name: &str, tag_name: &str) -> String {
let aligned_tool = format!(
"{:width$} {:<8}",
"{:tool_width$} {:tag_width$}",
tool_name,
tag_name,
width = self.max_tool_size
tool_width = self.max_tool_size,
tag_width = self.max_tag_size,
);

format!("{}{}", emoji, aligned_tool)
}

pub fn create_message_bar(&self, tool_name: &str) -> ProgressBar {
pub fn create_message_bar(&self, tool_name: &str, tag_name: &str) -> ProgressBar {
let message_style = ProgressStyle::with_template("{prefix:.bold.dim} {msg}").unwrap();

self.multi_progress.add(
ProgressBar::new(100)
.with_style(message_style)
.with_prefix(self.fmt_prefix(PROCESS, tool_name, "")),
.with_prefix(self.fmt_prefix(PROCESS, tool_name, tag_name)),
)
}

Expand All @@ -66,11 +79,58 @@ impl SyncProgress {
pb.finish();
}

pub fn failure(&self, pb: ProgressBar, tool_name: &str, err_msg: String) {
pb.set_prefix(self.fmt_prefix(FAILURE, tool_name, ""));
pub fn failure(&self, pb: ProgressBar, tool_name: &str, tag_name: &str, err_msg: String) {
pb.set_prefix(self.fmt_prefix(FAILURE, tool_name, tag_name));

let failure_msg = format!("{}", style(err_msg).red());
pb.set_message(failure_msg);
pb.finish();
}
}

#[cfg(test)]
mod tests {
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
use super::SyncProgress;

#[test]
fn test_max_tag_size_specific() {
let tags: Vec<String> = vec![
String::from("v10.10.100"),
String::from("latest"),
String::from("latest"),
];
let tools: Vec<String> = vec![
String::from("ripgrep"),
String::from("bat"),
String::from("exa"),
];

let progres = SyncProgress::new(tools, tags);

// v10.10.100 is 10 characters
assert_eq!(progres.max_tag_size, 10);
// ripgrep is 7 characters
assert_eq!(progres.max_tool_size, 7);
}

#[test]
fn test_max_tag_size_latest() {
let tags: Vec<String> = vec![
String::from("latest"),
String::from("latest"),
String::from("latest"),
];
let tools: Vec<String> = vec![
String::from("ripgrep"),
String::from("bat"),
String::from("exa"),
];

let progres = SyncProgress::new(tools, tags);

// latest is 6 characters so it should default to 8
assert_eq!(progres.max_tag_size, 8);
// ripgrep is 7 characters
assert_eq!(progres.max_tool_size, 7);
}
}