termal_core 5.0.0

This library contains implementation for the termal library
Documentation
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
mod bar;
mod bar_theme;
mod dots;
mod iter;
mod no_state;
mod progress_ext;
mod progress_formatter;
mod state_progress;
mod update_policy;

use std::{
    borrow::Cow,
    fmt::{Debug, Display, Write},
    mem,
    time::{Duration, Instant},
};

pub use self::{
    bar::*, bar_theme::*, dots::*, iter::*, no_state::*, progress_ext::*,
    progress_formatter::*, state_progress::*, update_policy::*,
};

/// Track progress with progress bar.
pub fn track_bar<T>(task: &str, f: impl FnOnce(&mut ProgressBar) -> T) -> T {
    let mut pb = ProgressBar::bar(task);
    pb.track_this(f)
}

/// Track fallible progress with progress bar.
pub fn try_track_bar<T, E: Display>(
    task: &str,
    f: impl FnOnce(&mut ProgressBar) -> Result<T, E>,
) -> Result<T, E> {
    let mut pb = ProgressBar::bar(task);
    pb.try_track_this(f)
}

/// Track progress with progress dots.
pub fn track_dots<T>(task: &str, f: impl FnOnce(&mut ProgressDots) -> T) -> T {
    let mut pb = ProgressDots::dots(task);
    pb.track_this(f)
}

/// Track fallible progress with progress dots.
pub fn try_track_dots<T, E: Display>(
    task: &str,
    f: impl FnOnce(&mut ProgressDots) -> Result<T, E>,
) -> Result<T, E> {
    let mut pb = ProgressDots::dots(task);
    pb.try_track_this(f)
}

/// A progress bar progress.
type ProgressBar<'a, T = DefaultBarTheme, S = NoState> =
    Progress<'a, Bar<T>, S>;

/// Progress with three dots.
type ProgressDots<'a, S = NoState> = Progress<'a, Dots, S>;

/// The core type for tracking progress.
#[derive(Debug, Clone)]
pub struct Progress<'a, F, S = NoState> {
    formatter: F,
    task: Cow<'a, str>,
    pos: Option<f32>,
    state_str: String,
    start_time: Instant,
    update: Update,
    state: S,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Update {
    Iterations { every: usize, remain: usize },
    Time { every: Duration, last: Instant },
}

impl<'a, T: BarTheme + Default, S: Display + Default> ProgressBar<'a, T, S> {
    pub fn bar(task: impl Into<Cow<'a, str>>) -> Self {
        Self::new(Bar::default(), task, S::default())
    }
}

impl<'a, S: Display + Default> ProgressDots<'a, S> {
    pub fn dots(task: impl Into<Cow<'a, str>>) -> Self {
        Self::new(Dots::default(), task, S::default())
    }
}

impl<'a, F: ProgressFormatter, S: Display> Progress<'a, F, S> {
    /// Create new progress tracker.
    ///
    /// - `formatter` decides how progress is displayed.
    /// - `task` name of the task.
    /// - `state` additional state information.
    pub fn new(formatter: F, task: impl Into<Cow<'a, str>>, state: S) -> Self {
        let now = Instant::now();
        Self {
            formatter,
            pos: Some(0.),
            task: task.into(),
            state_str: String::new(),
            start_time: now,
            update: Update::new(UpdatePolicy::default(), now),
            state,
        }
    }

    /// Mutate the state.
    ///
    /// State contains additional information about the progress.
    pub fn state_mut(&mut self) -> &mut S {
        self.state_str.clear();
        &mut self.state
    }

    /// Set the state.
    ///
    /// State contains additional information about the progress.
    pub fn set_state(&mut self, state: S) -> S {
        self.state_str.clear();
        mem::replace(&mut self.state, state)
    }

    /// Set the update policy.
    ///
    /// The update policy decides how often should the progress update. The
    /// default updates every 100 ms.
    pub fn set_update_policy(&mut self, policy: impl Into<UpdatePolicy>) {
        self.update = Update::new(policy.into(), self.start_time);
    }

