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)
| 162 | /// Note that generally the called function should not output |
| 163 | /// anything to stderr as this will interfere with the spinner. |
| 164 | pub(crate) async fn async_task_with_spinner<F, T>(msg: &str, f: F) -> T |
| 165 | where |
| 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... |
no test coverage detected