File: simple.rs

package info (click to toggle)
rust-progressing 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 168 kB
  • sloc: sh: 10; makefile: 4
file content (112 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download
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
use progressing::{
    bernoulli::Bar as BernoulliBar, clamping::Bar as ClampingBar, mapping::Bar as MappingBar,
    Baring,
};

fn main() {
    // different examples for different use-cases
    clamped();
    println!();
    mapped();
    println!();
    timed_mapped();
    println!();
    bernoulli();
    println!();
    styles();
    println!();
    remember_progress();
}

/// Printing value 0.3 clamped to [0, 1]
/// [=====>------------]
fn clamped() {
    println!("Printing value 0.3 clamped to [0, 1]");
    let mut progress_bar = ClampingBar::new();
    progress_bar.set_len(20);
    progress_bar.set(0.3);
    println!("{}", progress_bar);
}

/// Mapping from [-9, 5] to [0, 1]
/// [================>-] (4 / 5)
fn mapped() {
    println!("Mapping from [-9, 5] to [0, 1]");
    let mut progress_bar = MappingBar::with_range(-9, 5);
    progress_bar.set_len(20);
    progress_bar.set(4);
    println!("{}", progress_bar);
}

/// Mapping from [-9, 5] to [0, 1], but with time-approximation
/// [================>-] (4 / 5) ~ 2 min
fn timed_mapped() {
    println!("Mapping from [-9, 5] to [0, 1], but with time-approximation");
    let mut progress_bar = MappingBar::with_range(-9, 5).timed();
    progress_bar.set_len(20);
    progress_bar.set(4);
    println!("{}", progress_bar);
}

/// Bernoulli-Bar counting successes (42 / 60) and attempts (# 130)
/// [============>-----] (42 / 60 # 130)
fn bernoulli() {
    println!("Bernoulli-Bar counting successes (42 / 60) and attempts (# 130)");
    let mut progress_bar = BernoulliBar::with_goal(60);
    progress_bar.set_len(20);
    progress_bar.set((42, 130));
    progress_bar.add(true);
    println!("{}", progress_bar);

    let is_successful = true;
    if is_successful {
        // Does increase both 42 and 130
        progress_bar.add(true);
    } else {
        // Does increase 130 only
        progress_bar.add(false);
    }
}

/// clamped-example, but with other styles
fn styles() {
    println!("Custom styles");
    let mut progress_bar = ClampingBar::new();
    progress_bar.set_len(20);
    progress_bar.set(0.3);

    // different custom styles are possible

    // prints (----->............)
    progress_bar.set_style("(->.)");
    println!("{}", progress_bar);

    // prints [#####             ]
    progress_bar.set_style("[#  ]");
    println!("{}", progress_bar);

    // prints [#####-------------]
    progress_bar.set_style("(#--)");
    println!("{}", progress_bar);
}

fn remember_progress() {
    println!("Looped progress");

    // create bar
    let mut progress_bar = BernoulliBar::with_goal(100).timed();
    progress_bar.set_len(20);
    progress_bar.set(13);

    // do the job and show progress
    for _ in 0..100 {
        progress_bar.add(true);
        if progress_bar.has_progressed_significantly() {
            progress_bar.remember_significant_progress();
            println!("{}", progress_bar);
        }

        std::thread::sleep(std::time::Duration::from_millis(100));
    }
    println!("{}", progress_bar);
}