simple/
simple.rs

1use progressing::{
2    bernoulli::Bar as BernoulliBar, clamping::Bar as ClampingBar, mapping::Bar as MappingBar,
3    Baring,
4};
5
6fn main() {
7    // different examples for different use-cases
8    clamped();
9    println!();
10    mapped();
11    println!();
12    timed_mapped();
13    println!();
14    bernoulli();
15    println!();
16    styles();
17    println!();
18    remember_progress();
19}
20
21/// Printing value 0.3 clamped to [0, 1]
22/// [=====>------------]
23fn clamped() {
24    println!("Printing value 0.3 clamped to [0, 1]");
25    let mut progress_bar = ClampingBar::new();
26    progress_bar.set_len(20);
27    progress_bar.set(0.3);
28    println!("{}", progress_bar);
29}
30
31/// Mapping from [-9, 5] to [0, 1]
32/// [================>-] (4 / 5)
33fn mapped() {
34    println!("Mapping from [-9, 5] to [0, 1]");
35    let mut progress_bar = MappingBar::with_range(-9, 5);
36    progress_bar.set_len(20);
37    progress_bar.set(4);
38    println!("{}", progress_bar);
39}
40
41/// Mapping from [-9, 5] to [0, 1], but with time-approximation
42/// [================>-] (4 / 5) ~ 2 min
43fn timed_mapped() {
44    println!("Mapping from [-9, 5] to [0, 1], but with time-approximation");
45    let mut progress_bar = MappingBar::with_range(-9, 5).timed();
46    progress_bar.set_len(20);
47    progress_bar.set(4);
48    println!("{}", progress_bar);
49}
50
51/// Bernoulli-Bar counting successes (42 / 60) and attempts (# 130)
52/// [============>-----] (42 / 60 # 130)
53fn bernoulli() {
54    println!("Bernoulli-Bar counting successes (42 / 60) and attempts (# 130)");
55    let mut progress_bar = BernoulliBar::with_goal(60);
56    progress_bar.set_len(20);
57    progress_bar.set((42, 130));
58    progress_bar.add(true);
59    println!("{}", progress_bar);
60
61    let is_successful = true;
62    if is_successful {
63        // Does increase both 42 and 130
64        progress_bar.add(true);
65    } else {
66        // Does increase 130 only
67        progress_bar.add(false);
68    }
69}
70
71/// clamped-example, but with other styles
72fn styles() {
73    println!("Custom styles");
74    let mut progress_bar = ClampingBar::new();
75    progress_bar.set_len(20);
76    progress_bar.set(0.3);
77
78    // different custom styles are possible
79
80    // prints (----->............)
81    progress_bar.set_style("(->.)");
82    println!("{}", progress_bar);
83
84    // prints [#####             ]
85    progress_bar.set_style("[#  ]");
86    println!("{}", progress_bar);
87
88    // prints [#####-------------]
89    progress_bar.set_style("(#--)");
90    println!("{}", progress_bar);
91}
92
93fn remember_progress() {
94    println!("Looped progress");
95
96    // create bar
97    let mut progress_bar = BernoulliBar::with_goal(100).timed();
98    progress_bar.set_len(20);
99    progress_bar.set(13);
100
101    // do the job and show progress
102    for _ in 0..100 {
103        progress_bar.add(true);
104        if progress_bar.has_progressed_significantly() {
105            progress_bar.remember_significant_progress();
106            println!("{}", progress_bar);
107        }
108
109        std::thread::sleep(std::time::Duration::from_millis(100));
110    }
111    println!("{}", progress_bar);
112}