fix(ags-v3): nice-to-have — portabilité, perf, réactivité
- Prompt.tsx: GLib.get_user_name() au lieu de hardcode "tetardtek" - ghost-shell.desktop: $HOME au lieu de chemin absolu - SystemStats.tsx: lecture /proc/stat + /proc/meminfo (zero fork, économie batterie) - Battery.tsx: createDerivedBinding percentage+charging — réactif sur branchement
This commit is contained in:
@@ -1,21 +1,50 @@
|
||||
import GLib from "gi://GLib"
|
||||
import { createPoll } from "ags/time"
|
||||
import { exec } from "ags/process"
|
||||
|
||||
function readProc(path: string): string | null {
|
||||
try {
|
||||
const [ok, contents] = GLib.file_get_contents(path)
|
||||
if (ok && contents) return new TextDecoder().decode(contents)
|
||||
} catch {}
|
||||
return null
|
||||
}
|
||||
|
||||
let prevIdle = 0
|
||||
let prevTotal = 0
|
||||
|
||||
function getCpuUsage(): string {
|
||||
try {
|
||||
const out = exec("bash -c \"top -bn1 | grep 'Cpu(s)' | awk '{print 100 - $8}'\"")
|
||||
return `${Math.round(parseFloat(out))}`
|
||||
} catch {
|
||||
return "?"
|
||||
}
|
||||
const content = readProc("/proc/stat")
|
||||
if (!content) return "?"
|
||||
|
||||
const line = content.split("\n")[0] // "cpu user nice system idle ..."
|
||||
const parts = line.split(/\s+/).slice(1).map(Number)
|
||||
const idle = parts[3] + parts[4] // idle + iowait
|
||||
const total = parts.reduce((a, b) => a + b, 0)
|
||||
|
||||
const dIdle = idle - prevIdle
|
||||
const dTotal = total - prevTotal
|
||||
prevIdle = idle
|
||||
prevTotal = total
|
||||
|
||||
if (dTotal === 0) return "0"
|
||||
return `${Math.round(((dTotal - dIdle) / dTotal) * 100)}`
|
||||
}
|
||||
|
||||
function getRamUsage(): string {
|
||||
try {
|
||||
return exec("bash -c \"free | awk '/Mem:/ {printf \\\"%.0f\\\", $3/$2 * 100}'\"")
|
||||
} catch {
|
||||
return "?"
|
||||
const content = readProc("/proc/meminfo")
|
||||
if (!content) return "?"
|
||||
|
||||
const lines = content.split("\n")
|
||||
let total = 0, available = 0
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("MemTotal:")) total = parseInt(line.split(/\s+/)[1])
|
||||
if (line.startsWith("MemAvailable:")) available = parseInt(line.split(/\s+/)[1])
|
||||
if (total && available) break
|
||||
}
|
||||
|
||||
if (!total) return "?"
|
||||
return `${Math.round(((total - available) / total) * 100)}`
|
||||
}
|
||||
|
||||
export default function SystemStats() {
|
||||
|
||||
Reference in New Issue
Block a user