nixos/quickshell/NetMonitor.qml
2026-04-15 10:38:34 -03:00

53 lines
1.4 KiB
QML

pragma Singleton
import Quickshell
import Quickshell.Io
import QtQuick
Singleton {
id: root
property real rxSpeed: 0
property real txSpeed: 0
property var _prevRx: null
property var _prevTx: null
function _fmt(bytes) {
if (bytes >= 1048576) return (bytes / 1048576).toFixed(1) + " M"
if (bytes >= 1024) return (bytes / 1024).toFixed(0) + " K"
return bytes + " B"
}
readonly property string rxStr: _fmt(rxSpeed)
readonly property string txStr: _fmt(txSpeed)
Timer {
interval: 1000
running: true
repeat: true
triggeredOnStart: true
onTriggered: {
_proc.running = false
_proc.running = true
}
}
Process {
id: _proc
command: ["bash", "-c", "iface=$(ip route show default | awk '{print $5; exit}'); awk -v i=\"$iface:\" '$1==i {print $2, $10}' /proc/net/dev"]
stdout: SplitParser {
onRead: line => {
const parts = line.trim().split(/\s+/)
if (parts.length < 2) return
const rx = parseInt(parts[0])
const tx = parseInt(parts[1])
if (root._prevRx !== null) {
root.rxSpeed = Math.max(0, rx - root._prevRx)
root.txSpeed = Math.max(0, tx - root._prevTx)
}
root._prevRx = rx
root._prevTx = tx
}
}
}
}