mirror of
https://mzte.de/git/LordMZTE/dotfiles.git
synced 2024-12-14 07:43:49 +01:00
add cpu usage to i3status
This commit is contained in:
parent
dc8de7e3a8
commit
cfd2190260
5 changed files with 59 additions and 18 deletions
|
@ -6,10 +6,13 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
heim = "0.0.11"
|
||||
serde_json = "1.0.66"
|
||||
chrono = "0.4.19"
|
||||
|
||||
[dependencies.heim]
|
||||
version = "0.1.0-rc"
|
||||
features = ["cpu", "memory"]
|
||||
|
||||
[dependencies.serde]
|
||||
version = "1.0.127"
|
||||
features = ["derive"]
|
||||
|
|
12
i3status/rustfmt.toml
Normal file
12
i3status/rustfmt.toml
Normal 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
|
|
@ -8,4 +8,3 @@ pub const PURPLE: &str = "#bd93f9";
|
|||
pub const RED: &str = "#ff5555";
|
||||
pub const WHITE: &str = "#f8f8f2";
|
||||
pub const YELLOW: &str = "#f1fa8c";
|
||||
|
||||
|
|
|
@ -24,6 +24,10 @@ async fn main() {
|
|||
full_text: format!("龍 {:>7}", &bar.cpu_freq),
|
||||
color: colors::CYAN,
|
||||
},
|
||||
Block {
|
||||
full_text: format!(" {:>6}", &bar.cpu_usage),
|
||||
color: colors::RED,
|
||||
},
|
||||
Block {
|
||||
full_text: format!(" {}", &bar.ram),
|
||||
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::time(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)));
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Bar {
|
||||
battery: String,
|
||||
cpu_freq: String,
|
||||
cpu_usage: String,
|
||||
ram: String,
|
||||
time: String,
|
||||
vol: String,
|
||||
cpu_freq: String,
|
||||
battery: String,
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use crate::Bar;
|
||||
use chrono::Local;
|
||||
use heim::units::frequency::megahertz;
|
||||
use heim::{memory::os::linux::MemoryExt, units::information::megabyte};
|
||||
use heim::{
|
||||
memory::os::linux::MemoryExt,
|
||||
units::{frequency::megahertz, information::megabyte, ratio},
|
||||
};
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::process::Command;
|
||||
use tokio::sync::RwLock;
|
||||
use std::{sync::Arc, time::Duration};
|
||||
use tokio::{process::Command, sync::RwLock};
|
||||
|
||||
pub(crate) async fn ram(bar: Arc<RwLock<Bar>>) {
|
||||
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 { '蓼' };
|
||||
|
||||
msg = format!("{} {}%", symbol, avg);
|
||||
}
|
||||
},
|
||||
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));
|
||||
|
||||
loop {
|
||||
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 {
|
||||
let freq = freq.current().get::<megahertz>();
|
||||
txt = format!("{}MHz", freq);
|
||||
freq_txt = format!("{}MHz", freq);
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
@ -125,7 +144,10 @@ pub(crate) async fn battery(bar: Arc<RwLock<Bar>>) {
|
|||
let txt;
|
||||
if let Some(s) = out {
|
||||
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") {
|
||||
BatteryState::Charging
|
||||
} else if bat.contains("Discharging") {
|
||||
|
|
Loading…
Reference in a new issue