Watch
1
0
Fork
You've already forked Zoe-Dotfiles
0
mirror of synced 2026-11-03 22:17:19 +00:00
Zoe-Dotfiles/quickshell/modules/Cpu.qml
2026-09-10 09:39:34 +02:00

52 lines
1.2 KiB
QML

import Quickshell
import Quickshell.Io
import QtQuick
import qs
Rectangle {
id: root
property real lastCpuTotal: 0.0
property real lastCpuIdle: 0.0
property real cpuUsage: 0.0
color: Appearance.colAppBg
implicitHeight: Appearance.appletRadHeight
implicitWidth: cpuText.width + 25
radius: Appearance.appletRadHeight
Process {
id: cpuProc
command: ["sh", "-c", "head -1 /proc/stat"]
property double cpuUsage
stdout: SplitParser {
onRead: data => {
if (!data) {
return;
}
else {
var p = data.trim().split(/\s+/);
var idle = parseInt(p[4]) + parseInt(p[5]);
var total = p.slice(1, 8).reduce((a, b) => a + parseInt(b), 0);
root.cpuUsage = Math.round(100 * (1 - (idle - root.lastCpuIdle) / (total - root.lastCpuTotal)));
root.lastCpuTotal = total;
root.lastCpuIdle = idle;
}
}
}
Component.onCompleted: running = true
}
Timer {
id: cpuProcRestarter
interval: 2000
repeat: true
running: true
onTriggered: cpuProc.running = true
}
Text {
font.family: Appearance.font
font.pixelSize: Appearance.fontSize
font.bold: true
color: Appearance.colFg
id: cpuText
anchors.centerIn: parent
text: "CPU: " + root.cpuUsage + "%"
}
}