Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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)
  • position reflects only finished items. The item you are currently processing is not counted
  • An empty file always reports 0.0 because the position stays at 0 and max(file_size, 1) is 1
  • percent() reaches 100.0 only when the iterator is fully consumed

Several factors can prevent percent() from reaching 100.0: an end offset before the file end, a limit, 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 of bar.finish(). Calling finish() jumps the bar to 100% regardless of the actual position