| 1 |
// Copyright 2022-2026 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::io; |
| 16 |
use std::path::Path; |
| 17 |
use std::sync::Mutex; |
| 18 |
use std::time::Duration; |
| 19 |
use std::time::Instant; |
| 20 |
|
| 21 |
use crossterm::terminal::Clear; |
| 22 |
use crossterm::terminal::ClearType; |
| 23 |
use jj_lib::repo_path::RepoPath; |
| 24 |
|
| 25 |
use crate::text_util; |
| 26 |
use crate::ui::OutputGuard; |
| 27 |
use crate::ui::ProgressOutput; |
| 28 |
use crate::ui::Ui; |
| 29 |
|
| 30 |
pub const UPDATE_HZ: u32 = 30; |
| 31 |
pub const INITIAL_DELAY: Duration = Duration::from_millis(250); |
| 32 |
|
| 33 |
pub struct ProgressWriter<'a> { |
| 34 |
prefix: &'a str, |
| 35 |
guard: Option<OutputGuard>, |
| 36 |
output: ProgressOutput<io::Stderr>, |
| 37 |
next_display_time: Instant, |
| 38 |
} |
| 39 |
|
| 40 |
// Callers call `display` every time they have new progress, and this will |
| 41 |
// intermittently print the message synchronously, discarding too-frequent |
| 42 |
// updates. There is an initial delay to avoid printing anything on fast |
| 43 |
// commands. For example, `jj status` may be fast or slow depending on |
| 44 |
// repository size, and in slow runs the progress updates help users see that jj |
| 45 |
// is working. |
| 46 |
// |
| 47 |
// This deliberately doesn't try to be too smart about when to display progress, |
| 48 |
// such as trying to display the latest message. Printing synchronously without |
| 49 |
// any delay to a terminal would be a bottleneck; asynchronous output would both |
| 50 |
// be complex due to threading, and also need to address callers that have their |
| 51 |
// own output (for example, commit signing may subprocess and prompt for |
| 52 |
// passphrases). |
| 53 |
// |
| 54 |
// When something is slow, this means the displayed message may be from prior to |
| 55 |
// a slow step. Progress messages are an approximate indicator. |
| 56 |
impl<'a> ProgressWriter<'a> { |
| 57 |
pub fn new(ui: &Ui, prefix: &'a str) -> Option<Self> { |
| 58 |
let output = ui.progress_output()?; |
| 59 |
|
| 60 |
// Don't clutter the output during fast operations. |
| 61 |
let next_display_time = Instant::now() + INITIAL_DELAY; |
| 62 |
Some(Self { |
| 63 |
prefix, |
| 64 |
guard: None, |
| 65 |
output, |
| 66 |
next_display_time, |
| 67 |
}) |
| 68 |
} |
| 69 |
|
| 70 |
pub fn display(&mut self, text: &str) -> io::Result<()> { |
| 71 |
let now = Instant::now(); |
| 72 |
if now < self.next_display_time { |
| 73 |
return Ok(()); |
| 74 |
} |
| 75 |
|
| 76 |
self.next_display_time = now + Duration::from_secs(1) / UPDATE_HZ; |
| 77 |
|
| 78 |
if self.guard.is_none() { |
| 79 |
self.guard = Some( |
| 80 |
self.output |
| 81 |
.output_guard(format!("\r{}", Clear(ClearType::CurrentLine))), |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
let line_width = self.output.term_width().map(usize::from).unwrap_or(80); |
| 86 |
let max_path_width = self.prefix.len() + 1; // Take into account the empty space added after the prefix. |
| 87 |
let (display_text, _) = |
| 88 |
text_util::elide_start(text, "...", line_width.saturating_sub(max_path_width)); |
| 89 |
|
| 90 |
write!( |
| 91 |
self.output, |
| 92 |
"\r{}{} {display_text}", |
| 93 |
Clear(ClearType::CurrentLine), |
| 94 |
self.prefix |
| 95 |
)?; |
| 96 |
self.output.flush() |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
pub fn snapshot_progress(ui: &Ui) -> Option<impl Fn(&RepoPath) + use<>> { |
| 101 |
let writer = Mutex::new(ProgressWriter::new(ui, "Snapshotting")?); |
| 102 |
|
| 103 |
Some(move |path: &RepoPath| { |
| 104 |
// When the lock is held, skip updates to reduce contention. |
| 105 |
// |
| 106 |
// Executing the "future work" above may change this locking. Check |
| 107 |
// performance with output going to a tty so that writes are slower and |
| 108 |
// any lock contention is more visible. |
| 109 |
if let Ok(mut progress) = writer.try_lock() { |
| 110 |
progress |
| 111 |
.display(path.to_fs_path_unchecked(Path::new("")).to_str().unwrap()) |
| 112 |
.ok(); |
| 113 |
} |
| 114 |
}) |
| 115 |
} |