demo/
demo.rs

1use std::time::Duration;
2
3use cli_status_board::{SBStateConfig, SBState, Status};
4
5fn main() {
6    let state = SBState::new(SBStateConfig {
7        silent: false,
8        ..Default::default()
9    });
10
11    // The handles for these tasks are immediately dropped, so we automatically remove them.
12    // Except for error/info, which we display for a little while before removing automatically.
13    {
14        {
15            let task_id = state.add_task("finished task", Status::Finished);
16            state.add_subtask(&task_id, Status::Started);
17        }
18        let _ = state.add_task("error task", Status::Error);
19        let _ = state.add_task("started task", Status::Started);
20        let _ = state.add_task("queued task", Status::Queued);
21    }
22
23    let mut tasks = (0..10)
24        .into_iter()
25        .map(|index| {
26            let state = state.clone();
27            std::thread::spawn(move || {
28                std::thread::sleep(Duration::from_secs(index));
29                let task_id = state.add_task(format!("Task {index}"), Status::Started);
30                std::thread::sleep(Duration::from_secs(index.min(3)));
31                let sub_tasks = (0..10)
32                    .into_iter()
33                    .map(|_| state.add_subtask(&task_id, Status::Started))
34                    .collect::<Vec<_>>();
35
36                for sub_task_id in sub_tasks {
37                    std::thread::sleep(Duration::from_secs((index + 1).min(3)));
38                    state.update_subtask(&task_id, &sub_task_id, Status::Finished);
39                }
40
41                state.update_task(&task_id, Status::Finished);
42            })
43        })
44        .collect::<Vec<_>>();
45
46    tasks.push(std::thread::spawn({
47        let state = state.clone();
48        move || {
49            let task_id = state.add_task(format!("Task with no children"), Status::Started);
50            std::thread::sleep(Duration::from_secs(10));
51            state.update_task(&task_id, Status::Finished);
52        }
53    }));
54
55    tasks.push(std::thread::spawn(move || {
56        std::thread::sleep(Duration::from_secs(4));
57        state.info("Here's an informational message");
58        std::thread::sleep(Duration::from_secs(8));
59        state.info("Here's another one :)");
60    }));
61
62    for task in tasks {
63        task.join().unwrap();
64    }
65}