Struct indicatif::ProgressBar
source · pub struct ProgressBar { /* private fields */ }
Expand description
A progress bar or spinner
The progress bar is an Arc
around its internal state. When the progress bar is cloned it
just increments the refcount (so the original and its clone share the same state).
Implementations§
source§impl ProgressBar
impl ProgressBar
sourcepub fn new(len: u64) -> Self
pub fn new(len: u64) -> Self
Creates a new progress bar with a given length
This progress bar by default draws directly to stderr, and refreshes a maximum of 15 times a second. To change the refresh rate, set the draw target to one with a different refresh rate.
Creates a completely hidden progress bar
This progress bar still responds to API changes but it does not have a length or render in any way.
sourcepub fn with_draw_target(
len: Option<u64>,
draw_target: ProgressDrawTarget
) -> Self
pub fn with_draw_target( len: Option<u64>, draw_target: ProgressDrawTarget ) -> Self
Creates a new progress bar with a given length and draw target
sourcepub fn style(&self) -> ProgressStyle
pub fn style(&self) -> ProgressStyle
Get a clone of the current progress bar style.
sourcepub fn with_style(self, style: ProgressStyle) -> Self
pub fn with_style(self, style: ProgressStyle) -> Self
A convenience builder-like function for a progress bar with a given style
sourcepub fn with_tab_width(self, tab_width: usize) -> Self
pub fn with_tab_width(self, tab_width: usize) -> Self
A convenience builder-like function for a progress bar with a given tab width
sourcepub fn with_prefix(self, prefix: impl Into<Cow<'static, str>>) -> Self
pub fn with_prefix(self, prefix: impl Into<Cow<'static, str>>) -> Self
A convenience builder-like function for a progress bar with a given prefix
For the prefix to be visible, the {prefix}
placeholder must be present in the template
(see ProgressStyle
).
sourcepub fn with_message(self, message: impl Into<Cow<'static, str>>) -> Self
pub fn with_message(self, message: impl Into<Cow<'static, str>>) -> Self
A convenience builder-like function for a progress bar with a given message
For the message to be visible, the {msg}
placeholder must be present in the template (see
ProgressStyle
).
sourcepub fn with_position(self, pos: u64) -> Self
pub fn with_position(self, pos: u64) -> Self
A convenience builder-like function for a progress bar with a given position
sourcepub fn with_elapsed(self, elapsed: Duration) -> Self
pub fn with_elapsed(self, elapsed: Duration) -> Self
A convenience builder-like function for a progress bar with a given elapsed time
sourcepub fn with_finish(self, finish: ProgressFinish) -> Self
pub fn with_finish(self, finish: ProgressFinish) -> Self
Sets the finish behavior for the progress bar
This behavior is invoked when ProgressBar
or
ProgressBarIter
completes and
ProgressBar::is_finished()
is false.
If you don’t want the progress bar to be automatically finished then
call with_finish(Abandon)
.
sourcepub fn new_spinner() -> Self
pub fn new_spinner() -> Self
Creates a new spinner
This spinner by default draws directly to stderr. This adds the default spinner style to it.
sourcepub fn set_style(&self, style: ProgressStyle)
pub fn set_style(&self, style: ProgressStyle)
Overrides the stored style
This does not redraw the bar. Call ProgressBar::tick()
to force it.
sourcepub fn set_tab_width(&mut self, tab_width: usize)
pub fn set_tab_width(&mut self, tab_width: usize)
Sets the tab width (default: 8). All tabs will be expanded to this many spaces.
sourcepub fn enable_steady_tick(&self, interval: Duration)
pub fn enable_steady_tick(&self, interval: Duration)
Spawns a background thread to tick the progress bar
When this is enabled a background thread will regularly tick the progress bar in the given interval. This is useful to advance progress bars that are very slow by themselves.
When steady ticks are enabled, calling ProgressBar::tick()
on a progress bar does not
have any effect.
sourcepub fn disable_steady_tick(&self)
pub fn disable_steady_tick(&self)
sourcepub fn tick(&self)
pub fn tick(&self)
Manually ticks the spinner or progress bar
This automatically happens on any other change to a progress bar.
A quick convenience check if the progress bar is hidden
sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Indicates that the progress bar finished
sourcepub fn println<I: AsRef<str>>(&self, msg: I)
pub fn println<I: AsRef<str>>(&self, msg: I)
Print a log line above the progress bar
If the progress bar is hidden (e.g. when standard output is not a terminal), println()
will not do anything. If you want to write to the standard output in such cases as well, use
ProgressBar::suspend()
instead.
If the progress bar was added to a MultiProgress
, the log line will be
printed above all other progress bars.
sourcepub fn update(&self, f: impl FnOnce(&mut ProgressState))
pub fn update(&self, f: impl FnOnce(&mut ProgressState))
Update the ProgressBar
’s inner ProgressState
sourcepub fn set_position(&self, pos: u64)
pub fn set_position(&self, pos: u64)
Sets the position of the progress bar
sourcepub fn set_length(&self, len: u64)
pub fn set_length(&self, len: u64)
Sets the length of the progress bar
sourcepub fn inc_length(&self, delta: u64)
pub fn inc_length(&self, delta: u64)
Increase the length of the progress bar
sourcepub fn set_prefix(&self, prefix: impl Into<Cow<'static, str>>)
pub fn set_prefix(&self, prefix: impl Into<Cow<'static, str>>)
Sets the current prefix of the progress bar
For the prefix to be visible, the {prefix}
placeholder must be present in the template
(see ProgressStyle
).
sourcepub fn set_message(&self, msg: impl Into<Cow<'static, str>>)
pub fn set_message(&self, msg: impl Into<Cow<'static, str>>)
Sets the current message of the progress bar
For the message to be visible, the {msg}
placeholder must be present in the template (see
ProgressStyle
).
sourcepub fn downgrade(&self) -> WeakProgressBar
pub fn downgrade(&self) -> WeakProgressBar
Creates a new weak reference to this ProgressBar
sourcepub fn reset_eta(&self)
pub fn reset_eta(&self)
Resets the ETA calculation
This can be useful if the progress bars made a large jump or was paused for a prolonged time.
sourcepub fn reset_elapsed(&self)
pub fn reset_elapsed(&self)
Resets elapsed time and the ETA calculation
sourcepub fn finish_with_message(&self, msg: impl Into<Cow<'static, str>>)
pub fn finish_with_message(&self, msg: impl Into<Cow<'static, str>>)
Finishes the progress bar and sets a message
For the message to be visible, the {msg}
placeholder must be present in the template (see
ProgressStyle
).
sourcepub fn finish_and_clear(&self)
pub fn finish_and_clear(&self)
Finishes the progress bar and completely clears it
sourcepub fn abandon_with_message(&self, msg: impl Into<Cow<'static, str>>)
pub fn abandon_with_message(&self, msg: impl Into<Cow<'static, str>>)
Finishes the progress bar and sets a message, and leaves the current progress
For the message to be visible, the {msg}
placeholder must be present in the template (see
ProgressStyle
).
sourcepub fn finish_using_style(&self)
pub fn finish_using_style(&self)
Finishes the progress bar using the behavior stored in the ProgressStyle
sourcepub fn set_draw_target(&self, target: ProgressDrawTarget)
pub fn set_draw_target(&self, target: ProgressDrawTarget)
Sets a different draw target for the progress bar
This can be used to draw the progress bar to stderr (this is the default):
let pb = ProgressBar::new(100);
pb.set_draw_target(ProgressDrawTarget::stderr());
Note: Calling this method on a ProgressBar
linked with a MultiProgress
(after
running MultiProgress::add()
) will unlink this progress bar. If you don’t want this
behavior, call MultiProgress::set_draw_target()
instead.
Use ProgressBar::with_draw_target()
to set the draw target during creation.
sourcepub fn suspend<F: FnOnce() -> R, R>(&self, f: F) -> R
pub fn suspend<F: FnOnce() -> R, R>(&self, f: F) -> R
Hide the progress bar temporarily, execute f
, then redraw the progress bar
Useful for external code that writes to the standard output.
If the progress bar was added to a MultiProgress
, it will suspend the entire MultiProgress
.
Note: The internal lock is held while f
is executed. Other threads trying to print
anything on the progress bar will be blocked until f
finishes.
Therefore, it is recommended to avoid long-running operations in f
.
let mut pb = ProgressBar::new(3);
pb.suspend(|| {
println!("Log message");
})
sourcepub fn wrap_iter<It: Iterator>(&self, it: It) -> ProgressBarIter<It> ⓘ
pub fn wrap_iter<It: Iterator>(&self, it: It) -> ProgressBarIter<It> ⓘ
Wraps an Iterator
with the progress bar
let v = vec![1, 2, 3];
let pb = ProgressBar::new(3);
for item in pb.wrap_iter(v.iter()) {
// ...
}
sourcepub fn wrap_read<R: Read>(&self, read: R) -> ProgressBarIter<R> ⓘ
pub fn wrap_read<R: Read>(&self, read: R) -> ProgressBarIter<R> ⓘ
Wraps an io::Read
with the progress bar
let source = File::open("work.txt")?;
let mut target = File::create("done.txt")?;
let pb = ProgressBar::new(source.metadata()?.len());
io::copy(&mut pb.wrap_read(source), &mut target);
sourcepub fn wrap_write<W: Write>(&self, write: W) -> ProgressBarIter<W> ⓘ
pub fn wrap_write<W: Write>(&self, write: W) -> ProgressBarIter<W> ⓘ
Wraps an io::Write
with the progress bar
let mut source = File::open("work.txt")?;
let target = File::create("done.txt")?;
let pb = ProgressBar::new(source.metadata()?.len());
io::copy(&mut source, &mut pb.wrap_write(target));
sourcepub fn wrap_async_write<W: AsyncWrite + Unpin>(
&self,
write: W
) -> ProgressBarIter<W> ⓘ
Available on crate feature tokio
only.
pub fn wrap_async_write<W: AsyncWrite + Unpin>( &self, write: W ) -> ProgressBarIter<W> ⓘ
tokio
only.Wraps an tokio::io::AsyncWrite
with the progress bar
let mut source = File::open("work.txt").await?;
let mut target = File::open("done.txt").await?;
let pb = ProgressBar::new(source.metadata().await?.len());
io::copy(&mut source, &mut pb.wrap_async_write(target)).await?;
sourcepub fn wrap_async_read<R: AsyncRead + Unpin>(
&self,
read: R
) -> ProgressBarIter<R> ⓘ
Available on crate feature tokio
only.
pub fn wrap_async_read<R: AsyncRead + Unpin>( &self, read: R ) -> ProgressBarIter<R> ⓘ
tokio
only.Wraps an tokio::io::AsyncRead
with the progress bar
let mut source = File::open("work.txt").await?;
let mut target = File::open("done.txt").await?;
let pb = ProgressBar::new(source.metadata().await?.len());
io::copy(&mut pb.wrap_async_read(source), &mut target).await?;
sourcepub fn wrap_stream<S: Stream>(&self, stream: S) -> ProgressBarIter<S> ⓘ
Available on crate feature futures
only.
pub fn wrap_stream<S: Stream>(&self, stream: S) -> ProgressBarIter<S> ⓘ
futures
only.Wraps a futures::Stream
with the progress bar
use futures::stream::{self, StreamExt};
let pb = ProgressBar::new(10);
let mut stream = pb.wrap_stream(stream::iter('a'..='z'));
assert_eq!(stream.next().await, Some('a'));
assert_eq!(stream.count().await, 25);
Trait Implementations§
source§impl Clone for ProgressBar
impl Clone for ProgressBar
source§fn clone(&self) -> ProgressBar
fn clone(&self) -> ProgressBar
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more