pub trait Baring {
type Progress: Add<Output = Self::Progress>;
// Required methods
fn len(&self) -> usize;
fn set_len(&mut self, new_bar_len: usize);
fn progress(&self) -> Self::Progress;
fn set<P>(&mut self, new_progress: P)
where P: Into<Self::Progress>;
fn start(&self) -> Self::Progress;
fn end(&self) -> Self::Progress;
fn has_progressed_significantly(&self) -> bool;
fn remember_significant_progress(&mut self);
// Provided method
fn add<P>(&mut self, delta: P)
where P: Into<Self::Progress> { ... }
}Expand description
A trait describing basic functionality for simple text-based progress-bars.
§Mini-Examples
use progressing::{
bernoulli::Bar as BernoulliBar, clamping::Bar as ClampingBar, mapping::Bar as MappingBar,
Baring,
};
/// 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);
}
/// 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));
println!("{}", progress_bar);
}
fn main() {
clamped();
println!();
mapped();
println!();
bernoulli();
}Required Associated Types§
Required Methods§
fn len(&self) -> usize
Sourcefn set_len(&mut self, new_bar_len: usize)
fn set_len(&mut self, new_bar_len: usize)
Do not shorten the length before reprinting (“\r”) since the line will be overwritten, not cleared.
[========>-] becomes [====>]==>-] instead of [====>] .
fn progress(&self) -> Self::Progress
fn start(&self) -> Self::Progress
fn end(&self) -> Self::Progress
fn has_progressed_significantly(&self) -> bool
fn remember_significant_progress(&mut self)
Provided Methods§
Sourcefn add<P>(&mut self, delta: P)
fn add<P>(&mut self, delta: P)
Adds the given progress to the current progress
Examples found in repository?
examples/simple.rs (line 58)
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}More examples
examples/loops.rs (line 101)
80fn bernoulli() {
81 let min_value = -50;
82 let max_value = 120;
83
84 println!(
85 "The bar is running from {} to {} counting successes (if value is even) and attempts ({}).",
86 min_value,
87 max_value,
88 (max_value - min_value) + 1
89 );
90 println!("Note that the bar expects less successes than provided .");
91
92 // create bar
93 let mut progress_bar = BernoulliBar::with_goal(60).timed();
94 // you can reset the length of it
95 progress_bar.set_len(60);
96
97 // do the job and show progress
98 for value in min_value..(max_value + 1) {
99 // job is successful if value is even
100 let is_successful = value % 2 == 0;
101 progress_bar.add(is_successful);
102 if progress_bar.has_progressed_significantly() {
103 progress_bar.remember_significant_progress();
104 println!("{}", progress_bar);
105 }
106
107 // sleep for visual effects ;)
108 thread::sleep(time::Duration::from_millis(SLEEP_MS));
109 }
110 // add new line to finished progress-bar
111 println!("{}", progress_bar);
112}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.