nova-shell/modules/Cpu.qml

66 lines
1.7 KiB
QML

import QtQuick
import Quickshell.Io
import "." as M
Row {
id: root
spacing: 2
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: 1000
running: true
repeat: true
onTriggered: {
stat.reload();
cpuinfo.reload();
}
}
M.BarIcon {
icon: "\uF2DB"
anchors.verticalCenter: parent.verticalCenter
}
Text {
text: root.usage.toString().padStart(2) + "%@" + root.freqGhz.toFixed(2)
color: M.Theme.base05
font.pixelSize: M.Theme.fontSize
font.family: M.Theme.fontFamily
verticalAlignment: Text.AlignVCenter
}
}