rcli_loader/
loading_element.rs

1use std::{sync::Arc, usize};
2
3pub struct LoadingElement {
4    max: usize,
5    progress: usize,
6    name: Arc<Box<str>>, // TODO: make sure that Arc is the proper use for single write multiple read
7    formatter: Option< fn(usize) -> Box<str> >,
8}
9impl LoadingElement {
10    // New functions
11    pub fn new(max: usize, name: Box<str>, formatter: Option< fn(usize) -> Box<str> >) -> LoadingElement {
12        LoadingElement { max: (max), progress: (0), name: (Arc::new(name)), formatter: (formatter) }
13    }
14    // pub fn empty() -> LoadingElement {
15    //     LoadingElement { max: (0), progress: (0), name: (Rc::new(Box::from(""))) }
16    // }
17
18    // Getters
19    pub fn get_progress(&self) -> usize {
20        return self.progress;
21    }
22    pub fn get_progress_decimal(&self) -> f64 {
23        return self.progress as f64 / self.max as f64;
24    }
25    pub fn get_max(&self) -> usize {
26        return self.max;
27    }
28    pub fn get_name(&self) -> Arc<Box<str>> {
29        return self.name.clone();
30    }
31    pub fn format_progress_unit(&self, value: usize) -> Box<str> {
32        match self.formatter {
33            None => Box::from(value.to_string()),
34            Some (format_function) => { format_function(value) },
35        }
36    }
37
38    // Setters
39    pub fn set_max(&mut self, max: usize) {
40        self.max = max;
41    }
42    pub fn update(&mut self, addition: usize) {
43        self.progress += addition;
44    }
45}