8000 improve progress bar in install by Schniz · Pull Request #1125 · Schniz/fnm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

improve progress bar in install #1125

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

Merged
merged 8 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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)));
}
}
Loading
0