1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{
    fmt,
    io::{self, Write},
    sync::{
        atomic::{AtomicU64, Ordering},
        mpsc::{self, Sender, TryRecvError},
        Arc,
    },
    thread::{self, JoinHandle},
    time::Duration,
};

use necsim_core::{impl_report, reporter::Reporter};

struct ProgressUpdater {
    thread: JoinHandle<()>,
    sender: Sender<()>,
}

#[allow(clippy::module_name_repetitions)]
pub struct ProgressReporter {
    updater: Option<ProgressUpdater>,
    last_remaining: Arc<AtomicU64>,
    last_total: Arc<AtomicU64>,
}

impl fmt::Debug for ProgressReporter {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        struct Progress(u64, u64);

        impl fmt::Debug for Progress {
            fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
                write!(fmt, "{} / {}", self.0, self.1)
            }
        }

        let total = self.last_total.load(Ordering::Acquire);
        let remaining = self.last_remaining.load(Ordering::Acquire).min(total);

        fmt.debug_struct(stringify!(ProgressReporter))
            .field("progress", &Progress(total - remaining, total))
            .finish_non_exhaustive()
    }
}

impl serde::Serialize for ProgressReporter {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_unit()
    }
}

impl<'de> serde::Deserialize<'de> for ProgressReporter {
    fn deserialize<D: serde::Deserializer<'de>>(_deserializer: D) -> Result<Self, D::Error> {
        Ok(Self::default())
    }
}

impl Reporter for ProgressReporter {
    impl_report!(speciation(&mut self, _speciation: Ignored) {});

    impl_report!(dispersal(&mut self, _dispersal: Ignored) {});

    impl_report!(progress(&mut self, remaining: Used) {
        let last_remaining = self.last_remaining.swap(*remaining, Ordering::AcqRel);

        // Update the progress total in case of regression
        if last_remaining < *remaining {
            self.last_total
                .fetch_add(remaining - last_remaining, Ordering::AcqRel);
        }

        if last_remaining > 0 && *remaining == 0 {
            let total = self.last_total.load(Ordering::Acquire);

            display_progress(total, self.last_remaining.load(Ordering::Acquire).min(total));

            // Flush stdout to update the progress bar
            std::mem::drop(io::stdout().flush());
        }
    });

    fn initialise(&mut self) -> Result<(), String> {
        if self.updater.is_some() {
            return Ok(());
        }

        let remaining = Arc::clone(&self.last_remaining);
        let total = Arc::clone(&self.last_total);

        let (sender, receiver) = mpsc::channel();

        let thread = thread::spawn(move || {
            loop {
                thread::sleep(Duration::from_millis(100));

                match receiver.try_recv() {
                    Ok(()) | Err(TryRecvError::Disconnected) => break,
                    Err(TryRecvError::Empty) => {},
                }

                let total = total.load(Ordering::Acquire);

                if total > 0 {
                    display_progress(total, remaining.load(Ordering::Acquire).min(total));

                    // Flush stdout to update the progress bar
                    std::mem::drop(io::stdout().flush());
                }
            }
        });

        self.updater = Some(ProgressUpdater { thread, sender });

        Ok(())
    }
}

impl Drop for ProgressReporter {
    fn drop(&mut self) {
        if let Some(updater) = self.updater.take() {
            if updater.sender.send(()).is_ok() {
                std::mem::drop(updater.thread.join());
            }
        }
    }
}

impl Default for ProgressReporter {
    fn default() -> Self {
        let last_remaining = Arc::new(AtomicU64::new(0_u64));
        let last_total = Arc::new(AtomicU64::new(0_u64));

        Self {
            updater: None,
            last_remaining,
            last_total,
        }
    }
}

fn display_progress(total: u64, remaining: u64) {
    const UPDATE_PRECISION: usize = 50;

    #[allow(clippy::cast_possible_truncation)]
    let display_progress =
        ((total - remaining) * (UPDATE_PRECISION as u64) / total.max(1)) as usize;

    // Display a simple progress bar to stdout
    print!("\r{:>13} [", total - remaining);
    if display_progress == 0 {
        print!("{:>UPDATE_PRECISION$}", "");
    } else if remaining > 0 {
        print!(
            "{:=<progress$}>{:>rest$}",
            "",
            "",
            progress = (display_progress - 1),
            rest = (UPDATE_PRECISION - display_progress)
        );
    } else {
        print!("{:=<UPDATE_PRECISION$}", "");
    }
    print!("] {total:<13}");
}