diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-01-30 21:31:19 +1100 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-01-30 21:31:19 +1100 |
| commit | ad22dbdfebbb0def2ec2d8e2c91469e9a9e4fdf7 (patch) | |
| tree | 826e715ae0aa7d4c5bcd29d76421a211b920dca1 /src/services/memory.ts | |
| parent | sideright: fix assertion row != -1 (diff) | |
| download | caelestia-shell-ad22dbdfebbb0def2ec2d8e2c91469e9a9e4fdf7.tar.gz caelestia-shell-ad22dbdfebbb0def2ec2d8e2c91469e9a9e4fdf7.tar.bz2 caelestia-shell-ad22dbdfebbb0def2ec2d8e2c91469e9a9e4fdf7.zip | |
sideleft: create popdown window
Diffstat (limited to 'src/services/memory.ts')
| -rw-r--r-- | src/services/memory.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/services/memory.ts b/src/services/memory.ts new file mode 100644 index 0000000..74fa228 --- /dev/null +++ b/src/services/memory.ts @@ -0,0 +1,60 @@ +import { GObject, interval, property, readFileAsync, register } from "astal"; +import { memory as config } from "config"; + +@register({ GTypeName: "Memory" }) +export default class Memory extends GObject.Object { + static instance: Memory; + static get_default() { + if (!this.instance) this.instance = new Memory(); + + return this.instance; + } + + #total: number = 0; + #free: number = 0; + #used: number = 0; + #usage: number = 0; + + @property(Number) + get total() { + return this.#total; + } + + @property(Number) + get free() { + return this.#free; + } + + @property(Number) + get used() { + return this.#used; + } + + @property(Number) + get usage() { + return this.#usage; + } + + async update() { + const info = await readFileAsync("/proc/meminfo"); + this.#total = parseInt(info.match(/MemTotal:\s+(\d+)/)?.[1] ?? "0", 10) * 1024; + this.#free = parseInt(info.match(/MemAvailable:\s+(\d+)/)?.[1] ?? "0", 10) * 1024; + + if (isNaN(this.#total)) this.#total = 0; + if (isNaN(this.#free)) this.#free = 0; + + this.#used = this.#total - this.#free; + this.#usage = this.#total > 0 ? (this.#used / this.#total) * 100 : 0; + + this.notify("total"); + this.notify("free"); + this.notify("used"); + this.notify("usage"); + } + + constructor() { + super(); + + interval(config.interval, () => this.update().catch(console.error)); + } +} |