Skip to content

Commit c67ac0e

Browse files
authored
Make CURRENT_REPORTER a Weak<ProgressReporter> (#1390)
1 parent 4a8b58b commit c67ac0e

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

crates/prek/src/cli/reporter.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::borrow::Cow;
2-
use std::sync::{Arc, Mutex};
2+
use std::sync::{Arc, Mutex, Weak};
33
use std::time::Duration;
44

55
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
@@ -12,20 +12,23 @@ use crate::printer::Printer;
1212
use crate::workspace;
1313

1414
/// Current progress reporter used to suspend rendering while printing normal output.
15-
static CURRENT_REPORTER: Mutex<Option<Arc<ProgressReporter>>> = Mutex::new(None);
15+
static CURRENT_REPORTER: Mutex<Option<Weak<ProgressReporter>>> = Mutex::new(None);
1616

1717
/// Set the current reporter for lock acquisition warnings.
18-
fn set_current_reporter(reporter: Option<Arc<ProgressReporter>>) {
19-
*CURRENT_REPORTER.lock().unwrap() = reporter;
18+
fn set_current_reporter(reporter: Option<&Arc<ProgressReporter>>) {
19+
*CURRENT_REPORTER.lock().unwrap() = reporter.map(Arc::downgrade);
2020
}
2121

2222
/// Suspend progress rendering while emitting normal output.
23+
///
24+
/// If a progress reporter is currently active, this runs `f` inside
25+
/// `indicatif::MultiProgress::suspend` to avoid corrupting the progress display.
26+
/// If no reporter is active (or it has already been dropped), this just runs `f`.
2327
pub(crate) fn suspend(f: impl FnOnce() + Send + 'static) {
2428
let reporter = CURRENT_REPORTER.lock().unwrap().clone();
25-
if let Some(reporter) = reporter {
26-
reporter.children.suspend(f);
27-
} else {
28-
f();
29+
match reporter.and_then(|r| r.upgrade()) {
30+
Some(reporter) => reporter.children.suspend(f),
31+
None => f(),
2932
}
3033
}
3134

@@ -116,7 +119,7 @@ pub(crate) struct HookInitReporter {
116119
impl HookInitReporter {
117120
pub(crate) fn new(printer: Printer) -> Self {
118121
let reporter = Arc::new(ProgressReporter::from(printer));
119-
set_current_reporter(Some(reporter.clone()));
122+
set_current_reporter(Some(&reporter));
120123
Self { reporter }
121124
}
122125
}
@@ -147,7 +150,7 @@ pub(crate) struct HookInstallReporter {
147150
impl HookInstallReporter {
148151
pub(crate) fn new(printer: Printer) -> Self {
149152
let reporter = Arc::new(ProgressReporter::from(printer));
150-
set_current_reporter(Some(reporter.clone()));
153+
set_current_reporter(Some(&reporter));
151154
Self { reporter }
152155
}
153156
}
@@ -182,7 +185,7 @@ pub(crate) struct HookRunReporter {
182185
impl HookRunReporter {
183186
pub fn new(printer: Printer, dots: usize) -> Self {
184187
let reporter = Arc::new(ProgressReporter::from(printer));
185-
set_current_reporter(Some(reporter.clone()));
188+
set_current_reporter(Some(&reporter));
186189

187190
Self { reporter, dots }
188191
}
@@ -252,7 +255,7 @@ pub(crate) struct AutoUpdateReporter {
252255
impl AutoUpdateReporter {
253256
pub(crate) fn new(printer: Printer) -> Self {
254257
let reporter = Arc::new(ProgressReporter::from(printer));
255-
set_current_reporter(Some(reporter.clone()));
258+
set_current_reporter(Some(&reporter));
256259
Self { reporter }
257260
}
258261
}

0 commit comments

Comments
 (0)