-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use anyhow::{anyhow,Result,Context}; | ||
use std::path::PathBuf; | ||
use reqwest::Url; | ||
|
||
use trauma::downloader::{DownloaderBuilder,StyleOptions,ProgressBarOpts}; | ||
use trauma::download::Download; | ||
|
||
use crate::lib::progress::{DEFAULT_PROGRESS_STYLE, DEFAULT_PROGRESS_INC}; | ||
use crate::lib::utils::pluralize; | ||
|
||
pub struct Downloads { | ||
list: Vec<Download>, | ||
} | ||
|
||
|
||
pub trait Downloadable { | ||
fn to_url(self) -> Result<Url>; | ||
} | ||
|
||
impl Downloadable for String { | ||
fn to_url(self) -> Result<Url> { | ||
let url = Url::parse(&self).context(format!("Download URL '{}' is not valid.", &self))?; | ||
Ok(url) | ||
} | ||
} | ||
|
||
impl Downloadable for Url { | ||
fn to_url(self) -> Result<Url> { | ||
Ok(self) | ||
} | ||
} | ||
|
||
impl Downloads { | ||
pub fn new() -> Self { | ||
let list = Vec::new(); | ||
Downloads { list } | ||
} | ||
|
||
pub fn add<T: Downloadable>(&mut self, item: T, filename: Option<&str>) -> Result<&Download> { | ||
let url = item.to_url()?; | ||
|
||
let resolved_filename = match filename { | ||
Some(name) => name.to_string(), | ||
None => { | ||
url.path_segments() | ||
.ok_or_else(|| anyhow::anyhow!("Error parsing URL."))? | ||
.last() | ||
.ok_or_else(|| anyhow::anyhow!("Error getting filename from download URL."))? | ||
.to_string() | ||
} | ||
}; | ||
|
||
|
||
let download = Download { url, filename: resolved_filename }; | ||
self.list.push(download); | ||
Ok(self.list.last().ok_or(anyhow::anyhow!("Failed to add download"))?) | ||
} | ||
|
||
pub fn default_style(&self) -> Result<StyleOptions> { | ||
let style = ProgressBarOpts::new( | ||
Some(DEFAULT_PROGRESS_STYLE.to_string()), | ||
Some(DEFAULT_PROGRESS_INC.to_string()), | ||
true, true); | ||
|
||
let style_clone = style.clone(); | ||
Ok(StyleOptions::new(style, style_clone)) | ||
} | ||
|
||
|
||
pub async fn download_all(&self, success_status: Option<&str>, | ||
no_downloads_message: Option<&str>) -> Result<()> { | ||
let downloads = &self.list; | ||
let total_files = downloads.len(); | ||
if !downloads.is_empty() { | ||
let downloader = DownloaderBuilder::new() | ||
.style_options(self.default_style()?) | ||
.build(); | ||
downloader.download(&downloads).await; | ||
println!("Downloaded {}.", pluralize(total_files as u64, "file")); | ||
for download in downloads { | ||
if let Some(msg) = success_status { | ||
let filename = PathBuf::from(&download.filename); | ||
let name_str = filename.file_name().ok_or(anyhow!("Internal Error: could not extract filename from download"))?; | ||
//println!(" - {}", name_str.to_string_lossy()); | ||
println!("{}", msg.replace("{}", &name_str.to_string_lossy())); | ||
} | ||
} | ||
} else { | ||
if no_downloads_message.is_some() { | ||
println!("{}", no_downloads_message.unwrap_or("")); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use indicatif::{ProgressBar, ProgressStyle}; | ||
use std::time::Duration; | ||
use std::thread; | ||
use anyhow::{anyhow,Result}; | ||
|
||
// these are separated since some APIs don't overload | ||
// indicatif bars, but take the same primitives. | ||
pub const DEFAULT_PROGRESS_STYLE: &str = "{spinner:.green} [{bar:40.green/white}] {pos:>}/{len} ({percent}%) eta {eta_precise:.green} {msg}"; | ||
pub const DEFAULT_PROGRESS_INC: &str = "=>"; | ||
|
||
pub fn default_progress_style() -> Result<ProgressStyle, anyhow::Error> { | ||
let style = ProgressStyle::default_bar() | ||
.progress_chars(DEFAULT_PROGRESS_INC) | ||
.template(DEFAULT_PROGRESS_STYLE)?; | ||
Ok(style) | ||
} | ||
|
||
pub struct Progress { | ||
pub bar: ProgressBar, | ||
spinner: thread::JoinHandle<()> | ||
} | ||
|
||
impl Progress { | ||
pub fn new(len: u64) -> Result<Progress> { | ||
let bar = ProgressBar::new(len as u64); | ||
bar.set_style(default_progress_style()?); | ||
|
||
let bar_clone = bar.clone(); | ||
let spinner = thread::spawn(move || { | ||
loop { | ||
bar_clone.tick(); | ||
thread::sleep(Duration::from_millis(20)); | ||
} | ||
}); | ||
Ok(Progress { bar, spinner }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.