From a11c9e9d7030faea2b5ecd356c7ee09657af0ca9 Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Sun, 13 Oct 2024 08:01:03 -0400 Subject: [PATCH] Ratelimit save command progress bar updates (#14075) # Description 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 - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting N/A --- crates/nu-command/src/filesystem/save.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/filesystem/save.rs b/crates/nu-command/src/filesystem/save.rs index ec7e4140ce..6fda754341 100644 --- a/crates/nu-command/src/filesystem/save.rs +++ b/crates/nu-command/src/filesystem/save.rs @@ -12,6 +12,7 @@ use std::{ io::{self, BufRead, BufReader, Read, Write}, path::{Path, PathBuf}, thread, + time::{Duration, Instant}, }; #[derive(Clone)] @@ -482,7 +483,7 @@ fn stream_to_file( 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); @@ -499,7 +500,10 @@ fn stream_to_file( let len = buf.len(); reader.consume(len); 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) => break Err(e),