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/cpu.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 '')
| -rw-r--r-- | src/services/cpu.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/services/cpu.ts b/src/services/cpu.ts new file mode 100644 index 0000000..f1699f7 --- /dev/null +++ b/src/services/cpu.ts @@ -0,0 +1,45 @@ +import { GObject, interval, property, register } from "astal"; +import { cpu as config } from "config"; +import GTop from "gi://GTop"; + +@register({ GTypeName: "Cpu" }) +export default class Cpu extends GObject.Object { + static instance: Cpu; + static get_default() { + if (!this.instance) this.instance = new Cpu(); + + return this.instance; + } + + #previous: GTop.glibtop_cpu = new GTop.glibtop_cpu(); + #usage: number = 0; + + @property(Number) + get usage() { + return this.#usage; + } + + calculateUsage() { + const current = new GTop.glibtop_cpu(); + GTop.glibtop_get_cpu(current); + + // Calculate the differences from the previous to current data + const total = current.total - this.#previous.total; + const idle = current.idle - this.#previous.idle; + + this.#previous = current; + + return total > 0 ? ((total - idle) / total) * 100 : 0; + } + + update() { + this.#usage = this.calculateUsage(); + this.notify("usage"); + } + + constructor() { + super(); + + interval(config.interval, () => this.update()); + } +} |