    /// Set this progress to track the new task.
    pub fn track<T>(
        &mut self,
        task: impl Into<Cow<'a, str>>,
        f: impl FnOnce(&mut Progress<F, S>) -> T,
    ) -> T {
        self.reuse(task);
        self.track_this(f)
    }

    /// Set this progress to try track the new task.
    pub fn try_track<T, E: Display>(
        &mut self,
        task: impl Into<Cow<'a, str>>,
        f: impl FnOnce(&mut Progress<F, S>) -> Result<T, E>,
    ) -> Result<T, E> {
        self.reuse(task);
        self.try_track_this(f)
    }

    /// Track this progress.
    pub fn track_this<T>(
        &mut self,
        f: impl FnOnce(&mut Progress<F, S>) -> T,
    ) -> T {
        self.start();
        let res = f(self);
        self.finish();
        res
    }

    /// Try to track this process.
    pub fn try_track_this<T, E: Display>(
        &mut self,
        f: impl FnOnce(&mut Progress<F, S>) -> Result<T, E>,
    ) -> Result<T, E> {
        self.start();
        match f(self) {
            res @ Ok(_) => {
                self.finish();
                res
            }
            Err(e) => {
                self.fail(&format!("{e}"));
                Err(e)
            }
        }
    }

    /// Reuse this progress for another task.
    pub fn reuse(&mut self, task: impl Into<Cow<'a, str>>) {
        self.task = task.into();
        self.pos = Some(0.);
        let now = Instant::now();
        self.start_time = now;
        self.update.reset(now);
    }

    /// Start the task. This is not required to be called.
    pub fn start(&mut self) {
        let now = Instant::now();
        self.start_time = now;
        self.update.reset(now);

        self.update_state_str();
        self.formatter.start(&self.task, &self.state_str);
    }

    /// Update the progress.
    ///
    /// This respects the update policy so it may skip the update.
    pub fn update(&mut self, progress: impl FnOnce() -> f32) {
        let Some(now) = self.update.should() else {
            return;
        };
        self.update_inner(now, progress());
    }

    /// Update the progress with the given ratio.
    ///
    /// This respects the update policy so it may skip the update.
    pub fn update_of(&mut self, cur: usize, of: usize) {
        self.update(|| cur as f32 / of as f32)
    }

    /// Force the progress update.
    ///
    /// This doesn't respect the update policy and so it will always update.
    pub fn force_update(&mut self, progress: f32) {
        let now = Instant::now();
        self.update.reset(now);
        self.update_inner(now, progress);
    }

    /// Update the state and progress according to the state.
    ///
    /// This respects the update policy so it may skip the update.
    pub fn state_update(&mut self, state: S)
    where
        S: StateProgress,
    {
        self.set_state(state);
        let Some(now) = self.update.should() else {
            return;
        };
        self.update_inner(now, self.state.progress());
    }

    /// Update the state and progress according to the state.
    ///
    /// This respects the update policy so it may skip the update.
    pub fn state_update_mut(&mut self, f: impl FnOnce(&mut S))
    where
        S: StateProgress,
    {
        f(self.state_mut());
        let Some(now) = self.update.should() else {
            return;
        };
        self.update_inner(now, self.state.progress());
    }

    /// Update the state and progress according to the state.
    ///
    /// This doesn't respect the update policy and so it will always update.
    pub fn force_state_update(&mut self, state: S)
    where
        S: StateProgress,
    {
        self.set_state(state);
        let now = Instant::now();
        self.update.reset(now);
        self.update_inner(now, self.state.progress());
    }

    /// Update the state and progress according to the state.
    ///
    /// This doesn't respect the update policy and so it will always update.
    pub fn force_state_update_mut(&mut self, f: impl FnOnce(&mut S))
    where
        S: StateProgress,
    {
        f(self.state_mut());
        let now = Instant::now();
        self.update.reset(now);
        self.update_inner(now, self.state.progress());
    }

