about summary refs log tree commit diff stats
path: root/pkgs/by-name/ya/yambar-modules/src/cpu.rs
blob: f53107a87c7734b97197c46305ce598778c61e97 (plain) (blame)
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
// nixos-config - My current NixOS configuration
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of my nixos-config.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use std::{thread, time::Duration};

use sysinfo::{CpuExt, System, SystemExt};

pub fn cpu() {
    let mut sys = System::new();

    loop {
        sys.refresh_cpu();
        let cpu_usage: f32 = sys.cpus().iter().map(|cpu| cpu.cpu_usage()).sum();
        println!(
            "cpu|range:0-100|{:.0}",
            cpu_usage / sys.cpus().iter().count() as f32
        );
        println!();

        // Sleeping to give the system time to run for long
        // enough to have useful information.
        thread::sleep(Duration::from_secs(3));
    }
}