blob: 5f80d113acd2bd13444ea2cfc3a9e982c0ee96bc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
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();
let source = interval(config.interval.get(), () => this.update());
config.interval.subscribe(i => {
source.cancel();
source = interval(i, () => this.update());
});
}
}
|