nova-shell/modules/Cpu.qml

64 lines
1.8 KiB
QML

import QtQuick
import Quickshell.Io
import "." as M
M.BarSection {
id: root
spacing: Math.max(1, M.Theme.moduleSpacing - 2)
tooltip: "CPU: " + root.usage + "%\n" + root.freqGhz.toFixed(2) + " GHz"
property int usage: 0
property real freqGhz: 0
property var _prev: null
FileView {
id: stat
path: "/proc/stat"
onLoaded: {
const line = text().split("\n")[0];
const f = line.trim().split(/\s+/).slice(1).map(Number);
const idle = f[3] + f[4];
const total = f.reduce((a, b) => a + b, 0);
if (root._prev) {
const dIdle = idle - root._prev.idle;
const dTotal = total - root._prev.total;
if (dTotal > 0)
root.usage = Math.round((1 - dIdle / dTotal) * 100);
}
root._prev = {
idle,
total
};
}
}
FileView {
id: cpuinfo
path: "/proc/cpuinfo"
onLoaded: {
const lines = text().split("\n").filter(l => l.startsWith("cpu MHz"));
if (lines.length === 0)
return;
const sum = lines.reduce((a, l) => a + parseFloat(l.split(":")[1]), 0);
root.freqGhz = sum / lines.length / 1000;
}
}
Timer {
interval: M.Modules.cpu.interval || 1000
running: true
repeat: true
onTriggered: {
stat.reload();
cpuinfo.reload();
}
}
M.BarIcon {
icon: "\uF2DB"
anchors.verticalCenter: parent.verticalCenter
}
M.BarLabel {
label: root.usage.toString().padStart(2) + "%@" + root.freqGhz.toFixed(2)
minText: "99%@9.99"
anchors.verticalCenter: parent.verticalCenter
}
}