diff --git a/widget/modules/Cpu.tsx b/widget/modules/Cpu.tsx deleted file mode 100644 index 9f645ba..0000000 --- a/widget/modules/Cpu.tsx +++ /dev/null @@ -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("").poll(5000, () => - computeCPU()) - - return - -} \ No newline at end of file diff --git a/widget/modules/Cpu/Cpu.tsx b/widget/modules/Cpu/Cpu.tsx new file mode 100644 index 0000000..ac268c5 --- /dev/null +++ b/widget/modules/Cpu/Cpu.tsx @@ -0,0 +1,16 @@ +import { Variable } from "astal"; +import computeCPU from "./helpers"; + +export default function Cpu() { + const cpu_usage = Variable("").poll(5000, () => computeCPU()); + + return ( + + + ); +} diff --git a/widget/modules/Cpu/helpers.ts b/widget/modules/Cpu/helpers.ts new file mode 100644 index 0000000..74b1703 --- /dev/null +++ b/widget/modules/Cpu/helpers.ts @@ -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; diff --git a/widget/modules/Powermenu/PowerMenu.scss b/widget/modules/Powermenu/PowerMenu.scss index 4966e3e..a45591c 100644 --- a/widget/modules/Powermenu/PowerMenu.scss +++ b/widget/modules/Powermenu/PowerMenu.scss @@ -5,7 +5,6 @@ window.Powermenu { margin: 5px; background-color: black; border-radius: 25px; - // padding: 10px; button { all: unset; margin: 2px; diff --git a/widget/modules/Ram.tsx b/widget/modules/Ram.tsx deleted file mode 100644 index 5ba2236..0000000 --- a/widget/modules/Ram.tsx +++ /dev/null @@ -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("").poll(5000, () => - calculateRamUsage()) - - return - -} \ No newline at end of file diff --git a/widget/modules/Ram/Ram.tsx b/widget/modules/Ram/Ram.tsx new file mode 100644 index 0000000..7e06f6d --- /dev/null +++ b/widget/modules/Ram/Ram.tsx @@ -0,0 +1,16 @@ +import { Variable } from "astal"; +import calculateRamUsage from "./helpers"; + +export default function Ram() { + const ram_usage = Variable("").poll(5000, () => calculateRamUsage()); + + return ( + + + ); +} diff --git a/widget/modules/Ram/helpers.ts b/widget/modules/Ram/helpers.ts new file mode 100644 index 0000000..1eaf109 --- /dev/null +++ b/widget/modules/Ram/helpers.ts @@ -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; diff --git a/widget/modules/Storage.tsx b/widget/modules/Storage.tsx deleted file mode 100644 index 49cc505..0000000 --- a/widget/modules/Storage.tsx +++ /dev/null @@ -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("").poll(5000, () => - computeStorage()) - - return - -} \ No newline at end of file diff --git a/widget/modules/Storage/Storage.tsx b/widget/modules/Storage/Storage.tsx new file mode 100644 index 0000000..78e14f9 --- /dev/null +++ b/widget/modules/Storage/Storage.tsx @@ -0,0 +1,16 @@ +import { Variable } from "astal"; +import computeStorage from "./helpers"; + +export default function Storage() { + const storage_usage = Variable("").poll(5000, () => computeStorage()); + + return ( + + + ); +} diff --git a/widget/modules/Storage/helpers.ts b/widget/modules/Storage/helpers.ts new file mode 100644 index 0000000..c69ad78 --- /dev/null +++ b/widget/modules/Storage/helpers.ts @@ -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; diff --git a/widget/modules/export.ts b/widget/modules/export.ts index 819130f..009853f 100644 --- a/widget/modules/export.ts +++ b/widget/modules/export.ts @@ -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";