Module kdam::monitor

source ·
Expand description

Monitor mode for progress bars.

In monitor mode progress bar is refreshed in specific intervals. Default monitor modes may not fit in many cases. So, it is recommended to create a custom monitor mode. The basic idea behind monitor mode is to create a separate thread for updating progress bar which can be achieved by the following code.

use kdam::{Bar, BarExt};
use std::{
    sync::{Arc, Mutex},
    thread,
};

fn custom_monitor(pb: Bar, maxinterval: f32) -> (Arc<Mutex<Bar>>, thread::JoinHandle<()>) {
    let pb_arc = Arc::new(Mutex::new(pb));
    let pb_arc_clone = pb_arc.clone();

    let handle = thread::spawn(move || loop {
        thread::sleep(std::time::Duration::from_secs_f32(maxinterval));
        let mut pb_monitor = pb_arc_clone.lock().unwrap();

        if pb_monitor.completed() {
            break;
        }

        let _ = pb_monitor.refresh();
    });

    (pb_arc, handle)
}

Functions