MCPcopy Create free account
hub / github.com/bootc-dev/bootc / async_task_with_spinner

Function async_task_with_spinner

crates/lib/src/utils.rs:164–192  ·  view source on GitHub ↗

Call an async task function, and write a message to stderr with an automatic spinner to show that we're not blocked. Note that generally the called function should not output anything to stderr as this will interfere with the spinner.

(msg: &str, f: F)

Source from the content-addressed store, hash-verified

162/// Note that generally the called function should not output
163/// anything to stderr as this will interfere with the spinner.
164pub(crate) async fn async_task_with_spinner<F, T>(msg: &str, f: F) -> T
165where
166 F: Future<Output = T>,
167{
168 let start_time = std::time::Instant::now();
169 let pb = indicatif::ProgressBar::new_spinner();
170 let style = indicatif::ProgressStyle::default_bar();
171 pb.set_style(style.template("{spinner} {msg}").unwrap());
172 pb.set_message(msg.to_string());
173 pb.enable_steady_tick(Duration::from_millis(150));
174 // We need to handle the case where we aren't connected to
175 // a tty, so indicatif would show nothing by default.
176 if pb.is_hidden() {
177 eprint!("{msg}...");
178 std::io::stderr().flush().unwrap();
179 }
180 let r = f.await;
181 let elapsed = HumanDuration(start_time.elapsed());
182 let _ = journal_print(
183 libsystemd::logging::Priority::Info,
184 &format!("completed task in {elapsed}: {msg}"),
185 );
186 if pb.is_hidden() {
187 eprintln!("done ({elapsed})");
188 } else {
189 pb.finish_with_message(format!("{msg}: done ({elapsed})"));
190 }
191 r
192}
193
194/// Given a possibly tagged image like quay.io/foo/bar:latest and a digest 0ab32..., return
195/// the digested form quay.io/foo/bar:latest@sha256:0ab32...

Callers 5

install_containerFunction · 0.70
set_unified_composefsFunction · 0.70
set_unifiedFunction · 0.70
pull_images_implFunction · 0.70
deployFunction · 0.70

Calls 1

journal_printFunction · 0.85

Tested by

no test coverage detected