progressing/lib.rs
1pub mod bernoulli;
2pub mod clamping;
3pub mod mapping;
4pub mod timing;
5
6use std::ops::Add;
7
8/// A trait describing basic functionality for simple text-based progress-bars.
9///
10///
11/// # Mini-Examples
12///
13/// ```
14/// use progressing::{
15/// bernoulli::Bar as BernoulliBar, clamping::Bar as ClampingBar, mapping::Bar as MappingBar,
16/// Baring,
17/// };
18///
19/// /// Printing value 0.3 clamped to [0, 1]
20/// /// [=====>------------]
21/// fn clamped() {
22/// println!("Printing value 0.3 clamped to [0, 1]");
23/// let mut progress_bar = ClampingBar::new();
24/// progress_bar.set_len(20);
25/// progress_bar.set(0.3);
26/// println!("{}", progress_bar);
27/// }
28///
29/// /// Mapping from [-9, 5] to [0, 1]
30/// /// [================>-] (4 / 5)
31/// fn mapped() {
32/// println!("Mapping from [-9, 5] to [0, 1]");
33/// let mut progress_bar = MappingBar::with_range(-9, 5);
34/// progress_bar.set_len(20);
35/// progress_bar.set(4);
36/// println!("{}", progress_bar);
37/// }
38///
39/// /// Bernoulli-Bar counting successes (42 / 60) and attempts (# 130)
40/// /// [============>-----] (42 / 60 # 130)
41/// fn bernoulli() {
42/// println!("Bernoulli-Bar counting successes (42 / 60) and attempts (# 130)");
43/// let mut progress_bar = BernoulliBar::with_goal(60);
44/// progress_bar.set_len(20);
45/// progress_bar.set((42, 130));
46/// println!("{}", progress_bar);
47/// }
48///
49/// fn main() {
50/// clamped();
51/// println!();
52/// mapped();
53/// println!();
54/// bernoulli();
55/// }
56/// ```
57pub trait Baring {
58 type Progress: Add<Output = Self::Progress>;
59
60 fn len(&self) -> usize;
61
62 /// Do not shorten the length before reprinting ("\r") since the line will be overwritten, not cleared.
63 ///
64 /// `[========>-]` becomes `[====>]==>-]` instead of `[====>] `.
65 fn set_len(&mut self, new_bar_len: usize);
66
67 fn progress(&self) -> Self::Progress;
68
69 /// Sets the progress to the given value
70 fn set<P>(&mut self, new_progress: P)
71 where
72 P: Into<Self::Progress>;
73
74 /// Adds the given progress to the current progress
75 fn add<P>(&mut self, delta: P)
76 where
77 P: Into<Self::Progress>,
78 {
79 self.set(self.progress() + delta.into());
80 }
81
82 fn start(&self) -> Self::Progress;
83
84 fn end(&self) -> Self::Progress;
85
86 fn has_progressed_significantly(&self) -> bool;
87
88 fn remember_significant_progress(&mut self);
89}