add cpu usage to i3status

This commit is contained in:
LordMZTE 2021-08-12 16:16:08 +02:00
parent dc8de7e3a8
commit cfd2190260
5 changed files with 59 additions and 18 deletions

View file

@ -6,10 +6,13 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
heim = "0.0.11"
serde_json = "1.0.66" serde_json = "1.0.66"
chrono = "0.4.19" chrono = "0.4.19"
[dependencies.heim]
version = "0.1.0-rc"
features = ["cpu", "memory"]
[dependencies.serde] [dependencies.serde]
version = "1.0.127" version = "1.0.127"
features = ["derive"] features = ["derive"]

12
i3status/rustfmt.toml Normal file
View file

@ -0,0 +1,12 @@
unstable_features = true
binop_separator = "Back"
format_code_in_doc_comments = true
format_macro_matchers = true
format_strings = true
imports_layout = "HorizontalVertical"
match_block_trailing_comma = true
merge_imports = true
normalize_comments = true
use_field_init_shorthand = true
use_try_shorthand = true
wrap_comments = true

View file

@ -8,4 +8,3 @@ pub const PURPLE: &str = "#bd93f9";
pub const RED: &str = "#ff5555"; pub const RED: &str = "#ff5555";
pub const WHITE: &str = "#f8f8f2"; pub const WHITE: &str = "#f8f8f2";
pub const YELLOW: &str = "#f1fa8c"; pub const YELLOW: &str = "#f1fa8c";

View file

@ -24,6 +24,10 @@ async fn main() {
full_text: format!("龍 {:>7}", &bar.cpu_freq), full_text: format!("龍 {:>7}", &bar.cpu_freq),
color: colors::CYAN, color: colors::CYAN,
}, },
Block {
full_text: format!(" {:>6}", &bar.cpu_usage),
color: colors::RED,
},
Block { Block {
full_text: format!(" {}", &bar.ram), full_text: format!(" {}", &bar.ram),
color: colors::PURPLE, color: colors::PURPLE,
@ -51,15 +55,16 @@ async fn spawn_workers(bar: Arc<RwLock<Bar>>) {
tokio::spawn(workers::ram(Arc::clone(&bar))); tokio::spawn(workers::ram(Arc::clone(&bar)));
tokio::spawn(workers::time(Arc::clone(&bar))); tokio::spawn(workers::time(Arc::clone(&bar)));
tokio::spawn(workers::pulseaudio_vol(Arc::clone(&bar))); tokio::spawn(workers::pulseaudio_vol(Arc::clone(&bar)));
tokio::spawn(workers::cpu_freq(Arc::clone(&bar))); tokio::spawn(workers::cpu(Arc::clone(&bar)));
tokio::spawn(workers::battery(Arc::clone(&bar))); tokio::spawn(workers::battery(Arc::clone(&bar)));
} }
#[derive(Default)] #[derive(Default)]
struct Bar { struct Bar {
battery: String,
cpu_freq: String,
cpu_usage: String,
ram: String, ram: String,
time: String, time: String,
vol: String, vol: String,
cpu_freq: String,
battery: String,
} }

View file

@ -1,12 +1,12 @@
use crate::Bar; use crate::Bar;
use chrono::Local; use chrono::Local;
use heim::units::frequency::megahertz; use heim::{
use heim::{memory::os::linux::MemoryExt, units::information::megabyte}; memory::os::linux::MemoryExt,
units::{frequency::megahertz, information::megabyte, ratio},
};
use std::sync::Arc; use std::{sync::Arc, time::Duration};
use std::time::Duration; use tokio::{process::Command, sync::RwLock};
use tokio::process::Command;
use tokio::sync::RwLock;
pub(crate) async fn ram(bar: Arc<RwLock<Bar>>) { pub(crate) async fn ram(bar: Arc<RwLock<Bar>>) {
let mut int = tokio::time::interval(Duration::from_secs(1)); let mut int = tokio::time::interval(Duration::from_secs(1));
@ -80,7 +80,7 @@ pub(crate) async fn pulseaudio_vol(bar: Arc<RwLock<Bar>>) {
let symbol = if mute { '遼' } else { '蓼' }; let symbol = if mute { '遼' } else { '蓼' };
msg = format!("{} {}%", symbol, avg); msg = format!("{} {}%", symbol, avg);
} },
None => msg = String::from("PA Error :("), None => msg = String::from("PA Error :("),
} }
@ -89,19 +89,38 @@ pub(crate) async fn pulseaudio_vol(bar: Arc<RwLock<Bar>>) {
} }
} }
pub(crate) async fn cpu_freq(bar: Arc<RwLock<Bar>>) { pub(crate) async fn cpu(bar: Arc<RwLock<Bar>>) {
let mut int = tokio::time::interval(Duration::from_secs(2)); let mut int = tokio::time::interval(Duration::from_secs(2));
loop { loop {
let freq = heim::cpu::frequency().await; let freq = heim::cpu::frequency().await;
let txt; // need to take this twice and subtract for... reasons!
let usage_1 = heim::cpu::usage().await;
tokio::time::sleep(Duration::from_millis(200)).await;
let usage_2 = heim::cpu::usage().await;
let freq_txt;
if let Ok(freq) = freq { if let Ok(freq) = freq {
let freq = freq.current().get::<megahertz>(); let freq = freq.current().get::<megahertz>();
txt = format!("{}MHz", freq); freq_txt = format!("{}MHz", freq);
} else { } else {
txt = String::from("Error reading CPU frequency :("); freq_txt = String::from("Error reading CPU frequency :(");
} }
bar.write().await.cpu_freq = txt;
let usage_txt;
if let (Ok(usage_1), Ok(usage_2)) = (usage_1, usage_2) {
let usage = (usage_2 - usage_1).get::<ratio::percent>() / 10.;
usage_txt = format!("{:.1}%", usage);
} else {
usage_txt = String::from("Error reading CPU usage :(");
}
{
let mut bar = bar.write().await;
bar.cpu_freq = freq_txt;
bar.cpu_usage = usage_txt;
}
int.tick().await; int.tick().await;
} }
} }
@ -125,7 +144,10 @@ pub(crate) async fn battery(bar: Arc<RwLock<Bar>>) {
let txt; let txt;
if let Some(s) = out { if let Some(s) = out {
if let Some(bat) = s.lines().next() { if let Some(bat) = s.lines().next() {
let percent = bat.split(&[' ', ','][..]).find(|s| s.contains('%')).unwrap_or("0%"); let percent = bat
.split(&[' ', ','][..])
.find(|s| s.contains('%'))
.unwrap_or("0%");
let state = if bat.contains("Charging") { let state = if bat.contains("Charging") {
BatteryState::Charging BatteryState::Charging
} else if bat.contains("Discharging") { } else if bat.contains("Discharging") {