🔧 chore(arg-handler): more organized and supporting more arguments
This commit is contained in:
+47
-32
@@ -4,20 +4,25 @@ import { restartInstance } from "./reload-handler";
|
||||
import { timeout } from "ags/time";
|
||||
import { Runner } from "../runner/Runner";
|
||||
import { showWorkspaceNumber } from "../widget/bar/Workspaces";
|
||||
|
||||
import AstalIO from "gi://AstalIO";
|
||||
import { playSystemBell } from "./utils";
|
||||
import { Config } from "./config";
|
||||
|
||||
import AstalIO from "gi://AstalIO";
|
||||
import GLib from "gi://GLib?version=2.0";
|
||||
import App from "ags/gtk4/app";
|
||||
|
||||
|
||||
let wsTimeout: (AstalIO.Time|undefined);
|
||||
|
||||
export function handleArguments(request: string): any {
|
||||
const args: Array<string> = request.split(" ");
|
||||
const args: Array<string> = GLib.shell_parse_argv(request)[1]!;
|
||||
|
||||
switch(args[0]) {
|
||||
case "open":
|
||||
case "close":
|
||||
case "toggle":
|
||||
case "windows":
|
||||
case "reopen":
|
||||
return handleWindowArgs(args);
|
||||
|
||||
case "help":
|
||||
@@ -29,48 +34,55 @@ export function handleArguments(request: string): any {
|
||||
|
||||
case "reload":
|
||||
restartInstance();
|
||||
return "Restarting instance..."
|
||||
|
||||
case "windows":
|
||||
return Object.keys(Windows.getDefault().windows).map(name =>
|
||||
`${name}: ${Windows.getDefault().isOpen(name) ? "open" : "closed" }`).join('\n');
|
||||
return `Restarting instance with name: ${App.instanceName ?? "astal"}`;
|
||||
|
||||
case "runner":
|
||||
!Runner.instance ?
|
||||
Runner.openDefault(args[1] || undefined)
|
||||
: Runner.close();
|
||||
return "Opening runner..."
|
||||
|
||||
return `Opening runner${args[1] ? ` with predefined text: "${args[1]}"` : ""}`;
|
||||
|
||||
case "peek-workspace-num":
|
||||
if(wsTimeout) return "Workspace numbers are already showing";
|
||||
if(wsTimeout)
|
||||
return "Workspace numbers are already showing";
|
||||
|
||||
showWorkspaceNumber(true);
|
||||
wsTimeout = timeout(2200, () => {
|
||||
wsTimeout = timeout(Number.parseInt(args[1]) || 2200, () => {
|
||||
showWorkspaceNumber(false);
|
||||
wsTimeout = undefined;
|
||||
});
|
||||
return "Toggled workspace numbers";
|
||||
|
||||
default:
|
||||
return "command not found! try checking help";
|
||||
return "Error: command not found! try checking help";
|
||||
}
|
||||
}
|
||||
|
||||
// Didn't want to bloat the switch statement, so I just separated it into functions
|
||||
function handleWindowArgs(args: Array<string>): string {
|
||||
if(!args[1])
|
||||
return "Window argument not specified!";
|
||||
switch(args[0]) {
|
||||
case "reopen":
|
||||
Windows.getDefault().reopen();
|
||||
return "Reopening all open windows";
|
||||
|
||||
case "windows":
|
||||
return Object.keys(Windows.getDefault().windows).map(name =>
|
||||
`${name}: ${Windows.getDefault().isOpen(name) ? "open" : "closed" }`).join('\n');
|
||||
}
|
||||
|
||||
const specifiedWindow: string = args[1];
|
||||
|
||||
if(!specifiedWindow)
|
||||
return "Error: window argument not specified!";
|
||||
|
||||
if(!Windows.getDefault().hasWindow(specifiedWindow))
|
||||
return `Name "${specifiedWindow}" not found windows map! Make sure to add new Windows on the Map!`
|
||||
return `Error: "${specifiedWindow}" not found on window list! Make sure to add new windows to the system before using them`;
|
||||
|
||||
switch(args[0]) {
|
||||
case "open":
|
||||
if(!Windows.getDefault().isOpen(specifiedWindow)) {
|
||||
Windows.getDefault().open(specifiedWindow);
|
||||
return `Setting visibility of window "${args[1]}" to true`;
|
||||
return `Opening window with name "${args[1]}"`;
|
||||
}
|
||||
|
||||
return `Window is already open, ignored`;
|
||||
@@ -78,10 +90,10 @@ function handleWindowArgs(args: Array<string>): string {
|
||||
case "close":
|
||||
if(Windows.getDefault().isOpen(specifiedWindow)) {
|
||||
Windows.getDefault().close(specifiedWindow);
|
||||
return `Setting visibility of window "${args[1]}" to false`
|
||||
return `Closing window with name "${args[1]}"`;
|
||||
}
|
||||
|
||||
return `Window is already closed, ignored`
|
||||
return `Window is already closed, ignored`;
|
||||
|
||||
case "toggle":
|
||||
if(!Windows.getDefault().isOpen(specifiedWindow)) {
|
||||
@@ -90,15 +102,15 @@ function handleWindowArgs(args: Array<string>): string {
|
||||
}
|
||||
|
||||
Windows.getDefault().close(specifiedWindow);
|
||||
return `Toggle closing window "${args[1]}"`
|
||||
return `Toggle closing window "${args[1]}"`;
|
||||
}
|
||||
|
||||
return "Couldn't handle window management arguments"
|
||||
return "Couldn't handle window management arguments";
|
||||
}
|
||||
|
||||
function handleVolumeArgs(args: Array<string>) {
|
||||
if(!args[1])
|
||||
return `Please specify what you want to do!\n\n${volumeHelp()}`
|
||||
return `Please specify what you want to do!\n\n${volumeHelp()}`;
|
||||
|
||||
if(/^(sink|source)(\-increase|\-decrease|\-set)$/.test(args[1]) && !args[2])
|
||||
return `You forgot to add a value to be set!`;
|
||||
@@ -108,7 +120,7 @@ function handleVolumeArgs(args: Array<string>) {
|
||||
|
||||
const command: Array<string> = args[1].split('-');
|
||||
|
||||
if(/help/.test(args[1]))
|
||||
if(args[1] === "help")
|
||||
return volumeHelp();
|
||||
|
||||
switch(command[1]) {
|
||||
@@ -150,7 +162,7 @@ function handleVolumeArgs(args: Array<string>) {
|
||||
|
||||
function volumeHelp(): string {
|
||||
return `
|
||||
Control speaker and microphone volumes easily!
|
||||
Control speaker and microphone volumes
|
||||
Options:
|
||||
(sink|source)-set [number]: set speaker/microphone volume.
|
||||
(sink|source)-mute: toggle mute for the speaker/microphone device.
|
||||
@@ -161,23 +173,26 @@ Options:
|
||||
}
|
||||
|
||||
function getHelp(): string {
|
||||
return `Manage Astal Windows and do more stuff. From
|
||||
retrozinndev's Hyprland Dots, using Astal and AGS by Aylur.
|
||||
return `Manage Astal Windows and do more stuff. From retrozinndev's colorshell,
|
||||
made using Astal Libraries, AGS and Gnim by Aylur.
|
||||
|
||||
Window and Audio options:
|
||||
Window Management:
|
||||
open [window]: opens the specified window.
|
||||
close [window]: closes all instances of specified window.
|
||||
toggle [window]: toggle-open/close the specified window.
|
||||
windows: list shell windows.
|
||||
windows: list shell windows and their respective status.
|
||||
reload: quit this instance and start a new one.
|
||||
reopen: restart all open-windows.
|
||||
|
||||
Audio Controls:
|
||||
volume: speaker and microphone volume controller, see "volume help".
|
||||
h, help: shows this help message.
|
||||
|
||||
Other options:
|
||||
runner [initial_text]: open the application runner, optionally add an initial search.
|
||||
peek-workspace-num: peek the workspace numbers on bar window.
|
||||
peek-workspace-num [millis]: peek the workspace numbers on bar window.
|
||||
h, help: shows this help message.
|
||||
|
||||
2025 (c) retrozinndev's Hyprland-Dots, licensed under the MIT License.
|
||||
https://github.com/retrozinndev/Hyprland-Dots
|
||||
2025 (c) retrozinndev's colorshell, licensed under the MIT License.
|
||||
https://github.com/retrozinndev/colorshell
|
||||
`.split('\n').map(l => l.replace(/^ {8}/, "")).join('\n');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user