    fn update_inner(&mut self, now: Instant, p: f32) {
        self.pos = Some(p);
        let elapsed = (now - self.start_time).as_secs_f32();
        let ets = (1. - p) * (elapsed / p);
        let eta = if ets > u64::MAX as f32 {
            Duration::ZERO
        } else {
            Duration::from_secs_f32(ets)
        };
        self.update_state_str();
        self.formatter
            .update(Some(p), &self.task, &self.state_str, eta);
    }

    /// Update with unknown progress.
    pub fn update_unknown(&mut self) {
        self.pos = None;
        let Some(now) = self.update.should() else {
            return;
        };
        let elapsed = now - self.start_time;
        self.update_state_str();
        self.formatter
            .update(None, &self.task, &self.state_str, elapsed);
    }

    /// Update with unknown progress.
    pub fn force_update_unknown(&mut self) {
        let now = Instant::now();
        self.update.reset(now);
        let elapsed = now - self.start_time;
        self.update_state_str();
        self.formatter
            .update(None, &self.task, &self.state_str, elapsed);
    }

    /// Finish the progress.
    pub fn finish(&mut self) {
        self.pos = None;
        let now = Instant::now();
        self.update.reset(now);
        let elapsed = now - self.start_time;
        self.update_state_str();
        self.formatter.finish(&self.task, &self.state_str, elapsed);
    }

    /// Finish the progress with the given error.
    pub fn fail(&mut self, err: &str) {
        let now = Instant::now();
        self.update.reset(now);
        let elapsed = now - self.start_time;
        self.update_state_str();
        self.formatter.fail(
            self.pos,
            &self.task,
            &self.state_str,
            elapsed,
            err,
        );
    }

    fn update_state_str(&mut self) {
        if self.state_str.is_empty() {
            // Writing to string is infallible.
            _ = write!(&mut self.state_str, "{}", self.state);
        }
    }
}

impl<'a, F, S> AsMut<Progress<'a, F, S>> for Progress<'a, F, S> {
    fn as_mut(&mut self) -> &mut Progress<'a, F, S> {
        self
    }
}

impl Update {
    pub fn new(policy: UpdatePolicy, now: Instant) -> Self {
        match policy {
            UpdatePolicy::Iterations(every) => Self::Iterations {
                every,
                remain: every,
            },
            UpdatePolicy::Time(every) => Self::Time { every, last: now },
        }
    }

    pub fn reset(&mut self, now: Instant) {
        match self {
            Update::Iterations { every, remain } => *remain = *every,
            Update::Time { last, .. } => *last = now,
        }
    }

    pub fn should(&mut self) -> Option<Instant> {
        match self {
            Update::Iterations { every, remain } => {
                if *remain == 0 {
                    *remain = *every;
                    Some(Instant::now())
                } else {
                    None
                }
            }
            Update::Time { every, last } => {
                let now = Instant::now();
                if now - *last >= *every {
                    *last = now;
                    Some(now)
                } else {
                    None
                }
            }
        }
    }
}

pub(crate) fn duration_to_string(
    dur: Duration,
    trunc: bool,
    res: &mut String,
) {
    // Number of seconds in the time frame
    const MIN: u64 = 60;
    const HOUR: u64 = 60 * MIN;
    const DAY: u64 = 24 * HOUR;

    let mut secs = dur.as_secs();

    let d = secs / DAY;
    secs %= DAY;
    let h = secs / HOUR;
    secs %= HOUR;
    let m = secs / MIN;
    secs %= MIN;

    if d != 0 {
        _ = write!(res, "{d}d");
    }
    if h != 0 {
        _ = write!(res, "{h:02}:");
    }
    if trunc {
        _ = write!(res, "{m:02}:{secs:02}");
    } else {
        _ = write!(res, "{m:02}:{secs:02}");
        if dur.subsec_nanos() != 0 {
            let s = dur.subsec_nanos().to_string();
            res.push('.');
            res.extend(std::iter::repeat_n('0', 9 - s.len()));
            *res += s.trim_end_matches('0');
        }
    }
}