ags(runner/plugins/shell): better shell plugin

This commit is contained in:
retrozinndev
2025-05-11 22:03:51 -03:00
parent 2b202cbb02
commit 0a206ebd8d
+55 -14
View File
@@ -1,18 +1,59 @@
import { ResultWidget, ResultWidgetProps } from "../../widget/runner/ResultWidget"; import { ResultWidget, ResultWidgetProps } from "../../widget/runner/ResultWidget";
import AstalHyprland from "gi://AstalHyprland"; import { Gio, GLib } from "astal";
import { GLib } from "astal";
import { Runner } from "../Runner"; import { Runner } from "../Runner";
import { Notifications } from "../../scripts/notifications";
export const PluginShell = { export const PluginShell = (() => {
prefix: '!',
handle: (command: string): ResultWidget => {
const shell = GLib.getenv("SHELL") || "/usr/bin/env sh";
return new ResultWidget({ const shell = GLib.getenv("SHELL") ?? "/bin/sh";
onClick: () => AstalHyprland.get_default().dispatch("exec", `${shell} -c '${command}'`), const procLauncher = Gio.SubprocessLauncher.new(
title: `Run: \`${command}\``, Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE);
description: shell,
icon: "utilities-terminal-symbolic" procLauncher.set_cwd(GLib.get_home_dir());
} as ResultWidgetProps)
} return {
} as Runner.Plugin; prefix: '!',
prioritize: true,
handle: (input: string): ResultWidget => {
let showOutputNotif: boolean = false;
if(input.startsWith('!')) {
input = input.replace('!', "");
showOutputNotif = true;
}
const command = input ? GLib.shell_parse_argv(input) : undefined;
return new ResultWidget({
onClick: () => {
if(!command || !command[0]) return;
const proc = procLauncher.spawnv([ shell, "-c", `${input}` ]);
proc.communicate_utf8_async(null, null, (_, asyncResult) => {
const [ success, stdout, stderr ] = proc.communicate_utf8_finish(asyncResult);
if(!success || stderr) {
Notifications.getDefault().sendNotification({
appName: shell,
summary: "Command error",
body: `An error occurred on \`${input}\`. Stderr: ${stderr}`
});
return;
}
if(!showOutputNotif) return;
Notifications.getDefault().sendNotification({
appName: shell,
summary: "Command output",
body: stdout
});
});
},
title: `Run ${input ? ` \`${input}\`` : `with ${shell.split('/')[shell.split('/').length-1]}`}`,
description: (input || showOutputNotif) && `${input ? `${shell}\t` : ""}${ showOutputNotif ? "(showing output on notification)" : "" }`,
icon: "utilities-terminal-symbolic"
} as ResultWidgetProps)
}
} as Runner.Plugin
})();