From 6803ffd741a087d55b7ded3c771fb973ede21c93 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 12 Sep 2024 17:19:03 -0700 Subject: [PATCH] feat(bar): configurable network activity fill char len --- Cargo.lock | 37 +++++++++++++++++++++++++++++++++++++ komorebi-bar/Cargo.toml | 6 +++--- komorebi-bar/src/network.rs | 24 ++++++++++++++++-------- schema.bar.json | 12 ++++++++++++ 4 files changed, 68 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 049d1fff..48c8e4da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2690,6 +2690,9 @@ dependencies = [ "image", "komorebi-client", "netdev", + "num", + "num-derive", + "num-traits", "schemars", "serde", "serde_json_lenient", @@ -3368,6 +3371,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + [[package]] name = "num-bigint" version = "0.4.6" @@ -3378,6 +3395,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -3404,6 +3430,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + [[package]] name = "num-rational" version = "0.4.2" diff --git a/komorebi-bar/Cargo.toml b/komorebi-bar/Cargo.toml index 9ffb1974..66a95008 100644 --- a/komorebi-bar/Cargo.toml +++ b/komorebi-bar/Cargo.toml @@ -20,6 +20,9 @@ font-loader = "0.11" hotwatch = "0.5" image = "0.25" netdev = "0.30" +num = "0.4.3" +num-derive = "0.4.2" +num-traits = "0.2.19" schemars = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } @@ -31,6 +34,3 @@ tracing-appender = "0.2" tracing-subscriber = { version = "0.3", features = ["env-filter"] } windows = { workspace = true } windows-icons = "0.1" -num = "0.4.3" -num-derive = "0.4.2" -num-traits = "0.2.19" diff --git a/komorebi-bar/src/network.rs b/komorebi-bar/src/network.rs index d8ac6691..0bd0b692 100644 --- a/komorebi-bar/src/network.rs +++ b/komorebi-bar/src/network.rs @@ -4,15 +4,15 @@ use eframe::egui::Context; use eframe::egui::Label; use eframe::egui::Sense; use eframe::egui::Ui; +use num_derive::FromPrimitive; use schemars::JsonSchema; use serde::Deserialize; use serde::Serialize; +use std::fmt; use std::process::Command; use std::time::Duration; use std::time::Instant; use sysinfo::Networks; -use std::fmt; -use num_derive::FromPrimitive; #[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)] pub struct NetworkConfig { @@ -22,6 +22,8 @@ pub struct NetworkConfig { pub show_total_data_transmitted: bool, /// Show network activity pub show_network_activity: bool, + /// Characters to reserve for network activity data + pub network_activity_fill_characters: Option, /// Data refresh interval (default: 10 seconds) pub data_refresh_interval: Option, } @@ -60,11 +62,12 @@ impl From for Network { for (interface_name, data) in &networks_network_activity { if friendly_name.eq(interface_name) { last_state_transmitted.push(format!( - "{} {: >10}/s {} {: >10}/s", + "{} {: >width$}/s {} {: >width$}/s", egui_phosphor::regular::ARROW_FAT_DOWN, to_pretty_bytes(data.received(), 1), egui_phosphor::regular::ARROW_FAT_UP, to_pretty_bytes(data.transmitted(), 1), + width = value.network_activity_fill_characters.unwrap_or_default(), )) } } @@ -80,6 +83,9 @@ impl From for Network { data_refresh_interval: value.data_refresh_interval.unwrap_or(10), show_total_data_transmitted: value.show_total_data_transmitted, show_network_activity: value.show_network_activity, + network_activity_fill_characters: value + .network_activity_fill_characters + .unwrap_or_default(), last_state_total_data_transmitted: last_state_data, last_state_network_activity: last_state_transmitted, last_updated_total_data_transmitted: Instant::now(), @@ -100,6 +106,7 @@ pub struct Network { last_state_network_activity: Vec, last_updated_total_data_transmitted: Instant, last_updated_network_activity: Instant, + network_activity_fill_characters: usize, } impl Network { @@ -128,11 +135,12 @@ impl Network { for (interface_name, data) in &self.networks_network_activity { if friendly_name.eq(interface_name) { outputs.push(format!( - "{} {: >10}/s {} {: >10}/s", + "{} {: >width$}/s {} {: >width$}/s", egui_phosphor::regular::ARROW_FAT_DOWN, to_pretty_bytes(data.received(), self.data_refresh_interval), egui_phosphor::regular::ARROW_FAT_UP, to_pretty_bytes(data.transmitted(), self.data_refresh_interval), + width = self.network_activity_fill_characters, )) } } @@ -250,7 +258,7 @@ impl fmt::Display for DataUnit { fn to_pretty_bytes(input_in_bytes: u64, timespan_in_s: u64) -> String { let input = input_in_bytes as f32 / timespan_in_s as f32; - let mut magnitude = input.log(1024 as f32) as u32; + let mut magnitude = input.log(1024f32) as u32; // let the base unit be KiB if magnitude < 1 { @@ -258,11 +266,11 @@ fn to_pretty_bytes(input_in_bytes: u64, timespan_in_s: u64) -> String { } let base: Option = num::FromPrimitive::from_u32(magnitude); - let result = input as f32 / ((1 as u64) << (magnitude * 10)) as f32; - + let result = input / ((1u64) << (magnitude * 10)) as f32; + match base { Some(DataUnit::B) => format!("{result:.1} B"), Some(unit) => format!("{result:.1} {unit}iB"), - None => format!("Unknown data unit"), + None => String::from("Unknown data unit"), } } diff --git a/schema.bar.json b/schema.bar.json index 101ee5cf..eb4e2ab8 100644 --- a/schema.bar.json +++ b/schema.bar.json @@ -281,6 +281,12 @@ "description": "Enable the Network widget", "type": "boolean" }, + "network_activity_fill_characters": { + "description": "Characters to reserve for network activity data", + "type": "integer", + "format": "uint", + "minimum": 0.0 + }, "show_network_activity": { "description": "Show network activity", "type": "boolean" @@ -665,6 +671,12 @@ "description": "Enable the Network widget", "type": "boolean" }, + "network_activity_fill_characters": { + "description": "Characters to reserve for network activity data", + "type": "integer", + "format": "uint", + "minimum": 0.0 + }, "show_network_activity": { "description": "Show network activity", "type": "boolean"