From 52dfdb97ecad6c1a9bab3dfdb9996557e77a0a1e Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Mon, 9 Jun 2025 22:15:03 +1000 Subject: feat: add shell commands Also switch to classes --- src/data.py | 2 +- src/main.py | 2 +- src/parser.py | 31 +++++++++++++++++++------------ src/subcommands/clipboard.py | 10 ++++++++-- src/subcommands/emoji.py | 10 ++++++++-- src/subcommands/pip.py | 10 ++++++++-- src/subcommands/record.py | 10 ++++++++-- src/subcommands/scheme.py | 10 ++++++++-- src/subcommands/screenshot.py | 10 ++++++++-- src/subcommands/shell.py | 40 ++++++++++++++++++++++++++++++++++++++-- src/subcommands/toggle.py | 10 ++++++++-- src/subcommands/variant.py | 10 ++++++++-- src/subcommands/wallpaper.py | 10 ++++++++-- src/subcommands/wsaction.py | 10 ++++++++-- 14 files changed, 139 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/data.py b/src/data.py index f5fbe6e..caa78f1 100644 --- a/src/data.py +++ b/src/data.py @@ -96,7 +96,7 @@ def get_scheme_colours() -> dict[str, str]: if scheme_colours is None: scheme_colours = { - k.strip(): v.strip() for k, v in (line.split(" ") for line in get_scheme_path().read_text().split("\n")) + k.strip(): v.strip() for k, v in (line.split(" ") for line in get_scheme_path().read_text().splitlines()) } return scheme_colours diff --git a/src/main.py b/src/main.py index a01222d..e1f543f 100644 --- a/src/main.py +++ b/src/main.py @@ -2,4 +2,4 @@ from parser import parse_args if __name__ == "__main__": args = parse_args() - args.func(args) + args.cls(args).run() diff --git a/src/parser.py b/src/parser.py index 724992b..49695ee 100644 --- a/src/parser.py +++ b/src/parser.py @@ -14,14 +14,21 @@ def parse_args() -> argparse.Namespace: # Create parser for shell opts shell_parser = command_parser.add_parser("shell", help="start or message the shell") - shell_parser.set_defaults(func=shell.run) + shell_parser.set_defaults(cls=shell.Command) shell_parser.add_argument("message", nargs="*", help="a message to send to the shell") shell_parser.add_argument("-s", "--show", action="store_true", help="print all shell IPC commands") - shell_parser.add_argument("-l", "--log", action="store_true", help="print the shell log") + shell_parser.add_argument( + "-l", + "--log", + nargs="?", + const="quickshell.dbus.properties.warning=false;quickshell.dbus.dbusmenu.warning=false;quickshell.service.notifications.warning=false;quickshell.service.sni.host.warning=false", + metavar="RULES", + help="print the shell log", + ) # Create parser for toggle opts toggle_parser = command_parser.add_parser("toggle", help="toggle a special workspace") - toggle_parser.set_defaults(func=toggle.run) + toggle_parser.set_defaults(cls=toggle.Command) toggle_parser.add_argument( "workspace", choices=["communication", "music", "sysmon", "specialws", "todo"], help="the workspace to toggle" ) @@ -30,7 +37,7 @@ def parse_args() -> argparse.Namespace: ws_action_parser = command_parser.add_parser( "workspace-action", help="execute a Hyprland workspace dispatcher in the current group" ) - ws_action_parser.set_defaults(func=wsaction.run) + ws_action_parser.set_defaults(cls=wsaction.Command) ws_action_parser.add_argument( "-g", "--group", action="store_true", help="whether to execute the dispatcher on a group" ) @@ -41,7 +48,7 @@ def parse_args() -> argparse.Namespace: # Create parser for scheme opts scheme_parser = command_parser.add_parser("scheme", help="manage the colour scheme") - scheme_parser.set_defaults(func=scheme.run) + scheme_parser.set_defaults(cls=scheme.Command) scheme_parser.add_argument("-g", "--get", action="store_true", help="print the current scheme") scheme_parser.add_argument("-r", "--random", action="store_true", help="switch to a random scheme") scheme_parser.add_argument("-n", "--name", choices=get_scheme_names(), help="the name of the scheme to switch to") @@ -50,14 +57,14 @@ def parse_args() -> argparse.Namespace: # Create parser for variant opts variant_parser = command_parser.add_parser("variant", help="manage the dynamic scheme variant") - variant_parser.set_defaults(func=variant.run) + variant_parser.set_defaults(cls=variant.Command) variant_parser.add_argument("-g", "--get", action="store_true", help="print the current dynamic scheme variant") variant_parser.add_argument("-s", "--set", choices=scheme_variants, help="set the current dynamic scheme variant") variant_parser.add_argument("-r", "--random", action="store_true", help="switch to a random variant") # Create parser for screenshot opts screenshot_parser = command_parser.add_parser("screenshot", help="take a screenshot") - screenshot_parser.set_defaults(func=screenshot.run) + screenshot_parser.set_defaults(cls=screenshot.Command) screenshot_parser.add_argument("-r", "--region", help="take a screenshot of a region") screenshot_parser.add_argument( "-f", "--freeze", action="store_true", help="freeze the screen while selecting a region" @@ -65,22 +72,22 @@ def parse_args() -> argparse.Namespace: # Create parser for record opts record_parser = command_parser.add_parser("record", help="start a screen recording") - record_parser.set_defaults(func=record.run) + record_parser.set_defaults(cls=record.Command) record_parser.add_argument("-r", "--region", action="store_true", help="record a region") record_parser.add_argument("-s", "--sound", action="store_true", help="record audio") # Create parser for clipboard opts clipboard_parser = command_parser.add_parser("clipboard", help="open clipboard history") - clipboard_parser.set_defaults(func=clipboard.run) + clipboard_parser.set_defaults(cls=clipboard.Command) clipboard_parser.add_argument("-d", "--delete", action="store_true", help="delete from clipboard history") # Create parser for emoji-picker opts emoji_parser = command_parser.add_parser("emoji-picker", help="toggle the emoji picker") - emoji_parser.set_defaults(func=emoji.run) + emoji_parser.set_defaults(cls=emoji.Command) # Create parser for wallpaper opts wallpaper_parser = command_parser.add_parser("wallpaper", help="manage the wallpaper") - wallpaper_parser.set_defaults(func=wallpaper.run) + wallpaper_parser.set_defaults(cls=wallpaper.Command) wallpaper_parser.add_argument("-g", "--get", action="store_true", help="print the current wallpaper") wallpaper_parser.add_argument("-r", "--random", action="store_true", help="switch to a random wallpaper") wallpaper_parser.add_argument("-f", "--file", help="the path to the wallpaper to switch to") @@ -100,7 +107,7 @@ def parse_args() -> argparse.Namespace: # Create parser for pip opts pip_parser = command_parser.add_parser("pip", help="picture in picture utilities") - pip_parser.set_defaults(func=pip.run) + pip_parser.set_defaults(cls=pip.Command) pip_parser.add_argument("-d", "--daemon", action="store_true", help="start the daemon") return parser.parse_args() diff --git a/src/subcommands/clipboard.py b/src/subcommands/clipboard.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/clipboard.py +++ b/src/subcommands/clipboard.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/emoji.py b/src/subcommands/emoji.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/emoji.py +++ b/src/subcommands/emoji.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/pip.py b/src/subcommands/pip.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/pip.py +++ b/src/subcommands/pip.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/record.py b/src/subcommands/record.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/record.py +++ b/src/subcommands/record.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/scheme.py b/src/subcommands/scheme.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/scheme.py +++ b/src/subcommands/scheme.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/screenshot.py b/src/subcommands/screenshot.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/screenshot.py +++ b/src/subcommands/screenshot.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/shell.py b/src/subcommands/shell.py index a94f3d0..6802dc8 100644 --- a/src/subcommands/shell.py +++ b/src/subcommands/shell.py @@ -1,5 +1,41 @@ +import subprocess from argparse import Namespace +import data -def run(args: Namespace) -> None: - pass + +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + if self.args.show: + # Print the ipc + self.print_ipc() + elif self.args.log: + # Print the log + self.print_log() + elif self.args.message: + # Send a message + self.message(*self.args.message) + else: + # Start the shell + self.shell() + + def shell(self, *args: list[str]) -> str: + return subprocess.check_output(["qs", "-p", data.c_data_dir / "shell", *args], text=True) + + def print_ipc(self) -> None: + print(self.shell("ipc", "show"), end="") + + def print_log(self) -> None: + log = self.shell("log") + # FIXME: remove when logging rules are added/warning is removed + for line in log.splitlines(): + if "QProcess: Destroyed while process" not in line: + print(line) + + def message(self, *args: list[str]) -> None: + print(self.shell("ipc", "call", *args), end="") diff --git a/src/subcommands/toggle.py b/src/subcommands/toggle.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/toggle.py +++ b/src/subcommands/toggle.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/variant.py b/src/subcommands/variant.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/variant.py +++ b/src/subcommands/variant.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/wallpaper.py b/src/subcommands/wallpaper.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/wallpaper.py +++ b/src/subcommands/wallpaper.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass diff --git a/src/subcommands/wsaction.py b/src/subcommands/wsaction.py index a94f3d0..37f9a2b 100644 --- a/src/subcommands/wsaction.py +++ b/src/subcommands/wsaction.py @@ -1,5 +1,11 @@ from argparse import Namespace -def run(args: Namespace) -> None: - pass +class Command: + args: Namespace + + def __init__(self, args: Namespace) -> None: + self.args = args + + def run(self) -> None: + pass -- cgit v1.2.3-freya