Ratelimit save command progress bar updates (#14075)

<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Currently, the `save -p` command updates the progress animation each
time any data is written. This PR rate limits the animation so it
doesn't play as fast.

Here's an asciinema of [current
behavior](https://asciinema.org/a/8RWrWTozQSceqx6tYY7kzblqj) and
[proposed behavior](https://asciinema.org/a/E1pi0gMwMwFcxVHOy9Fv1Kk6R).

# User-Facing Changes

* `save -p` progress bar has a smoother animation

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting
N/A
This commit is contained in:
132ikl 2024-10-13 08:01:03 -04:00 committed by GitHub
parent bdbcf82967
commit a11c9e9d70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,6 +12,7 @@ use std::{
io::{self, BufRead, BufReader, Read, Write}, io::{self, BufRead, BufReader, Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
thread, thread,
time::{Duration, Instant},
}; };
#[derive(Clone)] #[derive(Clone)]
@ -482,7 +483,7 @@ fn stream_to_file(
let mut bar = progress_bar::NuProgressBar::new(known_size); let mut bar = progress_bar::NuProgressBar::new(known_size);
// TODO: reduce the number of progress bar updates? let mut last_update = Instant::now();
let mut reader = BufReader::new(source); let mut reader = BufReader::new(source);
@ -499,7 +500,10 @@ fn stream_to_file(
let len = buf.len(); let len = buf.len();
reader.consume(len); reader.consume(len);
bytes_processed += len as u64; bytes_processed += len as u64;
bar.update_bar(bytes_processed); if last_update.elapsed() >= Duration::from_millis(75) {
bar.update_bar(bytes_processed);
last_update = Instant::now();
}
} }
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => break Err(e), Err(e) => break Err(e),