Progress Bars

ProgressBar is cloneable and all mutation methods take &self, so a bar can be shared between threads or moved into worker closures.

Construction

#![allow(unused)]
fn main() {
use loaders::{DrawTarget, ProgressBar, Theme};

let plain = ProgressBar::new(100);
let hidden = ProgressBar::hidden();
let targeted = ProgressBar::with_draw_target(100, DrawTarget::stderr());
let custom = ProgressBar::with_style(100, Theme::Unicode.style());
let templated = ProgressBar::with_template(100, "{prefix} [{bar:20}] {percent}%")?;

let built = ProgressBar::builder()
    .length(100)
    .template("{prefix} [{bar:20}] {remaining} left")?
    .message("loading")
    .prefix("job")
    .draw_delta(2)
    .draw_rate(30)
    .build();
Ok::<(), loaders::bar::template::TemplateError>(())
}

Mutating Progress

Use inc or inc_by for relative progress and set_position for absolute progress. set_length can be called later when a total becomes known. set_length_unknown clears the total and makes the bar indeterminate again.

#![allow(unused)]
fn main() {
let pb = loaders::ProgressBar::new(10);
pb.set_prefix("compile");
pb.set_message("parsing");
pb.inc(3);
pb.set_position(7);
pb.set_postfix("fast path");
pb.set_length_unknown();
}

Width Control

Use {bar:N} for a fixed bar width and {wide_bar} for terminal-adaptive width. {wide_bar} recalculates on each draw and fills the remaining width in the full rendered template.

#![allow(unused)]
fn main() {
let fixed = loaders::ProgressBar::with_template(100, "[{bar:20}] {percent}%")?;
let adaptive = loaders::ProgressBar::with_template(100, "[{wide_bar}] {percent}%")?;
Ok::<(), loaders::bar::template::TemplateError>(())
}

Reading Progress State

position, length, remaining, fraction, and percent make progress state visible for summaries and application logic.

#![allow(unused)]
fn main() {
let pb = loaders::ProgressBar::hidden();
pb.set_length(10);
pb.inc(4);
assert_eq!(pb.remaining(), Some(6));
assert_eq!(pb.fraction(), 0.4);
assert_eq!(pb.percent(), 40.0);
}

Draw Delta and Draw Rate

draw_delta skips redraws until enough position changes have accumulated. It is useful for hot loops. draw_rate limits redraws per second and is useful when progress changes very frequently. Set draw_rate(0) for unlimited redraws.

Interactive terminals redraw in place on the same line. Non-interactive outputs (pipes, files, CI logs, custom non-TTY writers) fall back to newline snapshots.

Steady Tick

enable_steady_tick spawns a background thread that calls tick() until the bar finishes or disable_steady_tick() is called.

#![allow(unused)]
fn main() {
use std::time::Duration;
let pb = loaders::ProgressBar::new_spinner();
pb.enable_steady_tick(Duration::from_millis(80));
pb.set_message("waiting");
pb.disable_steady_tick();
pb.finish();
}

Finishing

finish completes the bar. finish_with_message sets a final message first. finish_with_symbol sets a fixed marker prefix and final message. finish_and_clear removes the line. abandon leaves the bar unfinished, which is useful for cancelled work and can be inspected with is_abandoned.

Reset and Reuse

reset clears position, completion state, spinner frame index, and timing. It keeps the same style, length, target, prefix, and message so repeated phases can reuse one handle.