🔧 chore(arg-handler): more organized and supporting more arguments
This commit is contained in:
+49
-34
@@ -4,20 +4,25 @@ import { restartInstance } from "./reload-handler";
|
|||||||
import { timeout } from "ags/time";
|
import { timeout } from "ags/time";
|
||||||
import { Runner } from "../runner/Runner";
|
import { Runner } from "../runner/Runner";
|
||||||
import { showWorkspaceNumber } from "../widget/bar/Workspaces";
|
import { showWorkspaceNumber } from "../widget/bar/Workspaces";
|
||||||
|
|
||||||
import AstalIO from "gi://AstalIO";
|
|
||||||
import { playSystemBell } from "./utils";
|
import { playSystemBell } from "./utils";
|
||||||
import { Config } from "./config";
|
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);
|
let wsTimeout: (AstalIO.Time|undefined);
|
||||||
|
|
||||||
export function handleArguments(request: string): any {
|
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]) {
|
switch(args[0]) {
|
||||||
case "open":
|
case "open":
|
||||||
case "close":
|
case "close":
|
||||||
case "toggle":
|
case "toggle":
|
||||||
|
case "windows":
|
||||||
|
case "reopen":
|
||||||
return handleWindowArgs(args);
|
return handleWindowArgs(args);
|
||||||
|
|
||||||
case "help":
|
case "help":
|
||||||
@@ -29,48 +34,55 @@ export function handleArguments(request: string): any {
|
|||||||
|
|
||||||
case "reload":
|
case "reload":
|
||||||
restartInstance();
|
restartInstance();
|
||||||
return "Restarting instance..."
|
return `Restarting instance with name: ${App.instanceName ?? "astal"}`;
|
||||||
|
|
||||||
case "windows":
|
|
||||||
return Object.keys(Windows.getDefault().windows).map(name =>
|
|
||||||
`${name}: ${Windows.getDefault().isOpen(name) ? "open" : "closed" }`).join('\n');
|
|
||||||
|
|
||||||
case "runner":
|
case "runner":
|
||||||
!Runner.instance ?
|
!Runner.instance ?
|
||||||
Runner.openDefault(args[1] || undefined)
|
Runner.openDefault(args[1] || undefined)
|
||||||
: Runner.close();
|
: Runner.close();
|
||||||
return "Opening runner..."
|
|
||||||
|
return `Opening runner${args[1] ? ` with predefined text: "${args[1]}"` : ""}`;
|
||||||
|
|
||||||
case "peek-workspace-num":
|
case "peek-workspace-num":
|
||||||
if(wsTimeout) return "Workspace numbers are already showing";
|
if(wsTimeout)
|
||||||
|
return "Workspace numbers are already showing";
|
||||||
|
|
||||||
showWorkspaceNumber(true);
|
showWorkspaceNumber(true);
|
||||||
wsTimeout = timeout(2200, () => {
|
wsTimeout = timeout(Number.parseInt(args[1]) || 2200, () => {
|
||||||
showWorkspaceNumber(false);
|
showWorkspaceNumber(false);
|
||||||
wsTimeout = undefined;
|
wsTimeout = undefined;
|
||||||
});
|
});
|
||||||
return "Toggled workspace numbers";
|
return "Toggled workspace numbers";
|
||||||
|
|
||||||
default:
|
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 {
|
function handleWindowArgs(args: Array<string>): string {
|
||||||
if(!args[1])
|
switch(args[0]) {
|
||||||
return "Window argument not specified!";
|
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];
|
const specifiedWindow: string = args[1];
|
||||||
|
|
||||||
|
if(!specifiedWindow)
|
||||||
|
return "Error: window argument not specified!";
|
||||||
|
|
||||||
if(!Windows.getDefault().hasWindow(specifiedWindow))
|
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]) {
|
switch(args[0]) {
|
||||||
case "open":
|
case "open":
|
||||||
if(!Windows.getDefault().isOpen(specifiedWindow)) {
|
if(!Windows.getDefault().isOpen(specifiedWindow)) {
|
||||||
Windows.getDefault().open(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`;
|
return `Window is already open, ignored`;
|
||||||
@@ -78,10 +90,10 @@ function handleWindowArgs(args: Array<string>): string {
|
|||||||
case "close":
|
case "close":
|
||||||
if(Windows.getDefault().isOpen(specifiedWindow)) {
|
if(Windows.getDefault().isOpen(specifiedWindow)) {
|
||||||
Windows.getDefault().close(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":
|
case "toggle":
|
||||||
if(!Windows.getDefault().isOpen(specifiedWindow)) {
|
if(!Windows.getDefault().isOpen(specifiedWindow)) {
|
||||||
@@ -90,15 +102,15 @@ function handleWindowArgs(args: Array<string>): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Windows.getDefault().close(specifiedWindow);
|
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>) {
|
function handleVolumeArgs(args: Array<string>) {
|
||||||
if(!args[1])
|
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])
|
if(/^(sink|source)(\-increase|\-decrease|\-set)$/.test(args[1]) && !args[2])
|
||||||
return `You forgot to add a value to be set!`;
|
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('-');
|
const command: Array<string> = args[1].split('-');
|
||||||
|
|
||||||
if(/help/.test(args[1]))
|
if(args[1] === "help")
|
||||||
return volumeHelp();
|
return volumeHelp();
|
||||||
|
|
||||||
switch(command[1]) {
|
switch(command[1]) {
|
||||||
@@ -150,7 +162,7 @@ function handleVolumeArgs(args: Array<string>) {
|
|||||||
|
|
||||||
function volumeHelp(): string {
|
function volumeHelp(): string {
|
||||||
return `
|
return `
|
||||||
Control speaker and microphone volumes easily!
|
Control speaker and microphone volumes
|
||||||
Options:
|
Options:
|
||||||
(sink|source)-set [number]: set speaker/microphone volume.
|
(sink|source)-set [number]: set speaker/microphone volume.
|
||||||
(sink|source)-mute: toggle mute for the speaker/microphone device.
|
(sink|source)-mute: toggle mute for the speaker/microphone device.
|
||||||
@@ -161,23 +173,26 @@ Options:
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getHelp(): string {
|
function getHelp(): string {
|
||||||
return `Manage Astal Windows and do more stuff. From
|
return `Manage Astal Windows and do more stuff. From retrozinndev's colorshell,
|
||||||
retrozinndev's Hyprland Dots, using Astal and AGS by Aylur.
|
made using Astal Libraries, AGS and Gnim by Aylur.
|
||||||
|
|
||||||
Window and Audio options:
|
Window Management:
|
||||||
open [window]: opens the specified window.
|
open [window]: opens the specified window.
|
||||||
close [window]: closes all instances of specified window.
|
close [window]: closes all instances of specified window.
|
||||||
toggle [window]: toggle-open/close the 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.
|
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".
|
volume: speaker and microphone volume controller, see "volume help".
|
||||||
h, help: shows this help message.
|
|
||||||
|
|
||||||
Other options:
|
Other options:
|
||||||
runner [initial_text]: open the application runner, optionally add an initial search.
|
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.
|
2025 (c) retrozinndev's colorshell, licensed under the MIT License.
|
||||||
https://github.com/retrozinndev/Hyprland-Dots
|
https://github.com/retrozinndev/colorshell
|
||||||
`.split('\n').map(l => l.replace(/^ {8}/, "")).join('\n');
|
`.split('\n').map(l => l.replace(/^ {8}/, "")).join('\n');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user