Skip to content

Commit

Permalink
improve progress bar in install (#1125)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Schniz authored May 26, 2024
1 parent ced2f30 commit d9af62f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-poets-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fnm": patch
---

make nicer styling in progress bar (add newline, make it unicode)
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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),
}
Expand All @@ -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()?
Expand Down Expand Up @@ -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");
Expand All @@ -262,7 +263,7 @@ mod tests {
version: None,
lts: false,
latest: true,
no_progress: true,
progress: ProgressConfig::Never,
}
.apply(&config)
.expect("Can't install");
Expand Down
30 changes: 24 additions & 6 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,6 +74,7 @@ impl Read for ResponseProgress {
impl Drop for ResponseProgress {
fn drop(&mut self) {
self.finish();
eprintln!();
}
}

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

0 comments on commit d9af62f

Please sign in to comment.