nushell/crates/nu-command/src/progress_bar.rs

68 lines
2.3 KiB
Rust
Raw Normal View History

Progress bar Implementation (#7661) # Description _(Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience.)_ I implemented the status bar we talk about yesterday. The idea was inspired by the progress bar of `wget`. I decided to go for the second suggestion by `@Reilly` > 2. add an Option<usize> or whatever to RawStream (and ListStream?) for situations where you do know the length ahead of time For now only works with the command `save` but after the approve of this PR we can see how we can implement it on commands like `cp` and `mv` When using `fetch` nushell will check if there is any `content-length` attribute in the request header. If so, then `fetch` will send it through the new `Option` variable in the `RawStream` to the `save`. If we know the total size we show the progress bar ![nu_pb01](https://user-images.githubusercontent.com/38369407/210298647-07ee55ea-e751-41b1-a84d-f72ec1f6e9e5.jpg) but if we don't then we just show the stats like: data already saved, bytes per second, and time lapse. ![nu_pb02](https://user-images.githubusercontent.com/38369407/210298698-1ef65f51-40cc-4481-83de-309cbd1049cb.jpg) ![nu_pb03](https://user-images.githubusercontent.com/38369407/210298701-eef2ef13-9206-4a98-8202-e4fe5531d79d.jpg) Please let me know If I need to make any changes and I will be happy to do it. # User-Facing Changes A new flag (`--progress` `-p`) was added to the `save` command Examples: ```nu fetch https://github.com/torvalds/linux/archive/refs/heads/master.zip | save --progress -f main.zip fetch https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso | save --progress -f main.zip open main.zip --raw | save --progress main.copy ``` # 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 -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - I am getting some errors and its weird because the errors are showing up in files i haven't touch. Is this normal? # 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. Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
2023-01-11 01:57:48 +00:00
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
`cp` progress bar implementation (#8012) # 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>
2023-02-22 19:57:38 +00:00
ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{bar:30.cyan/blue}] [{bytes}/{total_bytes}] {binary_bytes_per_sec} ({eta}) {wide_msg}")
Progress bar Implementation (#7661) # Description _(Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience.)_ I implemented the status bar we talk about yesterday. The idea was inspired by the progress bar of `wget`. I decided to go for the second suggestion by `@Reilly` > 2. add an Option<usize> or whatever to RawStream (and ListStream?) for situations where you do know the length ahead of time For now only works with the command `save` but after the approve of this PR we can see how we can implement it on commands like `cp` and `mv` When using `fetch` nushell will check if there is any `content-length` attribute in the request header. If so, then `fetch` will send it through the new `Option` variable in the `RawStream` to the `save`. If we know the total size we show the progress bar ![nu_pb01](https://user-images.githubusercontent.com/38369407/210298647-07ee55ea-e751-41b1-a84d-f72ec1f6e9e5.jpg) but if we don't then we just show the stats like: data already saved, bytes per second, and time lapse. ![nu_pb02](https://user-images.githubusercontent.com/38369407/210298698-1ef65f51-40cc-4481-83de-309cbd1049cb.jpg) ![nu_pb03](https://user-images.githubusercontent.com/38369407/210298701-eef2ef13-9206-4a98-8202-e4fe5531d79d.jpg) Please let me know If I need to make any changes and I will be happy to do it. # User-Facing Changes A new flag (`--progress` `-p`) was added to the `save` command Examples: ```nu fetch https://github.com/torvalds/linux/archive/refs/heads/master.zip | save --progress -f main.zip fetch https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso | save --progress -f main.zip open main.zip --raw | save --progress main.copy ``` # 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 -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - I am getting some errors and its weird because the errors are showing up in files i haven't touch. Is this normal? # 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. Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
2023-01-11 01:57:48 +00:00
}
_ => {
// But if we don't know the total then we just show the stats progress
ProgressStyle::with_template(
`cp` progress bar implementation (#8012) # 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>
2023-02-22 19:57:38 +00:00
"{spinner:.green} [{elapsed_precise}] {bytes} {binary_bytes_per_sec} {wide_msg}",
Progress bar Implementation (#7661) # Description _(Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience.)_ I implemented the status bar we talk about yesterday. The idea was inspired by the progress bar of `wget`. I decided to go for the second suggestion by `@Reilly` > 2. add an Option<usize> or whatever to RawStream (and ListStream?) for situations where you do know the length ahead of time For now only works with the command `save` but after the approve of this PR we can see how we can implement it on commands like `cp` and `mv` When using `fetch` nushell will check if there is any `content-length` attribute in the request header. If so, then `fetch` will send it through the new `Option` variable in the `RawStream` to the `save`. If we know the total size we show the progress bar ![nu_pb01](https://user-images.githubusercontent.com/38369407/210298647-07ee55ea-e751-41b1-a84d-f72ec1f6e9e5.jpg) but if we don't then we just show the stats like: data already saved, bytes per second, and time lapse. ![nu_pb02](https://user-images.githubusercontent.com/38369407/210298698-1ef65f51-40cc-4481-83de-309cbd1049cb.jpg) ![nu_pb03](https://user-images.githubusercontent.com/38369407/210298701-eef2ef13-9206-4a98-8202-e4fe5531d79d.jpg) Please let me know If I need to make any changes and I will be happy to do it. # User-Facing Changes A new flag (`--progress` `-p`) was added to the `save` command Examples: ```nu fetch https://github.com/torvalds/linux/archive/refs/heads/master.zip | save --progress -f main.zip fetch https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso | save --progress -f main.zip open main.zip --raw | save --progress main.copy ``` # 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 -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - I am getting some errors and its weird because the errors are showing up in files i haven't touch. Is this normal? # 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. Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
2023-01-11 01:57:48 +00:00
)
}
};
let total_bytes = total_bytes.unwrap_or_default();
Progress bar Implementation (#7661) # Description _(Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience.)_ I implemented the status bar we talk about yesterday. The idea was inspired by the progress bar of `wget`. I decided to go for the second suggestion by `@Reilly` > 2. add an Option<usize> or whatever to RawStream (and ListStream?) for situations where you do know the length ahead of time For now only works with the command `save` but after the approve of this PR we can see how we can implement it on commands like `cp` and `mv` When using `fetch` nushell will check if there is any `content-length` attribute in the request header. If so, then `fetch` will send it through the new `Option` variable in the `RawStream` to the `save`. If we know the total size we show the progress bar ![nu_pb01](https://user-images.githubusercontent.com/38369407/210298647-07ee55ea-e751-41b1-a84d-f72ec1f6e9e5.jpg) but if we don't then we just show the stats like: data already saved, bytes per second, and time lapse. ![nu_pb02](https://user-images.githubusercontent.com/38369407/210298698-1ef65f51-40cc-4481-83de-309cbd1049cb.jpg) ![nu_pb03](https://user-images.githubusercontent.com/38369407/210298701-eef2ef13-9206-4a98-8202-e4fe5531d79d.jpg) Please let me know If I need to make any changes and I will be happy to do it. # User-Facing Changes A new flag (`--progress` `-p`) was added to the `save` command Examples: ```nu fetch https://github.com/torvalds/linux/archive/refs/heads/master.zip | save --progress -f main.zip fetch https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso | save --progress -f main.zip open main.zip --raw | save --progress main.copy ``` # 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 -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - I am getting some errors and its weird because the errors are showing up in files i haven't touch. Is this normal? # 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. Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
2023-01-11 01:57:48 +00:00
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);
}
`cp` progress bar implementation (#8012) # 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>
2023-02-22 19:57:38 +00:00
pub fn finished_msg(&self, msg: String) {
self.pb.finish_with_message(msg);
}
Progress bar Implementation (#7661) # Description _(Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience.)_ I implemented the status bar we talk about yesterday. The idea was inspired by the progress bar of `wget`. I decided to go for the second suggestion by `@Reilly` > 2. add an Option<usize> or whatever to RawStream (and ListStream?) for situations where you do know the length ahead of time For now only works with the command `save` but after the approve of this PR we can see how we can implement it on commands like `cp` and `mv` When using `fetch` nushell will check if there is any `content-length` attribute in the request header. If so, then `fetch` will send it through the new `Option` variable in the `RawStream` to the `save`. If we know the total size we show the progress bar ![nu_pb01](https://user-images.githubusercontent.com/38369407/210298647-07ee55ea-e751-41b1-a84d-f72ec1f6e9e5.jpg) but if we don't then we just show the stats like: data already saved, bytes per second, and time lapse. ![nu_pb02](https://user-images.githubusercontent.com/38369407/210298698-1ef65f51-40cc-4481-83de-309cbd1049cb.jpg) ![nu_pb03](https://user-images.githubusercontent.com/38369407/210298701-eef2ef13-9206-4a98-8202-e4fe5531d79d.jpg) Please let me know If I need to make any changes and I will be happy to do it. # User-Facing Changes A new flag (`--progress` `-p`) was added to the `save` command Examples: ```nu fetch https://github.com/torvalds/linux/archive/refs/heads/master.zip | save --progress -f main.zip fetch https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso | save --progress -f main.zip open main.zip --raw | save --progress main.copy ``` # 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 -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - I am getting some errors and its weird because the errors are showing up in files i haven't touch. Is this normal? # 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. Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
2023-01-11 01:57:48 +00:00
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,
}
}
}