|
| 1 | +use std::{ |
| 2 | + cell::RefCell, |
| 3 | + sync::mpsc::{Sender, TryRecvError}, |
| 4 | + thread::ThreadId, |
| 5 | + time::Duration, |
| 6 | +}; |
| 7 | + |
| 8 | +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
| 9 | +use rustc_data_structures::{fx::FxHashMap, sync::IntoDynSyncSend}; |
| 10 | + |
| 11 | +use crate::Session; |
| 12 | + |
| 13 | +thread_local! { |
| 14 | + static CURRENT_SPINNER: RefCell<Option<(ProgressBar, usize)>> = RefCell::new(None); |
| 15 | +} |
| 16 | + |
| 17 | +pub struct ProgressBars { |
| 18 | + sender: IntoDynSyncSend<Sender<Msg>>, |
| 19 | +} |
| 20 | + |
| 21 | +enum Msg { |
| 22 | + Pop { thread: ThreadId }, |
| 23 | + Push { thread: ThreadId, name: &'static str }, |
| 24 | +} |
| 25 | + |
| 26 | +impl Session { |
| 27 | + /// Starts up a thread that makes sure all the threads' messages are collected and processed |
| 28 | + /// in one central location, and thus rendered correctly. |
| 29 | + pub(crate) fn init_progress_bars() -> ProgressBars { |
| 30 | + let (sender, receiver) = std::sync::mpsc::channel(); |
| 31 | + std::thread::spawn(move || { |
| 32 | + let bars = MultiProgress::new(); |
| 33 | + let mut threads: FxHashMap<ThreadId, Vec<_>> = FxHashMap::default(); |
| 34 | + 'outer: loop { |
| 35 | + std::thread::sleep(Duration::from_millis(100)); |
| 36 | + loop { |
| 37 | + match receiver.try_recv() { |
| 38 | + Ok(val) => match val { |
| 39 | + Msg::Pop { thread } => { |
| 40 | + threads.get_mut(&thread).unwrap().pop(); |
| 41 | + } |
| 42 | + Msg::Push { thread, name } => { |
| 43 | + let stack = threads.entry(thread).or_default(); |
| 44 | + |
| 45 | + let mut template = String::new(); |
| 46 | + use std::fmt::Write; |
| 47 | + if !stack.is_empty() { |
| 48 | + for _ in 1..stack.len() { |
| 49 | + write!(template, " ").unwrap(); |
| 50 | + } |
| 51 | + write!(template, "└").unwrap(); |
| 52 | + } |
| 53 | + write!(template, "{{spinner}} {{msg}}").unwrap(); |
| 54 | + |
| 55 | + let spinner = ProgressBar::new_spinner() |
| 56 | + .with_message(name) |
| 57 | + .with_style(ProgressStyle::with_template(&template).unwrap()); |
| 58 | + let spinner = bars.add(spinner); |
| 59 | + stack.push(spinner) |
| 60 | + } |
| 61 | + }, |
| 62 | + Err(TryRecvError::Disconnected) => break 'outer, |
| 63 | + Err(TryRecvError::Empty) => break, |
| 64 | + } |
| 65 | + } |
| 66 | + for thread in threads.values() { |
| 67 | + for spinner in thread { |
| 68 | + spinner.tick() |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + ProgressBars { sender: IntoDynSyncSend(sender) } |
| 74 | + } |
| 75 | + |
| 76 | + /// Append a new spinner to the current stack |
| 77 | + pub fn push_spinner(&self, bars: &ProgressBars, name: &'static str) -> impl Sized { |
| 78 | + let thread = std::thread::current().id(); |
| 79 | + bars.sender.send(Msg::Push { thread, name }).unwrap(); |
| 80 | + struct Spinner(Sender<Msg>, ThreadId); |
| 81 | + impl Drop for Spinner { |
| 82 | + fn drop(&mut self) { |
| 83 | + self.0.send(Msg::Pop { thread: self.1 }).unwrap(); |
| 84 | + } |
| 85 | + } |
| 86 | + Spinner(bars.sender.0.clone(), thread) |
| 87 | + } |
| 88 | +} |
0 commit comments