Fix wrong values in EWW_NET (fixes #958)

This commit is contained in:
elkowar 2024-02-17 17:34:58 +01:00
parent 3f478b6503
commit 2656e238ca
No known key found for this signature in database
GPG key ID: 862BA3D7D7760F13
4 changed files with 78 additions and 58 deletions

View file

@ -32,6 +32,9 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add `tooltip` widget, which allows setting a custom tooltip (not only text), to a widget (By: Rayzeq) - Add `tooltip` widget, which allows setting a custom tooltip (not only text), to a widget (By: Rayzeq)
- Add `eww shell-completions` command, generating completion scripts for different shells - Add `eww shell-completions` command, generating completion scripts for different shells
### Fixes
- Fixed wrong values in `EWW_NET`
## [0.4.0] (04.09.2022) ## [0.4.0] (04.09.2022)
### BREAKING CHANGES ### BREAKING CHANGES

View file

@ -56,7 +56,7 @@ regex.workspace = true
serde_json.workspace = true serde_json.workspace = true
serde = {workspace = true, features = ["derive"]} serde = {workspace = true, features = ["derive"]}
simple-signal.workspace = true simple-signal.workspace = true
sysinfo.workspace = true sysinfo = { workspace = true, features = ["linux-netdevs"] }
tokio-util.workspace = true tokio-util.workspace = true
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
unescape.workspace = true unescape.workspace = true

View file

@ -1,40 +1,54 @@
use crate::util::IterAverage; use crate::util::IterAverage;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use itertools::Itertools;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::{fs::read_to_string, sync::Mutex}; use std::{fs::read_to_string, sync::Mutex};
use sysinfo::System; use sysinfo::System;
struct RefreshTime(std::time::Instant);
impl RefreshTime {
pub fn new() -> Self {
Self(std::time::Instant::now())
}
pub fn next_refresh(&mut self) -> std::time::Duration {
let now = std::time::Instant::now();
let duration = now.duration_since(self.0);
self.0 = now;
duration
}
}
static SYSTEM: Lazy<Mutex<System>> = Lazy::new(|| Mutex::new(System::new())); static SYSTEM: Lazy<Mutex<System>> = Lazy::new(|| Mutex::new(System::new()));
static DISKS: Lazy<Mutex<sysinfo::Disks>> = Lazy::new(|| Mutex::new(sysinfo::Disks::new())); static DISKS: Lazy<Mutex<sysinfo::Disks>> = Lazy::new(|| Mutex::new(sysinfo::Disks::new_with_refreshed_list()));
static COMPONENTS: Lazy<Mutex<sysinfo::Components>> = Lazy::new(|| Mutex::new(sysinfo::Components::new())); static COMPONENTS: Lazy<Mutex<sysinfo::Components>> = Lazy::new(|| Mutex::new(sysinfo::Components::new_with_refreshed_list()));
static NETWORKS: Lazy<Mutex<sysinfo::Networks>> = Lazy::new(|| Mutex::new(sysinfo::Networks::new())); static NETWORKS: Lazy<Mutex<(RefreshTime, sysinfo::Networks)>> =
Lazy::new(|| Mutex::new((RefreshTime::new(), sysinfo::Networks::new_with_refreshed_list())));
pub fn get_disks() -> String { pub fn get_disks() -> String {
let mut disks = DISKS.lock().unwrap(); let mut disks = DISKS.lock().unwrap();
disks.refresh_list(); disks.refresh_list();
disks.refresh(); disks.refresh();
format!(
"{{ {} }}",
disks disks
.iter() .iter()
.map(|c| { .map(|c| {
let total_space = c.total_space(); let total_space = c.total_space();
let available_space = c.available_space(); let available_space = c.available_space();
let used_space = total_space - available_space; let used_space = total_space - available_space;
format!(
r#""{}": {{"name": {:?}, "total": {}, "free": {}, "used": {}, "used_perc": {}}}"#, (
c.mount_point().display(), c.mount_point().display().to_string(),
c.name(), serde_json::json!({
total_space, "name": c.name(),
available_space, "total": total_space,
used_space, "free": available_space,
(used_space as f32 / total_space as f32) * 100f32, "used": used_space,
"used_perc": (used_space as f32 / total_space as f32) * 100f32
}),
) )
}) })
.join(",") .collect::<serde_json::Value>()
) .to_string()
} }
pub fn get_ram() -> String { pub fn get_ram() -> String {
@ -44,42 +58,41 @@ pub fn get_ram() -> String {
let total_memory = system.total_memory(); let total_memory = system.total_memory();
let available_memory = system.available_memory(); let available_memory = system.available_memory();
let used_memory = total_memory as f32 - available_memory as f32; let used_memory = total_memory as f32 - available_memory as f32;
format!( serde_json::json!({
r#"{{"total_mem": {}, "free_mem": {}, "total_swap": {}, "free_swap": {}, "available_mem": {}, "used_mem": {}, "used_mem_perc": {}}}"#, "total_mem": total_memory,
total_memory, "free_mem": system.free_memory(),
system.free_memory(), "total_swap": system.total_swap(),
system.total_swap(), "free_swap": system.free_swap(),
system.free_swap(), "available_mem": available_memory,
available_memory, "used_mem": used_memory,
used_memory, "used_mem_perc": (used_memory / total_memory as f32) * 100f32,
(used_memory / total_memory as f32) * 100f32, })
) .to_string()
} }
pub fn get_temperatures() -> String { pub fn get_temperatures() -> String {
let mut components = COMPONENTS.lock().unwrap(); let mut components = COMPONENTS.lock().unwrap();
components.refresh_list(); components.refresh_list();
components.refresh(); components.refresh();
format!(
"{{ {} }}",
components components
.iter() .iter()
.map(|c| format!( .map(|c| {
r#""{}": {}"#, (
c.label().to_uppercase().replace(' ', "_"), c.label().to_uppercase().replace(' ', "_"),
// It is common for temperatures to report a non-numeric value. // It is common for temperatures to report a non-numeric value.
// Tolerate it by serializing it as the string "null" // Tolerate it by serializing it as the string "null"
c.temperature().to_string().replace("NaN", "\"null\"") c.temperature().to_string().replace("NaN", "\"null\""),
))
.join(",")
) )
})
.collect::<serde_json::Value>()
.to_string()
} }
pub fn get_cpus() -> String { pub fn get_cpus() -> String {
let mut system = SYSTEM.lock().unwrap(); let mut system = SYSTEM.lock().unwrap();
system.refresh_cpu_specifics(sysinfo::CpuRefreshKind::everything()); system.refresh_cpu_specifics(sysinfo::CpuRefreshKind::everything());
let cpus = system.cpus(); let cpus = system.cpus();
let json = serde_json::json!({ serde_json::json!({
"cores": cpus.iter() "cores": cpus.iter()
.map(|a| { .map(|a| {
serde_json::json!({ serde_json::json!({
@ -89,8 +102,8 @@ pub fn get_cpus() -> String {
}) })
}).collect::<Vec<_>>(), }).collect::<Vec<_>>(),
"avg": cpus.iter().map(|a| a.cpu_usage()).avg() "avg": cpus.iter().map(|a| a.cpu_usage()).avg()
}); })
serde_json::to_string(&json).unwrap() .to_string()
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@ -196,17 +209,21 @@ pub fn get_battery_capacity() -> Result<String> {
} }
pub fn net() -> String { pub fn net() -> String {
let mut networks = NETWORKS.lock().unwrap(); let (ref mut last_refresh, ref mut networks) = &mut *NETWORKS.lock().unwrap();
networks.refresh_list(); networks.refresh_list();
networks.refresh(); networks.refresh();
let interfaces = format!( let elapsed = last_refresh.next_refresh();
"{{ {} }}",
&networks networks
.iter() .iter()
.map(|a| format!(r#""{}": {{ "NET_UP": {}, "NET_DOWN": {} }}"#, a.0, a.1.transmitted(), a.1.received())) .map(|(name, data)| {
.join(","), let transmitted = data.transmitted() as f64 / elapsed.as_secs_f64();
); let received = data.received() as f64 / elapsed.as_secs_f64();
interfaces (name, serde_json::json!({ "NET_UP": transmitted, "NET_DOWN": received }))
})
.collect::<serde_json::Value>()
.to_string()
} }
pub fn get_time() -> String { pub fn get_time() -> String {

View file

@ -3,7 +3,7 @@ use codespan_reporting::diagnostic::Severity;
use eww_shared_util::{AttrName, Spanned}; use eww_shared_util::{AttrName, Spanned};
use gdk::prelude::Cast; use gdk::prelude::Cast;
use gtk::{ use gtk::{
prelude::{BoxExt, ContainerExt, WidgetExt, WidgetExtManual}, prelude::{BoxExt, ContainerExt, WidgetExt},
Orientation, Orientation,
}; };
use itertools::Itertools; use itertools::Itertools;