56 lines
1.8 KiB
QML
56 lines
1.8 KiB
QML
// CpuWidget.qml - "X.XGHz | Y%"
|
|
import QtQuick
|
|
import Quickshell.Io
|
|
import ".."
|
|
|
|
Pill {
|
|
id: root
|
|
property real freqGhz: 0
|
|
property int usagePct: 0
|
|
|
|
property int prevIdle: 0
|
|
property int prevTotal: 0
|
|
|
|
onClicked: (m) => {
|
|
if (m.button === Qt.LeftButton) Exec.run(["kitty", "-e", "btop"])
|
|
}
|
|
|
|
Text {
|
|
text: root.freqGhz.toFixed(1) + "GHz | " + root.usagePct + "%"
|
|
font.family: Theme.fontSans
|
|
font.pixelSize: Theme.fontSize
|
|
color: Theme.foreground
|
|
}
|
|
|
|
// /proc/stat - first line is total CPU
|
|
Process {
|
|
id: statProc
|
|
running: false
|
|
command: ["bash", "-c", "head -1 /proc/stat && cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq 2>/dev/null"]
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
const lines = this.text.split("\n")
|
|
// line 0: cpu user nice system idle iowait irq softirq steal
|
|
const nums = lines[0].replace(/^cpu\s+/, "").split(/\s+/).map(Number)
|
|
const idle = (nums[3] || 0) + (nums[4] || 0)
|
|
const total = nums.reduce((s, v) => s + v, 0)
|
|
const dIdle = idle - root.prevIdle
|
|
const dTotal = total - root.prevTotal
|
|
if (dTotal > 0) root.usagePct = Math.round((1 - dIdle / dTotal) * 100)
|
|
root.prevIdle = idle
|
|
root.prevTotal = total
|
|
// line 1: current frequency in kHz
|
|
const khz = parseInt(lines[1] || "0")
|
|
if (!isNaN(khz) && khz > 0) root.freqGhz = khz / 1e6
|
|
}
|
|
}
|
|
onExited: statProc.running = false
|
|
}
|
|
|
|
Timer {
|
|
interval: 1500; running: true; repeat: true
|
|
triggeredOnStart: true
|
|
onTriggered: statProc.running = true
|
|
}
|
|
}
|