mirror of
https://github.com/noctalia-dev/noctalia-shell.git
synced 2026-05-11 17:08:27 +08:00
Switched to qmlformat.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
pragma Singleton
|
||||
import Qt.labs.folderlistmodel
|
||||
|
||||
import QtQuick
|
||||
import Qt.labs.folderlistmodel
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import qs.Commons
|
||||
@@ -42,10 +42,10 @@ Singleton {
|
||||
|
||||
// --------------------------------------------
|
||||
Component.onCompleted: {
|
||||
Logger.i("SystemStat", "Service started with interval:", root.sleepDuration, "ms")
|
||||
Logger.i("SystemStat", "Service started with interval:", root.sleepDuration, "ms");
|
||||
|
||||
// Kickoff the cpu name detection for temperature
|
||||
cpuTempNameReader.checkNext()
|
||||
cpuTempNameReader.checkNext();
|
||||
}
|
||||
|
||||
// --------------------------------------------
|
||||
@@ -58,14 +58,14 @@ Singleton {
|
||||
triggeredOnStart: true
|
||||
onTriggered: {
|
||||
// Trigger all direct system files reads
|
||||
memInfoFile.reload()
|
||||
cpuStatFile.reload()
|
||||
netDevFile.reload()
|
||||
memInfoFile.reload();
|
||||
cpuStatFile.reload();
|
||||
netDevFile.reload();
|
||||
|
||||
// Run df (disk free) one time
|
||||
dfProcess.running = true
|
||||
dfProcess.running = true;
|
||||
|
||||
updateCpuTemperature()
|
||||
updateCpuTemperature();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,18 +99,18 @@ Singleton {
|
||||
running: false
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
const lines = text.trim().split('\n')
|
||||
const newPercents = {}
|
||||
const lines = text.trim().split('\n');
|
||||
const newPercents = {};
|
||||
// Start from line 1 (skip header)
|
||||
for (var i = 1; i < lines.length; i++) {
|
||||
const parts = lines[i].trim().split(/\s+/)
|
||||
const parts = lines[i].trim().split(/\s+/);
|
||||
if (parts.length >= 2) {
|
||||
const target = parts[0]
|
||||
const percent = parseInt(parts[1].replace(/[^0-9]/g, '')) || 0
|
||||
newPercents[target] = percent
|
||||
const target = parts[0];
|
||||
const percent = parseInt(parts[1].replace(/[^0-9]/g, '')) || 0;
|
||||
newPercents[target] = percent;
|
||||
}
|
||||
}
|
||||
root.diskPercents = newPercents
|
||||
root.diskPercents = newPercents;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,36 +129,36 @@ Singleton {
|
||||
function checkNext() {
|
||||
if (currentIndex >= 16) {
|
||||
// Check up to hwmon10
|
||||
Logger.w("No supported temperature sensor found")
|
||||
return
|
||||
Logger.w("No supported temperature sensor found");
|
||||
return;
|
||||
}
|
||||
|
||||
//Logger.i("SystemStat", "---- Probing: hwmon", currentIndex)
|
||||
cpuTempNameReader.path = `/sys/class/hwmon/hwmon${currentIndex}/name`
|
||||
cpuTempNameReader.reload()
|
||||
cpuTempNameReader.path = `/sys/class/hwmon/hwmon${currentIndex}/name`;
|
||||
cpuTempNameReader.reload();
|
||||
}
|
||||
|
||||
onLoaded: {
|
||||
const name = text().trim()
|
||||
const name = text().trim();
|
||||
if (root.supportedTempCpuSensorNames.includes(name)) {
|
||||
root.cpuTempSensorName = name
|
||||
root.cpuTempHwmonPath = `/sys/class/hwmon/hwmon${currentIndex}`
|
||||
Logger.i("SystemStat", `Found ${root.cpuTempSensorName} CPU thermal sensor at ${root.cpuTempHwmonPath}`)
|
||||
root.cpuTempSensorName = name;
|
||||
root.cpuTempHwmonPath = `/sys/class/hwmon/hwmon${currentIndex}`;
|
||||
Logger.i("SystemStat", `Found ${root.cpuTempSensorName} CPU thermal sensor at ${root.cpuTempHwmonPath}`);
|
||||
} else {
|
||||
currentIndex++
|
||||
currentIndex++;
|
||||
Qt.callLater(() => {
|
||||
// Qt.callLater is mandatory
|
||||
checkNext()
|
||||
})
|
||||
checkNext();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onLoadFailed: function (error) {
|
||||
currentIndex++
|
||||
currentIndex++;
|
||||
Qt.callLater(() => {
|
||||
// Qt.callLater is mandatory
|
||||
checkNext()
|
||||
})
|
||||
checkNext();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,26 +169,26 @@ Singleton {
|
||||
printErrors: false
|
||||
|
||||
onLoaded: {
|
||||
const data = text().trim()
|
||||
const data = text().trim();
|
||||
if (root.cpuTempSensorName === "coretemp") {
|
||||
// For Intel, collect all temperature values
|
||||
const temp = parseInt(data) / 1000.0
|
||||
const temp = parseInt(data) / 1000.0;
|
||||
//console.log(temp, cpuTempReader.path)
|
||||
root.intelTempValues.push(temp)
|
||||
root.intelTempValues.push(temp);
|
||||
Qt.callLater(() => {
|
||||
// Qt.callLater is mandatory
|
||||
checkNextIntelTemp()
|
||||
})
|
||||
checkNextIntelTemp();
|
||||
});
|
||||
} else {
|
||||
// For AMD sensors (k10temp and zenpower), directly set the temperature
|
||||
root.cpuTemp = Math.round(parseInt(data) / 1000.0)
|
||||
root.cpuTemp = Math.round(parseInt(data) / 1000.0);
|
||||
}
|
||||
}
|
||||
onLoadFailed: function (error) {
|
||||
Qt.callLater(() => {
|
||||
// Qt.callLater is mandatory
|
||||
checkNextIntelTemp()
|
||||
})
|
||||
checkNextIntelTemp();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,24 +197,23 @@ Singleton {
|
||||
// Parse memory info from /proc/meminfo
|
||||
function parseMemoryInfo(text) {
|
||||
if (!text)
|
||||
return
|
||||
|
||||
const lines = text.split('\n')
|
||||
let memTotal = 0
|
||||
let memAvailable = 0
|
||||
return;
|
||||
const lines = text.split('\n');
|
||||
let memTotal = 0;
|
||||
let memAvailable = 0;
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('MemTotal:')) {
|
||||
memTotal = parseInt(line.split(/\s+/)[1]) || 0
|
||||
memTotal = parseInt(line.split(/\s+/)[1]) || 0;
|
||||
} else if (line.startsWith('MemAvailable:')) {
|
||||
memAvailable = parseInt(line.split(/\s+/)[1]) || 0
|
||||
memAvailable = parseInt(line.split(/\s+/)[1]) || 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (memTotal > 0) {
|
||||
const usageKb = memTotal - memAvailable
|
||||
root.memGb = (usageKb / 1048576).toFixed(1) // 1024*1024 = 1048576
|
||||
root.memPercent = Math.round((usageKb / memTotal) * 100)
|
||||
const usageKb = memTotal - memAvailable;
|
||||
root.memGb = (usageKb / 1048576).toFixed(1); // 1024*1024 = 1048576
|
||||
root.memPercent = Math.round((usageKb / memTotal) * 100);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,16 +221,14 @@ Singleton {
|
||||
// Calculate CPU usage from /proc/stat
|
||||
function calculateCpuUsage(text) {
|
||||
if (!text)
|
||||
return
|
||||
|
||||
const lines = text.split('\n')
|
||||
const cpuLine = lines[0]
|
||||
return;
|
||||
const lines = text.split('\n');
|
||||
const cpuLine = lines[0];
|
||||
|
||||
// First line is total CPU
|
||||
if (!cpuLine.startsWith('cpu '))
|
||||
return
|
||||
|
||||
const parts = cpuLine.split(/\s+/)
|
||||
return;
|
||||
const parts = cpuLine.split(/\s+/);
|
||||
const stats = {
|
||||
"user": parseInt(parts[1]) || 0,
|
||||
"nice": parseInt(parts[2]) || 0,
|
||||
@@ -243,23 +240,23 @@ Singleton {
|
||||
"steal": parseInt(parts[8]) || 0,
|
||||
"guest": parseInt(parts[9]) || 0,
|
||||
"guestNice": parseInt(parts[10]) || 0
|
||||
}
|
||||
const totalIdle = stats.idle + stats.iowait
|
||||
const total = Object.values(stats).reduce((sum, val) => sum + val, 0)
|
||||
};
|
||||
const totalIdle = stats.idle + stats.iowait;
|
||||
const total = Object.values(stats).reduce((sum, val) => sum + val, 0);
|
||||
|
||||
if (root.prevCpuStats) {
|
||||
const prevTotalIdle = root.prevCpuStats.idle + root.prevCpuStats.iowait
|
||||
const prevTotal = Object.values(root.prevCpuStats).reduce((sum, val) => sum + val, 0)
|
||||
const prevTotalIdle = root.prevCpuStats.idle + root.prevCpuStats.iowait;
|
||||
const prevTotal = Object.values(root.prevCpuStats).reduce((sum, val) => sum + val, 0);
|
||||
|
||||
const diffTotal = total - prevTotal
|
||||
const diffIdle = totalIdle - prevTotalIdle
|
||||
const diffTotal = total - prevTotal;
|
||||
const diffIdle = totalIdle - prevTotalIdle;
|
||||
|
||||
if (diffTotal > 0) {
|
||||
root.cpuUsage = (((diffTotal - diffIdle) / diffTotal) * 100).toFixed(1)
|
||||
root.cpuUsage = (((diffTotal - diffIdle) / diffTotal) * 100).toFixed(1);
|
||||
}
|
||||
}
|
||||
|
||||
root.prevCpuStats = stats
|
||||
root.prevCpuStats = stats;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
@@ -267,82 +264,82 @@ Singleton {
|
||||
// Average speed of all interfaces excepted 'lo'
|
||||
function calculateNetworkSpeed(text) {
|
||||
if (!text) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
const currentTime = Date.now() / 1000
|
||||
const lines = text.split('\n')
|
||||
const currentTime = Date.now() / 1000;
|
||||
const lines = text.split('\n');
|
||||
|
||||
let totalRx = 0
|
||||
let totalTx = 0
|
||||
let totalRx = 0;
|
||||
let totalTx = 0;
|
||||
|
||||
for (var i = 2; i < lines.length; i++) {
|
||||
const line = lines[i].trim()
|
||||
const line = lines[i].trim();
|
||||
if (!line) {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
const colonIndex = line.indexOf(':')
|
||||
const colonIndex = line.indexOf(':');
|
||||
if (colonIndex === -1) {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
const iface = line.substring(0, colonIndex).trim()
|
||||
const iface = line.substring(0, colonIndex).trim();
|
||||
if (iface === 'lo') {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
const statsLine = line.substring(colonIndex + 1).trim()
|
||||
const stats = statsLine.split(/\s+/)
|
||||
const statsLine = line.substring(colonIndex + 1).trim();
|
||||
const stats = statsLine.split(/\s+/);
|
||||
|
||||
const rxBytes = parseInt(stats[0], 10) || 0
|
||||
const txBytes = parseInt(stats[8], 10) || 0
|
||||
const rxBytes = parseInt(stats[0], 10) || 0;
|
||||
const txBytes = parseInt(stats[8], 10) || 0;
|
||||
|
||||
totalRx += rxBytes
|
||||
totalTx += txBytes
|
||||
totalRx += rxBytes;
|
||||
totalTx += txBytes;
|
||||
}
|
||||
|
||||
// Compute only if we have a previous run to compare to.
|
||||
if (root.prevTime > 0) {
|
||||
const timeDiff = currentTime - root.prevTime
|
||||
const timeDiff = currentTime - root.prevTime;
|
||||
|
||||
// Avoid division by zero if time hasn't passed.
|
||||
if (timeDiff > 0) {
|
||||
let rxDiff = totalRx - root.prevRxBytes
|
||||
let txDiff = totalTx - root.prevTxBytes
|
||||
let rxDiff = totalRx - root.prevRxBytes;
|
||||
let txDiff = totalTx - root.prevTxBytes;
|
||||
|
||||
// Handle counter resets (e.g., WiFi reconnect), which would cause a negative value.
|
||||
if (rxDiff < 0) {
|
||||
rxDiff = 0
|
||||
rxDiff = 0;
|
||||
}
|
||||
if (txDiff < 0) {
|
||||
txDiff = 0
|
||||
txDiff = 0;
|
||||
}
|
||||
|
||||
root.rxSpeed = Math.round(rxDiff / timeDiff) // Speed in Bytes/s
|
||||
root.txSpeed = Math.round(txDiff / timeDiff)
|
||||
root.rxSpeed = Math.round(rxDiff / timeDiff); // Speed in Bytes/s
|
||||
root.txSpeed = Math.round(txDiff / timeDiff);
|
||||
}
|
||||
}
|
||||
|
||||
root.prevRxBytes = totalRx
|
||||
root.prevTxBytes = totalTx
|
||||
root.prevTime = currentTime
|
||||
root.prevRxBytes = totalRx;
|
||||
root.prevTxBytes = totalTx;
|
||||
root.prevTime = currentTime;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Helper function to format network speeds
|
||||
function formatSpeed(bytesPerSecond) {
|
||||
if (bytesPerSecond < 1024 * 1024) {
|
||||
const kb = bytesPerSecond / 1024
|
||||
const kb = bytesPerSecond / 1024;
|
||||
if (kb < 10) {
|
||||
return kb.toFixed(1) + "KB"
|
||||
return kb.toFixed(1) + "KB";
|
||||
} else {
|
||||
return Math.round(kb) + "KB"
|
||||
return Math.round(kb) + "KB";
|
||||
}
|
||||
} else if (bytesPerSecond < 1024 * 1024 * 1024) {
|
||||
return (bytesPerSecond / (1024 * 1024)).toFixed(1) + "MB"
|
||||
return (bytesPerSecond / (1024 * 1024)).toFixed(1) + "MB";
|
||||
} else {
|
||||
return (bytesPerSecond / (1024 * 1024 * 1024)).toFixed(1) + "GB"
|
||||
return (bytesPerSecond / (1024 * 1024 * 1024)).toFixed(1) + "GB";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,21 +347,21 @@ Singleton {
|
||||
// Compact speed formatter for vertical bar display
|
||||
function formatCompactSpeed(bytesPerSecond) {
|
||||
if (!bytesPerSecond || bytesPerSecond <= 0)
|
||||
return "0"
|
||||
const units = ["", "K", "M", "G"]
|
||||
let value = bytesPerSecond
|
||||
let unitIndex = 0
|
||||
return "0";
|
||||
const units = ["", "K", "M", "G"];
|
||||
let value = bytesPerSecond;
|
||||
let unitIndex = 0;
|
||||
while (value >= 1024 && unitIndex < units.length - 1) {
|
||||
value = value / 1024.0
|
||||
unitIndex++
|
||||
value = value / 1024.0;
|
||||
unitIndex++;
|
||||
}
|
||||
// Promote at ~100 of current unit (e.g., 100k -> ~0.1M shown as 0.1M or 0M if rounded)
|
||||
if (unitIndex < units.length - 1 && value >= 100) {
|
||||
value = value / 1024.0
|
||||
unitIndex++
|
||||
value = value / 1024.0;
|
||||
unitIndex++;
|
||||
}
|
||||
const display = Math.round(value).toString()
|
||||
return display + units[unitIndex]
|
||||
const display = Math.round(value).toString();
|
||||
return display + units[unitIndex];
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
@@ -373,13 +370,13 @@ Singleton {
|
||||
// For AMD sensors (k10temp and zenpower), only use Tctl sensor
|
||||
// temp1_input corresponds to Tctl (Temperature Control) on these sensors
|
||||
if (root.cpuTempSensorName === "k10temp" || root.cpuTempSensorName === "zenpower") {
|
||||
cpuTempReader.path = `${root.cpuTempHwmonPath}/temp1_input`
|
||||
cpuTempReader.reload()
|
||||
cpuTempReader.path = `${root.cpuTempHwmonPath}/temp1_input`;
|
||||
cpuTempReader.reload();
|
||||
} // For Intel coretemp, start averaging all available sensors/cores
|
||||
else if (root.cpuTempSensorName === "coretemp") {
|
||||
root.intelTempValues = []
|
||||
root.intelTempFilesChecked = 0
|
||||
checkNextIntelTemp()
|
||||
root.intelTempValues = [];
|
||||
root.intelTempFilesChecked = 0;
|
||||
checkNextIntelTemp();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -389,22 +386,22 @@ Singleton {
|
||||
if (root.intelTempFilesChecked >= root.intelTempMaxFiles) {
|
||||
// Calculate average of all found temperatures
|
||||
if (root.intelTempValues.length > 0) {
|
||||
let sum = 0
|
||||
let sum = 0;
|
||||
for (var i = 0; i < root.intelTempValues.length; i++) {
|
||||
sum += root.intelTempValues[i]
|
||||
sum += root.intelTempValues[i];
|
||||
}
|
||||
root.cpuTemp = Math.round(sum / root.intelTempValues.length)
|
||||
root.cpuTemp = Math.round(sum / root.intelTempValues.length);
|
||||
//Logger.i("SystemStat", `Averaged ${root.intelTempValues.length} CPU thermal sensors: ${root.cpuTemp}°C`)
|
||||
} else {
|
||||
Logger.w("SystemStat", "No temperature sensors found for coretemp")
|
||||
root.cpuTemp = 0
|
||||
Logger.w("SystemStat", "No temperature sensors found for coretemp");
|
||||
root.cpuTemp = 0;
|
||||
}
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
// Check next temperature file
|
||||
root.intelTempFilesChecked++
|
||||
cpuTempReader.path = `${root.cpuTempHwmonPath}/temp${root.intelTempFilesChecked}_input`
|
||||
cpuTempReader.reload()
|
||||
root.intelTempFilesChecked++;
|
||||
cpuTempReader.path = `${root.cpuTempHwmonPath}/temp${root.intelTempFilesChecked}_input`;
|
||||
cpuTempReader.reload();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user