Something went wrong. Try again.
Something went wrong. Try again.
13 kB · 469 lines
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470pragma ComponentBehavior: Bound
import "../components"import ".."import "../components/ModuleData.js" as ModuleDataimport QtQuickimport Quickshell.Ioimport Quickshell.Widgets
ClippingRectangle { id: root
required property var barWindow property var ipc: null property bool moduleVisible: true property int interval: 3000 property int hoverInterval: 1000 property var previousStats: ({}) property real currentUsage: 0 property var cores: [] property var usageHistory: [] property int historySampleCounter: 0 property string load1: "0.00" property string load5: "0.00" property string load15: "0.00" readonly property string statCommand: "awk '/^cpu[0-9 ]/ {print}' /proc/stat" readonly property string loadCommand: "awk '{print $1, $2, $3}' /proc/loadavg" readonly property bool tooltipVisible: mouse.containsMouse && cores.length > 0
function usageClass(value) { if (value > 95) return "critical";
if (value > 90) return "warning";
return ""; }
function usageBarColor(value) { const usageState = usageClass(value); if (usageState.length > 0) return theme.alpha(theme.classColor(usageState), 0.48);
return theme.alpha(theme.text, 0.32); }
function loadColor(value) { const coreCount = Math.max(1, cores.length); const normalized = Number(value || 0) / coreCount;
if (normalized > 1) return theme.red;
if (normalized > 0.7) return theme.yellow;
return theme.green; }
function loadText(value) { return Number(value || 0).toFixed(2).padStart(5, " "); }
function coreLabel(name) { return `Core ${String(name).slice(3)}`; }
function coreNames(stats) { return Object.keys(stats).filter(name => name.match(/^cpu\d+$/)).sort((left, right) => Number(left.slice(3)) - Number(right.slice(3))); }
function shouldAppendHistory() { if (!mouse.containsMouse) return true;
historySampleCounter += 1; if (historySampleCounter < Math.max(1, Math.round(interval / hoverInterval))) return false;
historySampleCounter = 0; return true; }
function appendUsageHistory(value) { const nextHistory = usageHistory.slice(); nextHistory.push(ModuleData.clampPercent(value)); usageHistory = nextHistory.slice(Math.max(0, nextHistory.length - 48)); }
function resetSamples() { previousStats = ({}); historySampleCounter = 0; }
function statTotals(values) { const idle = Number(values[3] || 0) + Number(values[4] || 0); let total = 0; // guest and guest_nice are already included in user and nice. for (const value of values.slice(0, 8)) total += Number(value || 0);
return { "idle": idle, "total": total }; }
function parseStats(output) { const stats = {}; const lines = output.trim().split("\n");
for (const line of lines) { const parts = line.trim().split(/\s+/); if (parts.length < 5 || !parts[0].match(/^cpu/)) continue;
stats[parts[0]] = parts.slice(1).map(value => Number(value)); }
return stats; }
function usageFor(name, stats) { const previous = previousStats[name]; const current = stats[name]; if (!previous || !current) return 0;
const previousTotals = statTotals(previous); const currentTotals = statTotals(current); const totalDelta = currentTotals.total - previousTotals.total; const idleDelta = currentTotals.idle - previousTotals.idle; if (totalDelta <= 0) return 0;
return ModuleData.clampPercent((totalDelta - idleDelta) * 100 / totalDelta); }
function applyOutput(output) { const stats = parseStats(output); const hasPrevious = Object.keys(previousStats).length > 0;
if (hasPrevious) { currentUsage = usageFor("cpu", stats); if (shouldAppendHistory()) appendUsageHistory(currentUsage);
cores = coreNames(stats).map(name => ({ "name": name, "usage": usageFor(name, stats) })); }
previousStats = stats; }
function applyLoadOutput(output) { const parts = output.trim().split(/\s+/); if (parts.length < 3) return;
load1 = parts[0]; load5 = parts[1]; load15 = parts[2]; }
function refresh() { if (!moduleVisible || refreshRunner.running || loadRunner.running) return;
refreshRunner.exec(["/usr/bin/sh", "-lc", statCommand]); loadRunner.exec(["/usr/bin/sh", "-lc", loadCommand]); }
implicitWidth: compactContent.implicitWidth + 20 implicitHeight: 34 radius: 8 color: mouse.containsMouse ? theme.surface2 : theme.alpha(theme.surface0, 0.28) border.color: theme.alpha(theme.text, 0.1) border.width: 1 contentUnderBorder: true visible: moduleVisible
Theme { id: theme }
Row { id: compactContent
anchors.centerIn: parent spacing: 6
Text { anchors.verticalCenter: parent.verticalCenter width: 28 color: theme.classColor(root.usageClass(root.currentUsage)) font.family: theme.fontFamily font.pixelSize: theme.barFontPixelSize horizontalAlignment: Text.AlignRight text: `${Math.round(root.currentUsage).toString().padStart(2, " ")}%` textFormat: Text.PlainText }
Text { anchors.verticalCenter: parent.verticalCenter color: theme.classColor(root.usageClass(root.currentUsage)) font.family: theme.fontFamily font.pixelSize: theme.barFontPixelSize text: "" textFormat: Text.PlainText } }
BarPopup { id: tooltipWindow
readonly property int maxTooltipHeight: root.barWindow && root.barWindow.screen ? Math.max(120, Math.round((root.barWindow.screen.height - root.barWindow.height - margin * 2) * 0.75)) : 480
barWindow: root.barWindow anchorItem: root shown: root.tooltipVisible implicitWidth: 430 implicitHeight: Math.min(maxTooltipHeight, detailsColumn.implicitHeight + 28)
PopupSurface { anchors.fill: parent clip: true
Flickable { anchors.fill: parent anchors.margins: 14 contentWidth: width contentHeight: detailsColumn.implicitHeight clip: true
Column { id: detailsColumn
width: parent.width spacing: 6
LoadHeader { width: parent.width text: "Load" }
UsageHistogram { width: parent.width height: 92 values: root.usageHistory maxSamples: 48 warningThreshold: 90 criticalThreshold: 95 }
Rectangle { width: parent.width height: 1 color: theme.alpha(theme.text, 0.1) }
TooltipHeader { width: parent.width text: "CPU Usage per Core" }
Grid { id: coreGrid
width: parent.width columns: 2 columnSpacing: 8 rowSpacing: 6
Repeater { model: root.cores
CpuCoreBar { required property var modelData
width: (coreGrid.width - coreGrid.columnSpacing) / coreGrid.columns name: root.coreLabel(modelData.name) usage: modelData.usage } } } } } } }
component CpuCoreBar: Item { id: barRoot
required property string name required property real usage
height: 18
ClippingRectangle { anchors.fill: parent radius: 4 color: theme.alpha(theme.crust, 0.3) border.color: theme.alpha(theme.text, 0.1) border.width: 1 contentUnderBorder: true
Rectangle { anchors.left: parent.left anchors.top: parent.top anchors.bottom: parent.bottom width: Math.max(2, parent.width * barRoot.usage / 100) color: root.usageBarColor(barRoot.usage) } }
Row { anchors.fill: parent anchors.leftMargin: 6 anchors.rightMargin: 6
Text { anchors.verticalCenter: parent.verticalCenter width: 48 color: theme.text font.family: theme.fontFamily font.pixelSize: 11 text: barRoot.name }
Text { anchors.verticalCenter: parent.verticalCenter width: parent.width - 48 color: theme.text font.family: theme.fontFamily font.pixelSize: 11 horizontalAlignment: Text.AlignRight text: `${Math.round(barRoot.usage)}%` } } }
component TooltipHeader: Text { color: theme.text font.family: theme.fontFamily font.pixelSize: theme.tooltipFontPixelSize + 1 font.bold: true }
component LoadHeader: Item { required property string text
height: title.implicitHeight
TooltipHeader { id: title
anchors.left: parent.left text: parent.text }
Row { anchors.right: parent.right anchors.verticalCenter: parent.verticalCenter spacing: 14
LoadMetric { label: "1min" value: root.load1 }
LoadMetric { label: "5min" value: root.load5 }
LoadMetric { label: "15min" value: root.load15 } } }
component LoadMetric: Row { required property string label required property string value
spacing: 5
Text { anchors.verticalCenter: parent.verticalCenter color: theme.subtext0 font.family: theme.fontFamily font.pixelSize: theme.tooltipFontPixelSize text: parent.label }
Text { anchors.verticalCenter: parent.verticalCenter width: 38 color: root.loadColor(parent.value) font.family: theme.fontFamily font.pixelSize: theme.tooltipFontPixelSize horizontalAlignment: Text.AlignRight text: root.loadText(parent.value) } }
MouseArea { id: mouse
anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton hoverEnabled: true z: 10 onClicked: root.refresh() onContainsMouseChanged: { root.historySampleCounter = 0; if (containsMouse) root.refresh(); } }
Timer { id: refreshTimer
interval: mouse.containsMouse ? root.hoverInterval : root.interval running: root.moduleVisible && interval > 0 repeat: true onTriggered: root.refresh() onIntervalChanged: { if (running) restart(); } }
Process { id: refreshRunner
stdout: StdioCollector { onStreamFinished: root.applyOutput(this.text) } }
Process { id: loadRunner
stdout: StdioCollector { onStreamFinished: root.applyLoadOutput(this.text) } }
Connections { target: root.ipc function onRefreshRequested(name) { if (name === "cpu" || name === "all") root.refresh(); } }
Component.onCompleted: refresh() onModuleVisibleChanged: { resetSamples(); if (moduleVisible) refresh(); }}