1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::Update;
use sysinfo::{ProcessorExt, System, SystemExt};
use std::fmt;
/**# Cpu Monitor
## Example
```no_run
#[macro_use]
extern crate redblocks;

use redblocks::{Widget, plugins::CpuPlugin};

fn main() {
    let bar = vec![
        Widget::new_mili(CpuPlugin::new(), 750),
    ];

    start_bar!(bar);
}
```
*/
pub struct CpuPlugin {
    pub used: System,
}

impl CpuPlugin {
    pub fn new() -> Box<Self> {
        Box::new(Self {
            used: System::new(),
        })
    }
}

impl fmt::Display for CpuPlugin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}%",
            (self.used.get_global_processor_info().get_cpu_usage() as f64 * 100_f64).round()
                / 100_f64
        )
    }
}

impl Update for CpuPlugin {
    fn refresh(&mut self) {
        self.used.refresh_cpu();
    }
}