Browse Source

improve progress bar in install (#1125)

* update indicatif version

* add a eprintln after it finishes

* make it prettier

* update progress bar docs

* add changeset

* s/no_progress: true/progress: ProgressConfig::Never/g

* styling the progress bar

* fix test
remotes/origin/use-bnz
Gal Schlezinger 9 months ago committed by GitHub
parent
commit
d9af62ff43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      .changeset/poor-poets-compete.md
  2. 4
      Cargo.lock
  3. 2
      Cargo.toml
  4. 7
      docs/commands.md
  5. 23
      src/commands/install.rs
  6. 30
      src/progress.rs

5
.changeset/poor-poets-compete.md

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
---
"fnm": patch
---
make nicer styling in progress bar (add newline, make it unicode)

4
Cargo.lock generated

@ -935,9 +935,9 @@ dependencies = [ @@ -935,9 +935,9 @@ dependencies = [
[[package]]
name = "indicatif"
version = "0.17.6"
version = "0.17.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b297dc40733f23a0e52728a58fa9489a5b7638a324932de16b41adc3ef80730"
checksum = "763a5a8f45087d6bcea4222e7b72c291a054edf80e4ef6efd2a4979878c7bea3"
dependencies = [
"console",
"instant",

2
Cargo.toml

@ -30,7 +30,7 @@ sysinfo = "0.29.3" @@ -30,7 +30,7 @@ sysinfo = "0.29.3"
thiserror = "1.0.44"
clap_complete = "4.3.1"
anyhow = "1.0.71"
indicatif = "0.17.6"
indicatif = "0.17.8"
[dev-dependencies]
pretty_assertions = "1.4.0"

7
docs/commands.md

@ -216,8 +216,11 @@ Options: @@ -216,8 +216,11 @@ Options:
--latest
Install latest version
--no-progress
Do not display a progress bar
--progress <PROGRESS>
Show an interactive progress bar for the download status
[default: auto]
[possible values: auto, never, always]
--log-level <LOG_LEVEL>
The log level of fnm commands

23
src/commands/install.rs

@ -3,9 +3,9 @@ use crate::alias::create_alias; @@ -3,9 +3,9 @@ use crate::alias::create_alias;
use crate::arch::get_safe_arch;
use crate::config::FnmConfig;
use crate::downloader::{install_node_dist, Error as DownloaderError};
use crate::log_level::LogLevel;
use crate::lts::LtsType;
use crate::outln;
use crate::progress::ProgressConfig;
use crate::remote_node_index;
use crate::user_version::UserVersion;
use crate::version::Version;
@ -27,9 +27,11 @@ pub struct Install { @@ -27,9 +27,11 @@ pub struct Install {
#[clap(long, conflicts_with_all = &["version", "lts"])]
pub latest: bool,
/// Do not display a progress bar
#[clap(long)]
pub no_progress: bool,
/// Show an interactive progress bar for the download
/// status.
#[clap(long, default_value_t)]
#[arg(value_enum)]
pub progress: ProgressConfig,
}
impl Install {
@ -39,19 +41,19 @@ impl Install { @@ -39,19 +41,19 @@ impl Install {
version: v,
lts: false,
latest: false,
no_progress: _,
..
} => Ok(v),
Self {
version: None,
lts: true,
latest: false,
no_progress: _,
..
} => Ok(Some(UserVersion::Full(Version::Lts(LtsType::Latest)))),
Self {
version: None,
lts: false,
latest: true,
no_progress: _,
..
} => Ok(Some(UserVersion::Full(Version::Latest))),
_ => Err(Error::TooManyVersionsProvided),
}
@ -63,8 +65,7 @@ impl Command for Install { @@ -63,8 +65,7 @@ impl Command for Install {
fn apply(self, config: &FnmConfig) -> Result<(), Self::Error> {
let current_dir = std::env::current_dir().unwrap();
let show_progress = !self.no_progress && config.log_level().is_writable(&LogLevel::Info);
let show_progress = self.progress.enabled(config);
let current_version = self
.version()?
@ -236,7 +237,7 @@ mod tests { @@ -236,7 +237,7 @@ mod tests {
version: UserVersion::from_str("12.0.0").ok(),
lts: false,
latest: false,
no_progress: true,
progress: ProgressConfig::Never,
}
.apply(&config)
.expect("Can't install");
@ -262,7 +263,7 @@ mod tests { @@ -262,7 +263,7 @@ mod tests {
version: None,
lts: false,
latest: true,
no_progress: true,
progress: ProgressConfig::Never,
}
.apply(&config)
.expect("Can't install");

30
src/progress.rs

@ -8,15 +8,35 @@ pub struct ResponseProgress { @@ -8,15 +8,35 @@ pub struct ResponseProgress {
response: Response,
}
#[derive(Default, Clone, Debug, clap::ValueEnum)]
pub enum ProgressConfig {
#[default]
Auto,
Never,
Always,
}
impl ProgressConfig {
pub fn enabled(&self, config: &crate::config::FnmConfig) -> bool {
match self {
Self::Never => false,
Self::Always => true,
Self::Auto => config
.log_level()
.is_writable(&crate::log_level::LogLevel::Info),
}
}
}
fn make_progress_bar(size: u64, target: ProgressDrawTarget) -> ProgressBar {
let bar = ProgressBar::with_draw_target(Some(size), target);
bar.set_style(
ProgressStyle::with_template(
"[{elapsed_precise}] [{bar:40}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
"{elapsed_precise:.white.dim} {wide_bar:.cyan} {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
)
.unwrap()
.progress_chars("#>-"),
.progress_chars("█▉▊▋▌▍▎▏ "),
);
bar
@ -54,6 +74,7 @@ impl Read for ResponseProgress { @@ -54,6 +74,7 @@ impl Read for ResponseProgress {
impl Drop for ResponseProgress {
fn drop(&mut self) {
self.finish();
eprintln!();
}
}
@ -139,9 +160,6 @@ mod tests { @@ -139,9 +160,6 @@ mod tests {
assert_eq!(size, CONTENT_LENGTH);
assert_eq!(buf, "a".repeat(CONTENT_LENGTH).as_bytes());
assert!(out_buf
.lock()
.unwrap()
.contains(&format!("[{}]", &"#".repeat(40))));
assert!(out_buf.lock().unwrap().contains(&"█".repeat(40)));
}
}

Loading…
Cancel
Save