nushell/crates/nu-command/src/progress_bar.rs
Stefan Holderbach 455d32d9e5
Cut down unnecessary lint allows (#14335)
Trying to reduce lint allows either by checking if they are former false
positives or by fixing the underlying warning.

- **Remove dead `allow(dead_code)`**
- **Remove recursive dead code**
- **Remove dead code**
- **Move test only functions to test module**
  The unit tests that use them, themselves are somewhat sus in that they
mock the usage and not test specificly used methods of the
implementation, so there is a risk for divergence
- **Remove `clippy::uninit_vec` allow.**
  May have been a false positive, or the impl has changed somewhat.
We certainly want to look at the unsafe code here to vet for
correctness.
2024-11-15 19:24:39 +01:00

52 lines
1.8 KiB
Rust

use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use std::fmt;
// This module includes the progress bar used to show the progress when using the command `save`
// Eventually it would be nice to find a better place for it.
pub struct NuProgressBar {
pub pb: ProgressBar,
}
impl NuProgressBar {
pub fn new(total_bytes: Option<u64>) -> NuProgressBar {
// Let's create the progress bar template.
let template = match total_bytes {
Some(_) => {
// We will use a progress bar if we know the total bytes of the stream
ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{bar:30.cyan/blue}] [{bytes}/{total_bytes}] {binary_bytes_per_sec} ({eta}) {wide_msg}")
}
_ => {
// But if we don't know the total then we just show the stats progress
ProgressStyle::with_template(
"{spinner:.green} [{elapsed_precise}] {bytes} {binary_bytes_per_sec} {wide_msg}",
)
}
};
let total_bytes = total_bytes.unwrap_or_default();
let new_progress_bar = ProgressBar::new(total_bytes);
new_progress_bar.set_style(
template
.unwrap_or_else(|_| ProgressStyle::default_bar())
.with_key("eta", |state: &ProgressState, w: &mut dyn fmt::Write| {
let _ = fmt::write(w, format_args!("{:.1}s", state.eta().as_secs_f64()));
})
.progress_chars("#>-"),
);
NuProgressBar {
pb: new_progress_bar,
}
}
pub fn update_bar(&mut self, bytes_processed: u64) {
self.pb.set_position(bytes_processed);
}
pub fn abandoned_msg(&self, msg: String) {
self.pb.abandon_with_message(msg);
}
}