feat: Netzwerk-Traffic anzeigen
Summiert rx/tx aus /proc/net/dev über alle Interfaces außer lo, zeigt die Rate alle 1s. Feste Breite (5 Zeichen, rechtsbündig) und durchgehend KiB/s als Einheit, damit die Bar bei schwankendem Traffic nicht wackelt. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+56
-1
@@ -13,6 +13,7 @@ enum Update {
|
|||||||
Clock(String),
|
Clock(String),
|
||||||
Volume(String),
|
Volume(String),
|
||||||
Temp(String),
|
Temp(String),
|
||||||
|
Net(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query_volume() -> String {
|
fn query_volume() -> String {
|
||||||
@@ -80,6 +81,41 @@ fn query_cpu_temp() -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sums rx/tx byte counters across all interfaces except loopback.
|
||||||
|
fn read_net_bytes() -> (u64, u64) {
|
||||||
|
let mut down = 0u64;
|
||||||
|
let mut up = 0u64;
|
||||||
|
|
||||||
|
let content = match fs::read_to_string("/proc/net/dev") {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(_) => return (down, up),
|
||||||
|
};
|
||||||
|
|
||||||
|
// First two lines are the header; each remaining line is "iface: f0 f1 ...".
|
||||||
|
for line in content.lines().skip(2) {
|
||||||
|
let Some((iface, rest)) = line.split_once(':') else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if iface.trim() == "lo" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let fields: Vec<&str> = rest.split_whitespace().collect();
|
||||||
|
// Layout per /proc/net/dev: field 0 = rx bytes, field 8 = tx bytes.
|
||||||
|
if fields.len() >= 9 {
|
||||||
|
down += fields[0].parse::<u64>().unwrap_or(0);
|
||||||
|
up += fields[8].parse::<u64>().unwrap_or(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(down, up)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fixed-width, fixed-unit (KiB/s) formatting so the bar doesn't change size
|
||||||
|
// as traffic fluctuates.
|
||||||
|
fn format_net(down_rate: u64, up_rate: u64) -> String {
|
||||||
|
format!("net:↓{down_rate:>5} ↑{up_rate:>5} KiB/s")
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (tx, rx) = mpsc::channel::<Update>();
|
let (tx, rx) = mpsc::channel::<Update>();
|
||||||
|
|
||||||
@@ -113,11 +149,29 @@ fn main() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let tx = tx.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
const INTERVAL: Duration = Duration::from_secs(1);
|
||||||
|
let (mut prev_down, mut prev_up) = read_net_bytes();
|
||||||
|
loop {
|
||||||
|
std::thread::sleep(INTERVAL);
|
||||||
|
let (down, up) = read_net_bytes();
|
||||||
|
let down_rate = down.saturating_sub(prev_down) / INTERVAL.as_secs() / 1024;
|
||||||
|
let up_rate = up.saturating_sub(prev_up) / INTERVAL.as_secs() / 1024;
|
||||||
|
tx.send(Update::Net(format_net(down_rate, up_rate))).unwrap();
|
||||||
|
prev_down = down;
|
||||||
|
prev_up = up;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
drop(tx);
|
drop(tx);
|
||||||
|
|
||||||
let mut clock = String::new();
|
let mut clock = String::new();
|
||||||
let mut volume = String::new();
|
let mut volume = String::new();
|
||||||
let mut temp = String::new();
|
let mut temp = String::new();
|
||||||
|
let mut net = String::new();
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut handle = stdout.lock();
|
let mut handle = stdout.lock();
|
||||||
|
|
||||||
@@ -126,8 +180,9 @@ fn main() {
|
|||||||
Update::Clock(s) => clock = s,
|
Update::Clock(s) => clock = s,
|
||||||
Update::Volume(s) => volume = s,
|
Update::Volume(s) => volume = s,
|
||||||
Update::Temp(s) => temp = s,
|
Update::Temp(s) => temp = s,
|
||||||
|
Update::Net(s) => net = s,
|
||||||
}
|
}
|
||||||
writeln!(handle, "{volume} | {temp} | {clock}").expect("failed to write to stdout");
|
writeln!(handle, "{volume} | {net} | {temp} | {clock}").expect("failed to write to stdout");
|
||||||
handle.flush().expect("failed to flush stdout");
|
handle.flush().expect("failed to flush stdout");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user