Skip to content

Commit

Permalink
to_pretty_bytes on network activity
Browse files Browse the repository at this point in the history
  • Loading branch information
CtByte authored and LGUG2Z committed Sep 13, 2024
1 parent 42b7a13 commit ac38f52
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
CHANGELOG.md
dummy.go
komorebic/applications.yaml
/.vs
3 changes: 3 additions & 0 deletions komorebi-bar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ 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"
84 changes: 60 additions & 24 deletions komorebi-bar/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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 {
Expand Down Expand Up @@ -43,11 +45,11 @@ impl From<NetworkConfig> for Network {
for (interface_name, data) in &networks_total_data_transmitted {
if friendly_name.eq(interface_name) {
last_state_data.push(format!(
"{} {:.0} MB / {} {:.0} MB",
egui_phosphor::regular::ARROW_CIRCLE_DOWN,
(data.total_received() as f32) / 1024.0 / 1024.0,
egui_phosphor::regular::ARROW_CIRCLE_UP,
(data.total_transmitted() as f32) / 1024.0 / 1024.0,
"{} {} / {} {}",
egui_phosphor::regular::ARROW_FAT_DOWN,
to_pretty_bytes(data.total_received(), 1),
egui_phosphor::regular::ARROW_FAT_UP,
to_pretty_bytes(data.total_transmitted(), 1),
))
}
}
Expand All @@ -58,11 +60,11 @@ impl From<NetworkConfig> for Network {
for (interface_name, data) in &networks_network_activity {
if friendly_name.eq(interface_name) {
last_state_transmitted.push(format!(
"{} {:.1} KB/s / {} {:.1} KB/s",
egui_phosphor::regular::ARROW_CIRCLE_DOWN,
(data.received() as f32) / 1024.0 / 1.0,
egui_phosphor::regular::ARROW_CIRCLE_UP,
(data.transmitted() as f32) / 1024.0 / 1.0,
"{} {: >10}/s {} {: >10}/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),
))
}
}
Expand Down Expand Up @@ -126,15 +128,11 @@ impl Network {
for (interface_name, data) in &self.networks_network_activity {
if friendly_name.eq(interface_name) {
outputs.push(format!(
"{} {:.1} KB/s / {} {:.1} KB/s",
egui_phosphor::regular::ARROW_CIRCLE_DOWN,
(data.received() as f32)
/ 1024.0
/ self.data_refresh_interval as f32,
egui_phosphor::regular::ARROW_CIRCLE_UP,
(data.transmitted() as f32)
/ 1024.0
/ self.data_refresh_interval as f32,
"{} {: >10}/s {} {: >10}/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),
))
}
}
Expand Down Expand Up @@ -167,11 +165,11 @@ impl Network {
for (interface_name, data) in &self.networks_total_data_transmitted {
if friendly_name.eq(interface_name) {
outputs.push(format!(
"{} {:.0} MB / {} {:.0} MB",
egui_phosphor::regular::ARROW_CIRCLE_DOWN,
(data.total_received() as f32) / 1024.0 / 1024.0,
egui_phosphor::regular::ARROW_CIRCLE_UP,
(data.total_transmitted() as f32) / 1024.0 / 1024.0,
"{} {} / {} {}",
egui_phosphor::regular::ARROW_FAT_DOWN,
to_pretty_bytes(data.total_received(), 1),
egui_phosphor::regular::ARROW_FAT_UP,
to_pretty_bytes(data.total_transmitted(), 1),
))
}
}
Expand Down Expand Up @@ -230,3 +228,41 @@ impl BarWidget for Network {
}
}
}

#[derive(Debug, FromPrimitive)]
enum DataUnit {
B = 0,
K = 1,
M = 2,
G = 3,
T = 4,
P = 5,
E = 6,
Z = 7,
Y = 8,
}

impl fmt::Display for DataUnit {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

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 the base unit be KiB
if magnitude < 1 {
magnitude = 1;
}

let base: Option<DataUnit> = num::FromPrimitive::from_u32(magnitude);
let result = input as f32 / ((1 as u64) << (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"),
}
}

0 comments on commit ac38f52

Please sign in to comment.