Skip to main content

rfs_runner/
worker_progress.rs

1use std::fmt::Display;
2use std::ops::Deref;
3use std::time::Duration;
4
5use indicatif::{MultiProgress, ProgressBar, ProgressFinish};
6
7use crate::templates::WorkerTemplate;
8use crate::{line_err, line_ok, Uid};
9
10pub struct MainProgress<S> {
11  style: S,
12  progress: ProgressBar,
13  multi_progress: MultiProgress,
14}
15
16impl<S> MainProgress<S>
17where
18  S: WorkerTemplate,
19{
20  pub fn new(length: u64, multi_progress: MultiProgress, style: S) -> Self {
21    let progress = ProgressBar::new(length)
22      .with_finish(ProgressFinish::AndLeave)
23      .with_style(style.as_progress_style());
24
25    progress.enable_steady_tick(Duration::from_millis(250));
26    multi_progress.insert(0, progress.clone());
27
28    Self {
29      style,
30      progress,
31      multi_progress,
32    }
33  }
34
35  pub fn add_worker(&self, id: Uid, worker: impl Display) {
36    self.style.add_worker(id, worker.to_string());
37    self.progress.set_style(self.style.as_progress_style());
38  }
39
40  pub fn remove_worker(&self, id: &Uid) {
41    self.style.remove_worker(id);
42    self.progress.set_style(self.style.as_progress_style());
43  }
44
45  pub fn edit_worker(&self, id: Uid, worker: impl Display) {
46    self.style.edit_worker(&id, worker.to_string());
47    self.progress.set_style(self.style.as_progress_style());
48  }
49
50  pub fn println(&self, line: impl AsRef<str>) {
51    _ = self.multi_progress.println(line_ok(line.as_ref()));
52  }
53
54  pub fn eprintln(&self, error: impl AsRef<str>) {
55    _ = self.multi_progress.println(line_err(error.as_ref()));
56  }
57
58  pub fn increment(&self, count: usize) {
59    self.style.increment(count);
60  }
61}
62
63impl<S: Clone> Clone for MainProgress<S> {
64  fn clone(&self) -> Self {
65    Self {
66      style: self.style.clone(),
67      progress: self.progress.clone(),
68      multi_progress: self.multi_progress.clone(),
69    }
70  }
71}
72
73impl<S> Deref for MainProgress<S> {
74  type Target = ProgressBar;
75
76  fn deref(&self) -> &Self::Target {
77    &self.progress
78  }
79}