parallel_disk_usage/
status_board.rs

1use fmt_iter::repeat;
2use std::sync::atomic::{AtomicUsize, Ordering};
3use zero_copy_pads::Width;
4
5/// Control all status indicators in stderr.
6pub static GLOBAL_STATUS_BOARD: StatusBoard = StatusBoard::new();
7
8/// Control all status indicators in stderr.
9#[derive(Debug)]
10pub struct StatusBoard {
11    line_width: AtomicUsize,
12}
13
14impl StatusBoard {
15    /// Create a new [`StatusBoard`].
16    #[inline]
17    const fn new() -> Self {
18        StatusBoard {
19            line_width: AtomicUsize::new(0),
20        }
21    }
22
23    /// Get the number of characters of the current line.
24    #[inline]
25    fn get_line_width(&self) -> usize {
26        self.line_width.load(Ordering::Relaxed)
27    }
28
29    /// Set the number of characters of the current line.
30    #[inline]
31    fn set_line_width(&self, value: usize) {
32        self.line_width.store(value, Ordering::Relaxed);
33    }
34
35    /// Clear the line that the cursor is pointing to.
36    pub fn clear_line(&self, new_line_width: usize) {
37        let empty_line = repeat(' ', self.get_line_width());
38        eprint!("\r{empty_line}\r");
39        self.set_line_width(new_line_width);
40    }
41
42    /// Show a temporary message.
43    pub fn temporary_message(&self, message: &str) {
44        self.clear_line(message.width());
45        eprint!("{message}");
46    }
47
48    /// Log a permanent message.
49    pub fn permanent_message(&self, message: &str) {
50        self.clear_line(0);
51        eprintln!("{message}");
52    }
53}