From d46b36860666583cd9645bfb1fa28d807973c324 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Wed, 5 Mar 2025 17:37:43 +1100 Subject: schemes: update for scripts refactor --- src/utils/strings.ts | 6 +++++- src/utils/system.ts | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) (limited to 'src/utils') diff --git a/src/utils/strings.ts b/src/utils/strings.ts index 2fd4c76..c2bf71a 100644 --- a/src/utils/strings.ts +++ b/src/utils/strings.ts @@ -1,3 +1,7 @@ export const ellipsize = (str: string, len: number) => (str.length > len ? `${str.slice(0, len - 1)}…` : str); -export const basename = (path: string) => path.slice(path.lastIndexOf("/") + 1, path.lastIndexOf(".")); +export const basename = (path: string) => { + const lastSlash = path.lastIndexOf("/"); + const lastDot = path.lastIndexOf("."); + return path.slice(lastSlash + 1, lastDot > lastSlash ? lastDot : undefined); +}; diff --git a/src/utils/system.ts b/src/utils/system.ts index 7ae23dd..d0470c7 100644 --- a/src/utils/system.ts +++ b/src/utils/system.ts @@ -1,4 +1,4 @@ -import { bind, execAsync, GLib, Variable, type Binding, type Gio } from "astal"; +import { bind, execAsync, Gio, GLib, Variable, type Binding } from "astal"; import type AstalApps from "gi://AstalApps"; import { osIcons } from "./icons"; @@ -75,3 +75,21 @@ export const bindCurrentTime = ( self?.connect("destroy", () => time.drop()); return bind(time); }; + +export const monitorDirectory = (path: string, callback: (path: string) => void, recursive?: boolean) => { + const file = Gio.file_new_for_path(path.replace("~", HOME)); + const monitor = file.monitor_directory(null, null); + monitor.connect("changed", callback); + + const monitors = [monitor]; + + if (recursive) { + const enumerator = file.enumerate_children("standard::*", null, null); + let child; + while ((child = enumerator.next_file(null))) + if (child.get_file_type() === Gio.FileType.DIRECTORY) + monitors.push(...monitorDirectory(`${path}/${child.get_name()}`, callback, recursive)); + } + + return monitors; +}; -- cgit v1.2.3-freya