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    const fn new() -> Self {
17        StatusBoard {
18            line_width: AtomicUsize::new(0),
19        }
20    }
21
22    /// Get the number of characters of the current line.
23    fn get_line_width(&self) -> usize {
24        self.line_width.load(Ordering::Relaxed)
25    }
26
27    /// Set the number of characters of the current line.
28    fn set_line_width(&self, value: usize) {
29        self.line_width.store(value, Ordering::Relaxed);
30    }
31
32    /// Clear the line that the cursor is pointing to.
33    pub fn clear_line(&self, new_line_width: usize) {
34        let empty_line = repeat(' ', self.get_line_width());
35        eprint!("\r{empty_line}\r");
36        self.set_line_width(new_line_width);
37    }
38
39    /// Show a temporary message.
40    pub fn temporary_message(&self, message: &str) {
41        self.clear_line(message.width());
42        eprint!("{message}");
43    }
44
45    /// Log a permanent message.
46    pub fn permanent_message(&self, message: &str) {
47        self.clear_line(0);
48        eprintln!("{message}");
49    }
50}