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
165
166
167
168
169
170
171
172
173
174
use std::io::Stdout;
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime};

use bytesize::ByteSize;
use ratatui::{backend::CrosstermBackend, Terminal};
use rustic_core::{Progress, ProgressBars};

use super::widgets::{popup_gauge, popup_text, Draw};

#[derive(Clone)]
pub struct TuiProgressBars {
    pub terminal: Arc<RwLock<Terminal<CrosstermBackend<Stdout>>>>,
}

impl TuiProgressBars {
    fn as_progress(&self, progress_type: TuiProgressType, prefix: String) -> TuiProgress {
        TuiProgress {
            terminal: self.terminal.clone(),
            data: Arc::new(RwLock::new(CounterData::new(prefix))),
            progress_type,
        }
    }
}

impl ProgressBars for TuiProgressBars {
    type P = TuiProgress;
    fn progress_hidden(&self) -> Self::P {
        self.as_progress(TuiProgressType::Hidden, String::new())
    }
    fn progress_spinner(&self, prefix: impl Into<std::borrow::Cow<'static, str>>) -> Self::P {
        let progress = self.as_progress(TuiProgressType::Spinner, String::from(prefix.into()));
        progress.popup();
        progress
    }
    fn progress_counter(&self, prefix: impl Into<std::borrow::Cow<'static, str>>) -> Self::P {
        let progress = self.as_progress(TuiProgressType::Counter, String::from(prefix.into()));
        progress.popup();
        progress
    }
    fn progress_bytes(&self, prefix: impl Into<std::borrow::Cow<'static, str>>) -> Self::P {
        let progress = self.as_progress(TuiProgressType::Bytes, String::from(prefix.into()));
        progress.popup();
        progress
    }
}

struct CounterData {
    prefix: String,
    begin: SystemTime,
    length: Option<u64>,
    count: u64,
}

impl CounterData {
    fn new(prefix: String) -> Self {
        Self {
            prefix,
            begin: SystemTime::now(),
            length: None,
            count: 0,
        }
    }
}

#[derive(Clone)]
enum TuiProgressType {
    Hidden,
    Spinner,
    Counter,
    Bytes,
}

#[derive(Clone)]
pub struct TuiProgress {
    terminal: Arc<RwLock<Terminal<CrosstermBackend<Stdout>>>>,
    data: Arc<RwLock<CounterData>>,
    progress_type: TuiProgressType,
}

fn fmt_duration(d: Duration) -> String {
    let seconds = d.as_secs();
    let (minutes, seconds) = (seconds / 60, seconds % 60);
    let (hours, minutes) = (minutes / 60, minutes % 60);
    format!("[{hours:02}:{minutes:02}:{seconds:02}]")
}

impl TuiProgress {
    fn popup(&self) {
        let data = self.data.read().unwrap();
        let elapsed = data.begin.elapsed().unwrap();
        let length = data.length;
        let count = data.count;
        let ratio = match length {
            None | Some(0) => 0.0,
            Some(l) => count as f64 / l as f64,
        };
        let eta = match ratio {
            r if r < 0.01 => " ETA: -".to_string(),
            r if r > 0.999999 => String::new(),
            r => {
                format!(
                    " ETA: {}",
                    fmt_duration(Duration::from_secs(1) + elapsed.div_f64(r / (1.0 - r)))
                )
            }
        };
        let prefix = &data.prefix;
        let message = match self.progress_type {
            TuiProgressType::Spinner => {
                format!("{} {prefix}", fmt_duration(elapsed))
            }
            TuiProgressType::Counter => {
                format!(
                    "{} {prefix} {}{}{eta}",
                    fmt_duration(elapsed),
                    count,
                    length.map_or(String::new(), |l| format!("/{l}"))
                )
            }
            TuiProgressType::Bytes => {
                format!(
                    "{} {prefix} {}{}{eta}",
                    fmt_duration(elapsed),
                    ByteSize(count).to_string_as(true),
                    length.map_or(String::new(), |l| format!(
                        "/{}",
                        ByteSize(l).to_string_as(true)
                    ))
                )
            }
            TuiProgressType::Hidden => String::new(),
        };
        drop(data);

        let mut terminal = self.terminal.write().unwrap();
        _ = terminal
            .draw(|f| {
                let area = f.area();
                match self.progress_type {
                    TuiProgressType::Hidden => {}
                    TuiProgressType::Spinner => {
                        let mut popup = popup_text("progress", message.into());
                        popup.draw(area, f);
                    }
                    TuiProgressType::Counter | TuiProgressType::Bytes => {
                        let mut popup = popup_gauge("progress", message.into(), ratio);
                        popup.draw(area, f);
                    }
                }
            })
            .unwrap();
    }
}

impl Progress for TuiProgress {
    fn is_hidden(&self) -> bool {
        matches!(self.progress_type, TuiProgressType::Hidden)
    }
    fn set_length(&self, len: u64) {
        self.data.write().unwrap().length = Some(len);
        self.popup();
    }
    fn set_title(&self, title: &'static str) {
        self.data.write().unwrap().prefix = String::from(title);
        self.popup();
    }

    fn inc(&self, inc: u64) {
        self.data.write().unwrap().count += inc;
        self.popup();
    }
    fn finish(&self) {}
}