pijul_interaction/progress/
mod.rs

1mod terminal;
2
3use super::{ProgressBar, Spinner};
4use crate::{InteractionError, InteractiveContext};
5
6pub trait ProgressBarTrait: Send {
7    fn inc(&self, delta: u64);
8    fn finish(&self);
9    fn boxed_clone(&self) -> Box<dyn ProgressBarTrait>;
10}
11
12impl ProgressBar {
13    pub fn new<S: ToString>(len: u64, message: S) -> Result<ProgressBar, InteractionError> {
14        Ok(Self(match crate::get_context()? {
15            InteractiveContext::Terminal | InteractiveContext::NotInteractive => {
16                Box::new(terminal::new_progress(len, message.to_string()))
17            }
18        }))
19    }
20
21    pub fn inc(&self, delta: u64) {
22        self.0.inc(delta);
23    }
24
25    fn finish(&self) {
26        self.0.finish()
27    }
28}
29
30impl Drop for ProgressBar {
31    fn drop(&mut self) {
32        self.finish();
33    }
34}
35
36impl Clone for ProgressBar {
37    fn clone(&self) -> Self {
38        Self(self.0.boxed_clone())
39    }
40}
41
42pub trait SpinnerTrait: Send {
43    fn finish(&self);
44    fn boxed_clone(&self) -> Box<dyn SpinnerTrait>;
45}
46
47impl Spinner {
48    pub fn new<S: ToString>(message: S) -> Result<Spinner, InteractionError> {
49        Ok(Self(match crate::get_context()? {
50            InteractiveContext::Terminal | InteractiveContext::NotInteractive => {
51                Box::new(terminal::new_spinner(message.to_string()))
52            }
53        }))
54    }
55
56    fn finish(&self) {
57        self.0.finish();
58    }
59}
60
61impl Drop for Spinner {
62    fn drop(&mut self) {
63        self.finish();
64    }
65}
66
67impl Clone for Spinner {
68    fn clone(&self) -> Self {
69        Self(self.0.boxed_clone())
70    }
71}