✨ feat: add escapeUnintendedMarkup(), getChildren() and pathToURI() utility functions
gtk4 doesn't have a `get_children()` method anymore 😭
This commit is contained in:
+48
-1
@@ -21,7 +21,41 @@ export function getHyprlandInstanceSig(): (string|null) {
|
||||
}
|
||||
|
||||
export function getHyprlandVersion(): string {
|
||||
return exec(`${GLib.getenv("HYPRLAND_CMD") || "Hyprland"} --version | head -n1`).split(" ")[1];
|
||||
return exec(`${GLib.getenv("HYPRLAND_CMD") ?? "Hyprland"} --version | head -n1`).split(" ")[1];
|
||||
}
|
||||
|
||||
export function escapeUnintendedMarkup(input: string): string {
|
||||
return input.replace(/(?<!<[^>]*)[<>&"]/g, (s) => {
|
||||
switch(s) {
|
||||
case "<": return "<";
|
||||
case ">": return ">";
|
||||
case "&": return "&";
|
||||
case "\"": return """;
|
||||
}
|
||||
|
||||
return `\\${s}`;
|
||||
});
|
||||
}
|
||||
|
||||
export function getChildren(widget: Gtk.Widget): Array<Gtk.Widget> {
|
||||
const firstChild = widget.get_first_child();
|
||||
if(!firstChild)
|
||||
return [];
|
||||
|
||||
const children: Array<Gtk.Widget> = [ firstChild ];
|
||||
|
||||
let child: Gtk.Widget = firstChild.get_next_sibling()!;
|
||||
if(!child)
|
||||
return children;
|
||||
|
||||
do {
|
||||
child = (child ?? children[0]).get_next_sibling()!;
|
||||
if(!child) return children;
|
||||
|
||||
children.unshift(child);
|
||||
} while(child != null);
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
export function omitObjectKeys<ObjT = object>(obj: ObjT, keys: keyof ObjT|Array<keyof ObjT>): ObjT {
|
||||
@@ -57,6 +91,19 @@ export function variableToBoolean(variable: any|Array<any>|Accessor<Array<any>|a
|
||||
: Boolean(variable);
|
||||
}
|
||||
|
||||
export function pathToURI(path: string): string {
|
||||
switch(true) {
|
||||
case (/^[/]/).test(path):
|
||||
return `file://${path}`;
|
||||
|
||||
case (/^[~]/).test(path):
|
||||
case (/^file:\/\/[~]/i).test(path):
|
||||
return `file://${GLib.get_home_dir()}/${path.replace(/^(file\:\/\/|[~]|file\:\/\[~])/i, "")}`;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
export function transform<ValueType = any|Array<any>, RType = any>(
|
||||
v: Accessor<ValueType>|ValueType, fn: (v: ValueType) => RType
|
||||
): RType|Accessor<RType> {
|
||||
|
||||
Reference in New Issue
Block a user