refactor
This commit is contained in:
1 parent
dddb4bce11
commit
3edfbc19f6
11 files changed
+134
-116
No files matched your search
@@ -1,34 +0,0 @@
|
||||
import { Variable } from "astal";
|
||||
import GTop from "gi://GTop"
|
||||
|
||||
let previousCpuData = new GTop.glibtop_cpu();
|
||||
GTop.glibtop_get_cpu(previousCpuData);
|
||||
|
||||
const computeCPU = (): string => {
|
||||
const currentCpuData = new GTop.glibtop_cpu();
|
||||
GTop.glibtop_get_cpu(currentCpuData);
|
||||
|
||||
// Calculate the differences from the previous to current data
|
||||
const totalDiff = currentCpuData.total - previousCpuData.total;
|
||||
const idleDiff = currentCpuData.idle - previousCpuData.idle;
|
||||
|
||||
const cpuUsagePercentage = totalDiff > 0 ? ((totalDiff - idleDiff) / totalDiff) * 100 : 0;
|
||||
|
||||
previousCpuData = currentCpuData;
|
||||
|
||||
const cpuUsagePercentageString = " " + Math.floor(cpuUsagePercentage) + "%";
|
||||
|
||||
return cpuUsagePercentageString;
|
||||
}
|
||||
|
||||
export default function Cpu() {
|
||||
const cpu_usage = Variable<string>("").poll(5000, () =>
|
||||
computeCPU())
|
||||
|
||||
return <box className="cpu">
|
||||
<label
|
||||
className="Cpu"
|
||||
onDestroy={() => cpu_usage.drop()}
|
||||
label={cpu_usage()} />
|
||||
</box>
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Variable } from "astal";
|
||||
import computeCPU from "./helpers";
|
||||
|
||||
export default function Cpu() {
|
||||
const cpu_usage = Variable<string>("").poll(5000, () => computeCPU());
|
||||
|
||||
return (
|
||||
<box className="cpu">
|
||||
<label
|
||||
className="Cpu"
|
||||
onDestroy={() => cpu_usage.drop()}
|
||||
label={cpu_usage()}
|
||||
/>
|
||||
</box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import GTop from "gi://GTop";
|
||||
|
||||
let previousCpuData = new GTop.glibtop_cpu();
|
||||
GTop.glibtop_get_cpu(previousCpuData);
|
||||
|
||||
const computeCPU = (): string => {
|
||||
const currentCpuData = new GTop.glibtop_cpu();
|
||||
GTop.glibtop_get_cpu(currentCpuData);
|
||||
|
||||
// Calculate the differences from the previous to current data
|
||||
const totalDiff = currentCpuData.total - previousCpuData.total;
|
||||
const idleDiff = currentCpuData.idle - previousCpuData.idle;
|
||||
|
||||
const cpuUsagePercentage =
|
||||
totalDiff > 0 ? ((totalDiff - idleDiff) / totalDiff) * 100 : 0;
|
||||
|
||||
previousCpuData = currentCpuData;
|
||||
|
||||
const cpuUsagePercentageString =
|
||||
" " + Math.floor(cpuUsagePercentage) + "%";
|
||||
|
||||
return cpuUsagePercentageString;
|
||||
};
|
||||
|
||||
export default computeCPU;
|
||||
@@ -5,7 +5,6 @@ window.Powermenu {
|
||||
margin: 5px;
|
||||
background-color: black;
|
||||
border-radius: 25px;
|
||||
// padding: 10px;
|
||||
button {
|
||||
all: unset;
|
||||
margin: 2px;
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
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>
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Variable } from "astal";
|
||||
import calculateRamUsage from "./helpers";
|
||||
|
||||
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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { 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 calculateRamUsage;
|
||||
@@ -1,33 +0,0 @@
|
||||
import { Variable } from "astal";
|
||||
import GTop from 'gi://GTop';
|
||||
|
||||
const computeStorage = (): string => {
|
||||
try {
|
||||
const currentFsUsage = new GTop.glibtop_fsusage();
|
||||
|
||||
GTop.glibtop_get_fsusage(currentFsUsage, '/');
|
||||
|
||||
const total = currentFsUsage.blocks * currentFsUsage.block_size;
|
||||
const available = currentFsUsage.bavail * currentFsUsage.block_size;
|
||||
const used = total - available;
|
||||
|
||||
const storagestring = " " + Math.floor(used / total * 100) + "%"
|
||||
|
||||
return storagestring
|
||||
} catch (error) {
|
||||
console.error('Error calculating RAM usage:', error);
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
export default function Storage() {
|
||||
const storage_usage = Variable<string>("").poll(5000, () =>
|
||||
computeStorage())
|
||||
|
||||
return <box className="storage">
|
||||
<label
|
||||
className="Storage"
|
||||
onDestroy={() => storage_usage.drop()}
|
||||
label={storage_usage()} />
|
||||
</box>
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Variable } from "astal";
|
||||
import computeStorage from "./helpers";
|
||||
|
||||
export default function Storage() {
|
||||
const storage_usage = Variable<string>("").poll(5000, () => computeStorage());
|
||||
|
||||
return (
|
||||
<box className="storage">
|
||||
<label
|
||||
className="Storage"
|
||||
onDestroy={() => storage_usage.drop()}
|
||||
label={storage_usage()}
|
||||
/>
|
||||
</box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import GTop from "gi://GTop";
|
||||
|
||||
const computeStorage = (): string => {
|
||||
try {
|
||||
const currentFsUsage = new GTop.glibtop_fsusage();
|
||||
|
||||
GTop.glibtop_get_fsusage(currentFsUsage, "/");
|
||||
|
||||
const total = currentFsUsage.blocks * currentFsUsage.block_size;
|
||||
const available = currentFsUsage.bavail * currentFsUsage.block_size;
|
||||
const used = total - available;
|
||||
|
||||
const storagestring = " " + Math.floor((used / total) * 100) + "%";
|
||||
|
||||
return storagestring;
|
||||
} catch (error) {
|
||||
console.error("Error calculating RAM usage:", error);
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
export default computeStorage;
|
||||
@@ -1,9 +1,9 @@
|
||||
import BatteryLevel from "./Battery";
|
||||
import Cpu from "./Cpu";
|
||||
import Cpu from "./Cpu/Cpu";
|
||||
import FocusedClient from "./FocusedClient";
|
||||
import PowerMenu from "./Powermenu/PowerMenu";
|
||||
import Ram from "./Ram";
|
||||
import Storage from "./Storage";
|
||||
import Ram from "./Ram/Ram";
|
||||
import Storage from "./Storage/Storage";
|
||||
import SysTray from "./SysTray";
|
||||
import Time from "./Time";
|
||||
import Workspaces from "./Workspaces";
|
||||
|
||||
Reference in new issue
Block a user