nushell/crates/nu-command/src/progress_bar.rs
Darren Schroeder ff5815c0a3
remove cp-old (#11622)
# Description

The `cp-old` command has been deprecated for a few releases now. It
should be safe to remove it once and for all. Let's see.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2024-01-24 07:38:15 +08:00

69 lines
2.3 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,
bytes_processed: u64,
total_bytes: Option<u64>,
}
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,
total_bytes: None,
bytes_processed: 0,
}
}
pub fn update_bar(&mut self, bytes_processed: u64) {
self.pb.set_position(bytes_processed);
}
#[allow(dead_code)]
pub fn finished_msg(&self, msg: String) {
self.pb.finish_with_message(msg);
}
pub fn abandoned_msg(&self, msg: String) {
self.pb.abandon_with_message(msg);
}
pub fn clone(&self) -> NuProgressBar {
NuProgressBar {
pb: self.pb.clone(),
bytes_processed: self.bytes_processed,
total_bytes: self.total_bytes,
}
}
}