pub struct ProgressBar { /* private fields */ }Expand description
A convenient progress bar.
See the module documentation for example code and more documentation.
Implementations§
Source§impl ProgressBar

impl ProgressBar
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new progress bar.
use headway::ProgressBar;
let p = ProgressBar::new().with_message("Calibrating flux capacitors");
for _ in p.wrap(0..100) {
sleep(Duration::from_millis(20));
}Creates a new progress bar which will never be rendered.
This can be useful if you need to pass a progress bar to some function, but you don’t actually want a bar to show up.
Sourcepub fn split_weighted(self) -> ProgressBarWeightedNester
pub fn split_weighted(self) -> ProgressBarWeightedNester
Splits the bar into children of given proportions.
This is useful if you have many tasks, but you only want to show a single progress bar. Then you can split the bar so that for example the first task gets 40% of the bar and the second task gets 60% of the bar.
The display will automatically change to percentages if this mode of splitting is used.
use headway::{ProgressBar, ProgressBarIterable};
let mut p = ProgressBar::new().split_weighted();
let first_half = p.take(0.4).with_message("First part");
let second_half = p.take(0.6).with_message("Second part");
for _ in (0..50).progress_with(first_half) {
sleep(Duration::from_millis(20));
}
for _ in (0..50).progress_with(second_half) {
sleep(Duration::from_millis(30));
}Sourcepub fn split_sized(self) -> ProgressBarSizedNester
pub fn split_sized(self) -> ProgressBarSizedNester
Splits the bar into children of fixed sizes.
Each child bar will represent N items of the parent. Note that the children can have any length, but a filled child bar will be remapped to N items in the parent.
use headway::ProgressBar;
let mut p = ProgressBar::new().split_sized();
// Create the bars up front so that the bar knows how many items
// there are in total.
let first = p.take(5).with_message("First");
let second = p.take(20).with_message("Second");
for _ in first.wrap(0..5) {
sleep(Duration::from_millis(300));
}
// Here we only loop over 5 items, but we make the child bar represent
// 20 items in the parent bar.
for _ in second.wrap(0..5) {
sleep(Duration::from_millis(300));
}Sourcepub fn split_summed(self) -> ProgressBarSummedNester
pub fn split_summed(self) -> ProgressBarSummedNester
Splits the bar into children and displays the summed progress of all children.
use headway::ProgressBar;
let p = ProgressBar::new().split_summed();
let tasks = (0..4)
.map(|_| {
let child_bar = p.take();
std::thread::spawn(move || {
for _ in child_bar.wrap(0..100) {
sleep(Duration::from_millis(20));
}
})
})
.collect::<Vec<_>>();
for task in tasks {
task.join().unwrap()
}Sourcepub fn split_each<It: Iterator>(
self,
it: It,
) -> impl Iterator<Item = (ProgressBar, It::Item)>
pub fn split_each<It: Iterator>( self, it: It, ) -> impl Iterator<Item = (ProgressBar, It::Item)>
Splits the bar into N bars, each representing an item in the iterator.
This is useful if each item takes a long time and you want progress for it. It is also useful if you are sending each item to separate threads to process them independently.
use headway::ProgressBar;
let p = ProgressBar::new();
// Split the progress bar into 10 nested bars
for (nested_bar, i) in p.split_each(0..10) {
// Wrap the nested bar around an iterator representing this subtask
nested_bar.set_message(format!("Subtask {}", i));
for _ in nested_bar.wrap(0..200) {
sleep(Duration::from_millis(5));
}
}Sourcepub fn set_length(&self, len: usize)
pub fn set_length(&self, len: usize)
Sets the length of this progress bar.
This has no effect if the bar has already been finished or abandoned.
Sourcepub fn set_position(&self, pos: usize)
pub fn set_position(&self, pos: usize)
Sets the amount of progress this bar has made.
Should usually be between 0 and Self::length.
This has no effect if the bar has already been finished or abandoned.
Sourcepub fn clear_message(&self)
pub fn clear_message(&self)
Clears any message set using Self::set_message or Self::with_message.
Sourcepub fn with_message(self, message: impl Into<String>) -> Self
pub fn with_message(self, message: impl Into<String>) -> Self
Equivalent to Self::set_message, but may be more ergonomic in some situations since it returns self.
Sourcepub fn with_length(self, length: usize) -> Self
pub fn with_length(self, length: usize) -> Self
Equivalent to Self::set_length, but may be more ergonomic in some situations since it returns self.
Sourcepub fn set_message(&self, message: impl Into<String>)
pub fn set_message(&self, message: impl Into<String>)
Sets a message which will show up next to the bar.
If the root bar has been split into multiple children, then the message that is displayed is from the first bar that is not finished. Or if all bars are finished then the last bar with a message will be used.
Sourcepub fn inc(&self)
pub fn inc(&self)
Increments the progress of this bar by 1.
Usually it’s more convenient to work with the iterator-wrapping functions like Self::wrap
Sourcepub fn finish_with_message(&mut self, message: impl Into<String>)
pub fn finish_with_message(&mut self, message: impl Into<String>)
Marks the bar as finished and sets the message.
Equivalent to first setting the message and then marking the bar as finished.
Sourcepub fn abandon(&mut self)
pub fn abandon(&mut self)
Abandons the progress bar.
The remaining part of the progress bar will be colored red to indicate it will never be completed. Progress bars are automatically marked as abandoned when they are dropped and they are only partially complete.
Sourcepub fn finish(&mut self)
pub fn finish(&mut self)
Marks the bar as finished.
If the bar has a length, the position of the bar will be set to Self::length.
Sourcepub fn wrap<It: Iterator>(self, it: It) -> ProgressBarIterator<It> ⓘ
pub fn wrap<It: Iterator>(self, it: It) -> ProgressBarIterator<It> ⓘ
Wraps the bar around an iterator.
If the iterator has a known length, the bar’s length will be set to that length. The iterator will headway the progress by 1 each step. When reaching the end of the iterator, the bar will be marked as finished.
See also ProgressBarIterable::progress and ProgressBarIterable::progress_with
use headway::ProgressBar;
let p = ProgressBar::new().with_message("Calibrating flux capacitors");
for _ in p.wrap(0..100) {
sleep(Duration::from_millis(20));
}