Files
linux-dotfiles/quickshell/services/SystemStats.qml

76 lines
2.0 KiB
QML

import Quickshell
import Quickshell.Io
import QtQuick
Item {
id: root
// =====================
// System stats
// =====================
property int cpuUsage: 0
property int memUsage: 0
property var lastCpuIdle: 0
property var lastCpuTotal: 0
Process {
id: cpuProc
// get cpu usage, first line of /proc/stat
command: ["cat", "/proc/stat"]
stdout: StdioCollector {
onStreamFinished: {
var line = this.text.split(/\r?\n/)[1].split(" ");
var user = parseInt(line[1]);
var nice = parseInt(line[2]);
var system = parseInt(line[3]);
var idle = parseInt(line[4]);
var iowait = parseInt(line[5]);
var irq = parseInt(line[6]);
var softirq = parseInt(line[7]);
var idleAll = idle + iowait;
var total = user + nice + system + idle + iowait + irq + softirq;
var diffIdle = idleAll - root.lastCpuIdle;
var diffTotal = total - root.lastCpuTotal;
if (diffTotal > 0) {
root.cpuUsage = Math.round(100 * (1 - diffIdle / diffTotal));
}
root.lastCpuIdle = idleAll;
root.lastCpuTotal = total;
}
}
}
Process {
id: memProc
command: ["sh", "-c", "free | grep Mem"]
stdout: SplitParser {
onRead: data => {
if (!data)
return;
var parts = data.trim().split(/\s+/);
var total = parseInt(parts[1]) || 1;
var used = parseInt(parts[2]) || 0;
memUsage = Math.round(100 * used / total);
}
}
Component.onCompleted: running = true
}
// Update loop
Timer {
interval: 2000
running: true
repeat: true
onTriggered: {
cpuProc.running = true;
memProc.running = true;
}
}
}