mirror of
https://github.com/nushell/nushell
synced 2024-12-29 06:23:11 +00:00
fc23c6721a
# NOTE Clean duplicate of #7825 Sorry about all the mess guys... I got confuse with GitHub and and ended up mankind that mess. This the same code I just cleaned the commits. # Description Progress bar implementation for the `cp` command. Now if the flag `-p` or `--progress` is set, then the user will be able to see the progress of the file or files being copy ![progressbar_cp01](https://user-images.githubusercontent.com/38369407/213899494-0f6a4aa9-ee82-48c3-a1f1-1816f3fc1d9c.jpg) ![progressbar_cp02](https://user-images.githubusercontent.com/38369407/213899497-2f9e6e8c-fdd9-400b-bd8d-c59899ae0368.jpg) # User-Facing Changes A new flag (`--progress` `-p`) was added to the `cp` command Examples: ```nu cp -p test_file.txt test_folder_1\ cp -r -p test_folder\* test_folder_1\ cp -r -p -i test_folder\* test_folder_1\ ``` ## Notes - The progress bar uses `std::io::{Read, Write}` instead of `std::fs::copy` to get the progress. that means that when the progress bar is used the copy process might be a bit slower. - Progress bar for symbolic links TBD: Since symbolic links are usually very light I think is not worth it to duplicate the function `copy_symlink` just to add a progress bar that will be so fast to the point is not needed, but.. for consistency purposes we might need to added it, In that case I would have to pass the variable `progress` by parameter (to not duplicate code unnecessary). If I do that i would have to pass the `progress` var to every function to respect `copy_impl: impl Fn(PathBuf, PathBuf, Span)`. Please let me know if this is not clear :p --------- Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
67 lines
2.3 KiB
Rust
67 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);
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|
|
}
|