3 releases (breaking)

0.3.0 Feb 25, 2024
0.2.0 Feb 19, 2024
0.1.0 Feb 15, 2024

#787 in Rust patterns

Download history 79/week @ 2024-02-09 183/week @ 2024-02-16 187/week @ 2024-02-23 32/week @ 2024-03-01 24/week @ 2024-03-08 30/week @ 2024-03-15 28/week @ 2024-03-29

62 downloads per month

MPL-2.0 license

60KB
1K SLoC

sitrep

Crates.io Crates.io Crates.io docs.rs

Frontend-agnostic progress reporting.


Usage

use std::{sync::{mpsc, Arc}, thread, time::Duration};

use sitrep::{
    Event, MessageEvent, Progress, DetachmentEvent, Reporter, StdMpscObserver, Task, UpdateEvent,
};

fn main() {
    let (sender, receiver) = mpsc::channel();
    let observer = Arc::new(StdMpscObserver::from(sender));

    let (progress, reporter) = Progress::new(Task::default(), observer);

    // The sending end of the progress report:
    let worker_handle = thread::spawn(move || {
        progress.set_label("Crunching numbers ...".to_owned());

        let total = 100;
        progress.set_total(total);

        for completed in 1..=total {
            thread::sleep(Duration::from_millis(100));

            progress.set_completed(completed);
        }
    });

    // The receiving end of the progress report:
    let reporter_handle = thread::spawn(move || {
        while let Ok(event) = receiver.recv() {
            let Event::Update(UpdateEvent { id }) = event else {
                continue;
            };

            // The reporter is only available as long as
            // the corresponding progress is alive, too:
            let Some(reporter) = reporter.upgrade() else {
                break;
            };

            println!("{:#?}", reporter.report());
        }
    });

    worker_handle.join().unwrap();
    reporter_handle.join().unwrap();
}

See the examples directory for more examples.

Documentation

Please refer to the documentation on docs.rs.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct,
and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MPL-2.0 – see the LICENSE.md file for details.

Dependencies

~1.4–8.5MB
~43K SLoC