blob: 4c5174457d6ad656491f041622b9dfa54bf290fe (
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
|
import { basename } from "@/utils/strings";
import { execAsync, GLib, GObject, monitorFile, property, readFileAsync, register } from "astal";
import type { IPalette } from "./palette";
const DATA = `${GLib.get_user_data_dir()}/caelestia`;
@register({ GTypeName: "Schemes" })
export default class Schemes extends GObject.Object {
static instance: Schemes;
static get_default() {
if (!this.instance) this.instance = new Schemes();
return this.instance;
}
#map: { [k: string]: IPalette } = {};
@property(Object)
get map() {
return this.#map;
}
async parseScheme(path: string) {
const schemeColours = (await readFileAsync(path)).split("\n").map(l => l.split(" "));
return schemeColours.reduce((acc, [name, hex]) => ({ ...acc, [name]: `#${hex}` }), {} as IPalette);
}
async update() {
const schemes = await execAsync(`find ${DATA}/scripts/data/schemes/ -type f`);
for (const scheme of schemes.split("\n")) this.#map[basename(scheme)] = await this.parseScheme(scheme);
this.notify("map");
}
constructor() {
super();
this.update().catch(console.error);
monitorFile(`${DATA}/scripts/data/schemes`, () => this.update().catch(console.error));
}
}
|