Skip to content

Commit 7acd8b2

Browse files
djcrami3l
authored andcommitted
Decouple Cfg from Notifier initialization
1 parent c19aae9 commit 7acd8b2

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/cli/common.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt::Display;
55
use std::fs;
66
use std::io::{BufRead, ErrorKind, Write};
77
use std::path::{Path, PathBuf};
8-
use std::sync::Arc;
8+
use std::sync::{Arc, Mutex};
99
use std::{cmp, env};
1010

1111
use anyhow::{anyhow, Context, Result};
@@ -167,23 +167,34 @@ impl NotifyOnConsole {
167167
}
168168
}
169169

170+
struct Notifier {
171+
tracker: Arc<Mutex<DownloadTracker>>,
172+
notifier: RefCell<NotifyOnConsole>,
173+
}
174+
175+
impl Notifier {
176+
fn new(verbose: bool, quiet: bool) -> Self {
177+
Self {
178+
tracker: DownloadTracker::new_with_display_progress(!quiet),
179+
notifier: RefCell::new(NotifyOnConsole {
180+
verbose,
181+
..Default::default()
182+
}),
183+
}
184+
}
185+
186+
fn handle(&self, n: Notification<'_>) {
187+
if self.tracker.lock().unwrap().handle_notification(&n) {
188+
return;
189+
}
190+
self.notifier.borrow_mut().handle(n);
191+
}
192+
}
193+
170194
#[cfg_attr(feature = "otel", tracing::instrument)]
171195
pub(crate) fn set_globals(current_dir: PathBuf, verbose: bool, quiet: bool) -> Result<Cfg> {
172-
let download_tracker = DownloadTracker::new_with_display_progress(!quiet);
173-
let console_notifier = RefCell::new(NotifyOnConsole {
174-
verbose,
175-
..Default::default()
176-
});
177-
178-
Cfg::from_env(
179-
current_dir,
180-
Arc::new(move |n: Notification<'_>| {
181-
if download_tracker.lock().unwrap().handle_notification(&n) {
182-
return;
183-
}
184-
console_notifier.borrow_mut().handle(n);
185-
}),
186-
)
196+
let notifier = Notifier::new(verbose, quiet);
197+
Cfg::from_env(current_dir, Arc::new(move |n| notifier.handle(n)))
187198
}
188199

189200
pub(crate) fn show_channel_update(

0 commit comments

Comments
 (0)