Progress Tracking
How Percent Works
percent() returns the read progress as a floating-point number from 0.0 to 100.0:
percent = position × 100 / max(file_size, 1)
positionreflects only finished items. The item you are currently processing is not counted- An empty file always reports
0.0because the position stays at 0 andmax(file_size, 1)is 1 percent()reaches100.0only when the iterator is fully consumed
Several factors can prevent
percent()from reaching100.0: anendoffset before the file end, alimit,drop_partial, or breaking out of the loop early
Progress Bar with Indicatif (Rust)
use indicatif::{ProgressBar, ProgressStyle};
use preader::PReader;
let reader = PReader::default();
let mut lines = reader.lines("data.txt").build()?;
let bar = ProgressBar::new(100);
bar.set_style(
ProgressStyle::with_template("{percent:>3}%|{bar:20}| {msg}")
.unwrap()
.progress_chars("█▉▊▋▌▍▎▏ "),
);
while let Some(line) = lines.next() {
let _line = line?;
let pct = lines.percent();
bar.set_position(pct.round() as u64);
bar.set_message(format!("{pct:.1}%"));
}
bar.abandon();
Progress Bar with Tqdm (Python)
from tqdm import tqdm
from preader import PReader
reader = PReader()
lines = reader.lines("data.txt")
bar = tqdm(total=100, bar_format="{l_bar}{bar}| {n:.1f}%")
for line in lines:
bar.update(lines.percent() - bar.n)
bar.close()
Use
bar.abandon()in Rust instead ofbar.finish(). Callingfinish()jumps the bar to 100% regardless of the actual position