-
Notifications
You must be signed in to change notification settings - Fork 464
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
Add progress bar to install command #1028
Changes from 1 commit
a7fe780
9ae6a36
0298a47
d0695be
22d2dd9
eb36bd3
22d7645
065ff49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"fnm": minor | ||
--- | ||
|
||
Show a progress bar when downloading and extracting node |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,8 +2,10 @@ use crate::arch::Arch; | |||||
use crate::archive; | ||||||
use crate::archive::{Error as ExtractError, Extract}; | ||||||
use crate::directory_portal::DirectoryPortal; | ||||||
use crate::progress::ResponseProgress; | ||||||
use crate::version::Version; | ||||||
use log::debug; | ||||||
use std::io::Read; | ||||||
use std::path::Path; | ||||||
use std::path::PathBuf; | ||||||
use thiserror::Error; | ||||||
|
@@ -63,10 +65,7 @@ fn download_url(base_url: &Url, version: &Version, arch: &Arch) -> Url { | |||||
.unwrap() | ||||||
} | ||||||
|
||||||
pub fn extract_archive_into<P: AsRef<Path>>( | ||||||
path: P, | ||||||
response: crate::http::Response, | ||||||
) -> Result<(), Error> { | ||||||
fn extract_archive_into<P: AsRef<Path>, R: Read>(path: P, response: R) -> Result<(), Error> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a thought (not important, can be done in a different PR): what do you think about starting to use
Suggested change
|
||||||
#[cfg(unix)] | ||||||
let extractor = archive::TarXz::new(response); | ||||||
#[cfg(windows)] | ||||||
|
@@ -81,6 +80,7 @@ pub fn install_node_dist<P: AsRef<Path>>( | |||||
node_dist_mirror: &Url, | ||||||
installations_dir: P, | ||||||
arch: &Arch, | ||||||
show_progress: bool, | ||||||
) -> Result<(), Error> { | ||||||
let installation_dir = PathBuf::from(installations_dir.as_ref()).join(version.v_str()); | ||||||
|
||||||
|
@@ -109,7 +109,11 @@ pub fn install_node_dist<P: AsRef<Path>>( | |||||
} | ||||||
|
||||||
debug!("Extracting response..."); | ||||||
extract_archive_into(&portal, response)?; | ||||||
if show_progress { | ||||||
extract_archive_into(&portal, ResponseProgress::new(response))?; | ||||||
} else { | ||||||
extract_archive_into(&portal, response)?; | ||||||
} | ||||||
debug!("Extraction completed"); | ||||||
|
||||||
let installed_directory = std::fs::read_dir(&portal)? | ||||||
|
@@ -171,7 +175,8 @@ mod tests { | |||||
let version = Version::parse("12.0.0").unwrap(); | ||||||
let arch = Arch::X64; | ||||||
let node_dist_mirror = Url::parse("https://nodejs.org/dist/").unwrap(); | ||||||
install_node_dist(&version, &node_dist_mirror, path, &arch).expect("Can't install Node 12"); | ||||||
install_node_dist(&version, &node_dist_mirror, path, &arch, false) | ||||||
.expect("Can't install Node 12"); | ||||||
|
||||||
let mut location_path = path.join(version.v_str()).join("installation"); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::io::Read; | ||
|
||
use indicatif::{ProgressBar, ProgressStyle}; | ||
use reqwest::blocking::Response; | ||
|
||
pub struct ResponseProgress { | ||
progress: Option<ProgressBar>, | ||
response: Response, | ||
} | ||
|
||
fn make_progress_bar(size: u64) -> ProgressBar { | ||
let bar = ProgressBar::new(size); | ||
|
||
bar.set_style( | ||
ProgressStyle::default_bar() | ||
.template("[{elapsed_precise}] [{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})") | ||
.unwrap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can return a |
||
.progress_chars("#>-") | ||
); | ||
|
||
bar | ||
} | ||
|
||
impl ResponseProgress { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you think we should unit test this somehow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a test to ensure we still read from the underlying response with the progress wrapper. Also added a The mock impl is a bit verbose so I added it in a separate commit to revert it easily. Let me know if you'd prefer to just test the read operations pass through. |
||
pub fn new(response: Response) -> Self { | ||
Self { | ||
progress: response.content_length().map(make_progress_bar), | ||
response, | ||
} | ||
} | ||
|
||
pub fn finish(&self) { | ||
if let Some(ref bar) = self.progress { | ||
bar.finish(); | ||
} | ||
} | ||
} | ||
|
||
impl Read for ResponseProgress { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
let size = self.response.read(buf)?; | ||
|
||
if let Some(ref bar) = self.progress { | ||
bar.inc(size as u64); | ||
} | ||
|
||
Ok(size) | ||
} | ||
} | ||
|
||
impl Drop for ResponseProgress { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Drop because the XzDecoder takes ownership of the reader. Ensures the progress bar is finalized once the download is completed. Not sure if there's a better way of doing this. |
||
fn drop(&mut self) { | ||
self.finish() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think clap has a way to add
--progress/--no-progress
to enable or disable a flag, just made a progress bar the default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi, I'm gonna change that before releasing to
--progress=never
, and will have--progress=auto|always
soauto
will be based on the log level