From b1e418a633ca914ced078b3d4b3774a52401a80f Mon Sep 17 00:00:00 2001 From: AB Date: Sat, 11 Apr 2020 16:35:55 +0300 Subject: [PATCH] Cleanup cpu_load_bar func. Using lib. --- src/main.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index ef28d04..993d54a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ extern crate dbus; use crate::dbus::blocking::stdintf::org_freedesktop_dbus::Properties; use chrono::{DateTime, Local, Utc}; use dbus::{arg, blocking::Connection}; -use std::{env, fs, time::Duration}; +use std::{env, time::Duration}; use sys_info; const LOW: &str = "#[fg=colour119]"; @@ -24,10 +24,6 @@ struct TrackInfo { status: String, } -fn read_file(file_path: &str) -> String { - fs::read_to_string(file_path).expect("Cant read file.") -} - fn to_bar(value: i32, max: i32, low: f32, mid: f32) { let mut bar = "".to_string(); let bar_sym = "▮".to_string(); @@ -63,14 +59,17 @@ fn mem_load_bar(bar_len: i32) { } fn cpu_load_bar(bar_len: i32) { - let load = read_file("/proc/loadavg"); - let load_data = load.split_whitespace().collect::>(); - let _cpu_count = read_file("/proc/cpuinfo"); - let cpu_count = _cpu_count.matches("model name").count(); - let one: f32 = load_data[0].parse().unwrap(); - let len: f32 = one as f32 / cpu_count as f32 * bar_len as f32; + let cpu_count = match sys_info::cpu_num() { + Ok(c) => c, + Err(e) => panic!("{:?}", e), + }; + let la_one: f32 = match sys_info::loadavg() { + Ok(l) => l.one as f32, + Err(e) => panic!("{:?}", e), + }; + let len: f32 = la_one as f32 / cpu_count as f32 * bar_len as f32; to_bar(len as i32, bar_len, 0.3, 0.7); - print!("{:.2} LA1#[default]", one); + print!("{:.2} LA1#[default]", la_one); } fn get_player() -> Result, Box> {