diff --git a/CHANGELOG.md b/CHANGELOG.md index bc89a741..de2c30ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `eww shell-completions` command, generating completion scripts for different shells +### Fixes +- Fixed wrong values in `EWW_NET` + ## [0.4.0] (04.09.2022) ### BREAKING CHANGES diff --git a/crates/eww/Cargo.toml b/crates/eww/Cargo.toml index 18e07d98..ff1f04e7 100644 --- a/crates/eww/Cargo.toml +++ b/crates/eww/Cargo.toml @@ -56,7 +56,7 @@ regex.workspace = true serde_json.workspace = true serde = {workspace = true, features = ["derive"]} simple-signal.workspace = true -sysinfo.workspace = true +sysinfo = { workspace = true, features = ["linux-netdevs"] } tokio-util.workspace = true tokio = { workspace = true, features = ["full"] } unescape.workspace = true diff --git a/crates/eww/src/config/system_stats.rs b/crates/eww/src/config/system_stats.rs index 18b5d29e..995d7ada 100644 --- a/crates/eww/src/config/system_stats.rs +++ b/crates/eww/src/config/system_stats.rs @@ -1,40 +1,54 @@ use crate::util::IterAverage; use anyhow::{Context, Result}; -use itertools::Itertools; use once_cell::sync::Lazy; use std::{fs::read_to_string, sync::Mutex}; 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> = Lazy::new(|| Mutex::new(System::new())); -static DISKS: Lazy> = Lazy::new(|| Mutex::new(sysinfo::Disks::new())); -static COMPONENTS: Lazy> = Lazy::new(|| Mutex::new(sysinfo::Components::new())); -static NETWORKS: Lazy> = Lazy::new(|| Mutex::new(sysinfo::Networks::new())); +static DISKS: Lazy> = Lazy::new(|| Mutex::new(sysinfo::Disks::new_with_refreshed_list())); +static COMPONENTS: Lazy> = Lazy::new(|| Mutex::new(sysinfo::Components::new_with_refreshed_list())); +static NETWORKS: Lazy> = + Lazy::new(|| Mutex::new((RefreshTime::new(), sysinfo::Networks::new_with_refreshed_list()))); pub fn get_disks() -> String { let mut disks = DISKS.lock().unwrap(); disks.refresh_list(); disks.refresh(); - format!( - "{{ {} }}", - disks - .iter() - .map(|c| { - let total_space = c.total_space(); - let available_space = c.available_space(); - let used_space = total_space - available_space; - format!( - r#""{}": {{"name": {:?}, "total": {}, "free": {}, "used": {}, "used_perc": {}}}"#, - c.mount_point().display(), - c.name(), - total_space, - available_space, - used_space, - (used_space as f32 / total_space as f32) * 100f32, - ) - }) - .join(",") - ) + disks + .iter() + .map(|c| { + let total_space = c.total_space(); + let available_space = c.available_space(); + let used_space = total_space - available_space; + + ( + c.mount_point().display().to_string(), + serde_json::json!({ + "name": c.name(), + "total": total_space, + "free": available_space, + "used": used_space, + "used_perc": (used_space as f32 / total_space as f32) * 100f32 + }), + ) + }) + .collect::() + .to_string() } pub fn get_ram() -> String { @@ -44,42 +58,41 @@ pub fn get_ram() -> String { let total_memory = system.total_memory(); let available_memory = system.available_memory(); let used_memory = total_memory as f32 - available_memory as f32; - format!( - r#"{{"total_mem": {}, "free_mem": {}, "total_swap": {}, "free_swap": {}, "available_mem": {}, "used_mem": {}, "used_mem_perc": {}}}"#, - total_memory, - system.free_memory(), - system.total_swap(), - system.free_swap(), - available_memory, - used_memory, - (used_memory / total_memory as f32) * 100f32, - ) + serde_json::json!({ + "total_mem": total_memory, + "free_mem": system.free_memory(), + "total_swap": system.total_swap(), + "free_swap": system.free_swap(), + "available_mem": available_memory, + "used_mem": used_memory, + "used_mem_perc": (used_memory / total_memory as f32) * 100f32, + }) + .to_string() } pub fn get_temperatures() -> String { let mut components = COMPONENTS.lock().unwrap(); components.refresh_list(); components.refresh(); - format!( - "{{ {} }}", - components - .iter() - .map(|c| format!( - r#""{}": {}"#, + components + .iter() + .map(|c| { + ( c.label().to_uppercase().replace(' ', "_"), // It is common for temperatures to report a non-numeric value. // Tolerate it by serializing it as the string "null" - c.temperature().to_string().replace("NaN", "\"null\"") - )) - .join(",") - ) + c.temperature().to_string().replace("NaN", "\"null\""), + ) + }) + .collect::() + .to_string() } pub fn get_cpus() -> String { let mut system = SYSTEM.lock().unwrap(); system.refresh_cpu_specifics(sysinfo::CpuRefreshKind::everything()); let cpus = system.cpus(); - let json = serde_json::json!({ + serde_json::json!({ "cores": cpus.iter() .map(|a| { serde_json::json!({ @@ -89,8 +102,8 @@ pub fn get_cpus() -> String { }) }).collect::>(), "avg": cpus.iter().map(|a| a.cpu_usage()).avg() - }); - serde_json::to_string(&json).unwrap() + }) + .to_string() } #[cfg(target_os = "macos")] @@ -196,17 +209,21 @@ pub fn get_battery_capacity() -> Result { } 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(); - let interfaces = format!( - "{{ {} }}", - &networks - .iter() - .map(|a| format!(r#""{}": {{ "NET_UP": {}, "NET_DOWN": {} }}"#, a.0, a.1.transmitted(), a.1.received())) - .join(","), - ); - interfaces + let elapsed = last_refresh.next_refresh(); + + networks + .iter() + .map(|(name, data)| { + let transmitted = data.transmitted() as f64 / elapsed.as_secs_f64(); + let received = data.received() as f64 / elapsed.as_secs_f64(); + (name, serde_json::json!({ "NET_UP": transmitted, "NET_DOWN": received })) + }) + .collect::() + .to_string() } pub fn get_time() -> String { diff --git a/crates/eww/src/widgets/build_widget.rs b/crates/eww/src/widgets/build_widget.rs index 2ba7c26c..c3a18990 100644 --- a/crates/eww/src/widgets/build_widget.rs +++ b/crates/eww/src/widgets/build_widget.rs @@ -3,7 +3,7 @@ use codespan_reporting::diagnostic::Severity; use eww_shared_util::{AttrName, Spanned}; use gdk::prelude::Cast; use gtk::{ - prelude::{BoxExt, ContainerExt, WidgetExt, WidgetExtManual}, + prelude::{BoxExt, ContainerExt, WidgetExt}, Orientation, }; use itertools::Itertools;