summaryrefslogtreecommitdiff
path: root/src/services/storage.ts
blob: e1e1c55be1d0b8b003d105fd82bb6e8d3a6acc03 (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
50
51
52
53
54
55
56
57
58
59
60
61
import { GObject, interval, property, register } from "astal";
import { storage as config } from "config";
import GTop from "gi://GTop";

@register({ GTypeName: "Storage" })
export default class Storage extends GObject.Object {
    static instance: Storage;
    static get_default() {
        if (!this.instance) this.instance = new Storage();

        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;
    }

    update() {
        const root = new GTop.glibtop_fsusage();
        GTop.glibtop_get_fsusage(root, "/");
        const home = new GTop.glibtop_fsusage();
        GTop.glibtop_get_fsusage(home, "/home");

        this.#total = root.blocks * root.block_size + home.blocks * home.block_size;
        this.#free = root.bavail * root.block_size + home.bavail * home.block_size;
        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());
    }
}