diff options
Diffstat (limited to 'lang/gjs/lib/file.ts')
-rw-r--r-- | lang/gjs/lib/file.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lang/gjs/lib/file.ts b/lang/gjs/lib/file.ts new file mode 100644 index 0000000..7b9de3a --- /dev/null +++ b/lang/gjs/lib/file.ts @@ -0,0 +1,45 @@ +import Astal from "gi://AstalIO" +import Gio from "gi://Gio" + +export function readFile(path: string): string { + return Astal.read_file(path) || "" +} + +export function readFileAsync(path: string): Promise<string> { + return new Promise((resolve, reject) => { + Astal.read_file_async(path, (_, res) => { + try { + resolve(Astal.read_file_finish(res) || "") + } + catch (error) { + reject(error) + } + }) + }) +} + +export function writeFile(path: string, content: string): void { + Astal.write_file(path, content) +} + +export function writeFileAsync(path: string, content: string): Promise<void> { + return new Promise((resolve, reject) => { + Astal.write_file_async(path, content, (_, res) => { + try { + resolve(Astal.write_file_finish(res)) + } + catch (error) { + reject(error) + } + }) + }) +} + +export function monitorFile( + path: string, + callback: (file: string, event: Gio.FileMonitorEvent) => void, +): Gio.FileMonitor { + return Astal.monitor_file(path, (file: string, event: Gio.FileMonitorEvent) => { + callback(file, event) + })! +} |