refactor and add modules directory
This commit is contained in:
1 parent
b272840eae
commit
8f5ad70ed3
13 files changed
+209
-90
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import { Variable, GLib } from "astal";
|
||||
|
||||
const calculateRamUsage = (): string => {
|
||||
try {
|
||||
const [success, meminfoBytes] = GLib.file_get_contents('/proc/meminfo');
|
||||
|
||||
if (!success || !meminfoBytes) {
|
||||
throw new Error('Failed to read /proc/meminfo or file content is null.');
|
||||
}
|
||||
|
||||
const meminfo = new TextDecoder('utf-8').decode(meminfoBytes);
|
||||
|
||||
const totalMatch = meminfo.match(/MemTotal:\s+(\d+)/);
|
||||
const availableMatch = meminfo.match(/MemAvailable:\s+(\d+)/);
|
||||
|
||||
if (!totalMatch || !availableMatch) {
|
||||
throw new Error('Failed to parse /proc/meminfo for memory values.');
|
||||
}
|
||||
|
||||
const totalRamInBytes = parseInt(totalMatch[1], 10) * 1024;
|
||||
const availableRamInBytes = parseInt(availableMatch[1], 10) * 1024;
|
||||
|
||||
let usedRam = totalRamInBytes - availableRamInBytes;
|
||||
usedRam = isNaN(usedRam) || usedRam < 0 ? 0 : usedRam;
|
||||
|
||||
const ramstring = " " + Math.floor(usedRam / totalRamInBytes * 100) + "%"
|
||||
|
||||
return ramstring
|
||||
} catch (error) {
|
||||
console.error('Error calculating RAM usage:', error);
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
export default function Ram() {
|
||||
const ram_usage = Variable<string>("").poll(5000, () =>
|
||||
calculateRamUsage())
|
||||
|
||||
return <box className="ram">
|
||||
<label
|
||||
className="Ram"
|
||||
onDestroy={() => ram_usage.drop()}
|
||||
label={ram_usage()} />
|
||||
</box>
|
||||
}
|
||||
Reference in new issue
Block a user