52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { Variable, bind } from "astal";
|
|
import { readFileAsync, monitorFile } from "astal/file";
|
|
import Gio from "gi://Gio";
|
|
|
|
export class System {
|
|
private static _instance: System;
|
|
|
|
private async read_name() {
|
|
const content = await readFileAsync("/etc/machine-info");
|
|
|
|
const lines = content
|
|
.split("\n")
|
|
.map((line) => line.trim())
|
|
.map((line) => /^PRETTY_HOSTNAME="?([^"]*)"?$/.exec(line))
|
|
.filter((line) => line !== null)
|
|
.map((line) => line[1]);
|
|
if (lines.length > 0) {
|
|
this.#name.set(lines[0]);
|
|
}
|
|
}
|
|
|
|
private constructor() {
|
|
monitorFile("/etc/machine-info", async (file, event) => {
|
|
console.log("file changed", file, event);
|
|
switch (event) {
|
|
case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
|
|
await this.read_name();
|
|
break;
|
|
}
|
|
});
|
|
this.read_name();
|
|
}
|
|
|
|
public static get_default(): System {
|
|
if (!System._instance) {
|
|
System._instance = new System();
|
|
}
|
|
return System._instance;
|
|
}
|
|
|
|
#name: Variable<string> = Variable("");
|
|
|
|
/**
|
|
* The "pretty" hostname of this computer, as a bound value.
|
|
*
|
|
* This value is updated when the `/etc/machine-info` file changes.
|
|
*/
|
|
public get name() {
|
|
return bind(this.#name);
|
|
}
|
|
}
|