scheme: add notify opt

For sending a notification on error
This commit is contained in:
2 * r + 2 * t
2025-06-17 12:49:16 +10:00
parent f0c892749d
commit 2c94c42cbd
7 changed files with 52 additions and 42 deletions
+1
View File
@@ -94,6 +94,7 @@ complete -c caelestia -n "$seen scheme && $seen get" -s 'f' -l 'flavour' -d 'Get
complete -c caelestia -n "$seen scheme && $seen get" -s 'm' -l 'mode' -d 'Get scheme mode' complete -c caelestia -n "$seen scheme && $seen get" -s 'm' -l 'mode' -d 'Get scheme mode'
complete -c caelestia -n "$seen scheme && $seen get" -s 'v' -l 'variant' -d 'Get scheme variant' complete -c caelestia -n "$seen scheme && $seen get" -s 'v' -l 'variant' -d 'Get scheme variant'
complete -c caelestia -n "$seen scheme && $seen set" -l 'notify' -d 'Send a notification on error'
complete -c caelestia -n "$seen scheme && $seen set" -s 'r' -l 'random' -d 'Switch to a random scheme' complete -c caelestia -n "$seen scheme && $seen set" -s 'r' -l 'random' -d 'Switch to a random scheme'
complete -c caelestia -n "$seen scheme && $seen set" -s 'n' -l 'name' -d 'Set scheme name' -a "$(caelestia scheme list -n)" -r complete -c caelestia -n "$seen scheme && $seen set" -s 'n' -l 'name' -d 'Set scheme name' -a "$(caelestia scheme list -n)" -r
complete -c caelestia -n "$seen scheme && $seen set" -s 'f' -l 'flavour' -d 'Set scheme flavour' -a "$(caelestia scheme list -f)" -r complete -c caelestia -n "$seen scheme && $seen set" -s 'f' -l 'flavour' -d 'Set scheme flavour' -a "$(caelestia scheme list -f)" -r
+1
View File
@@ -68,6 +68,7 @@ def parse_args() -> (argparse.ArgumentParser, argparse.Namespace):
set_parser = scheme_command_parser.add_parser("set", help="set the current scheme") set_parser = scheme_command_parser.add_parser("set", help="set the current scheme")
set_parser.set_defaults(cls=scheme.Set) set_parser.set_defaults(cls=scheme.Set)
set_parser.add_argument("--notify", action="store_true", help="send a notification on error")
set_parser.add_argument("-r", "--random", action="store_true", help="switch to a random scheme") set_parser.add_argument("-r", "--random", action="store_true", help="switch to a random scheme")
set_parser.add_argument("-n", "--name", choices=get_scheme_names(), help="the name of the scheme to switch to") set_parser.add_argument("-n", "--name", choices=get_scheme_names(), help="the name of the scheme to switch to")
set_parser.add_argument("-f", "--flavour", help="the flavour to switch to") set_parser.add_argument("-f", "--flavour", help="the flavour to switch to")
+10 -25
View File
@@ -3,6 +3,7 @@ import time
from argparse import Namespace from argparse import Namespace
from datetime import datetime from datetime import datetime
from caelestia.utils.notify import notify
from caelestia.utils.paths import recording_notif_path, recording_path, recordings_dir from caelestia.utils.paths import recording_notif_path, recording_path, recordings_dir
@@ -48,20 +49,10 @@ class Command:
# Send notif if proc hasn't ended after a small delay # Send notif if proc hasn't ended after a small delay
time.sleep(0.1) time.sleep(0.1)
if proc.poll() is None: if proc.poll() is None:
notif = subprocess.check_output( notif = notify("-p", "Recording started", "Recording...")
["notify-send", "-p", "-a", "caelestia-cli", "Recording started", "Recording..."], text=True
).strip()
recording_notif_path.write_text(notif) recording_notif_path.write_text(notif)
else: else:
subprocess.run( notify("Recording failed", f"Recording failed to start: {proc.communicate()[1]}")
[
"notify-send",
"-a",
"caelestia-cli",
"Recording failed",
f"Recording failed to start: {proc.communicate()[1]}",
]
)
def stop(self) -> None: def stop(self) -> None:
subprocess.run(["pkill", "wl-screenrec"]) subprocess.run(["pkill", "wl-screenrec"])
@@ -87,19 +78,13 @@ class Command:
except IOError: except IOError:
pass pass
action = subprocess.check_output( action = notify(
[ "--action=watch=Watch",
"notify-send", "--action=open=Open",
"-a", "--action=delete=Delete",
"caelestia-cli", "Recording stopped",
"--action=watch=Watch", f"Recording saved in {new_path}",
"--action=open=Open", )
"--action=delete=Delete",
"Recording stopped",
f"Recording saved in {new_path}",
],
text=True,
).strip()
if action == "watch": if action == "watch":
subprocess.Popen(["app2unit", "-O", new_path], start_new_session=True) subprocess.Popen(["app2unit", "-O", new_path], start_new_session=True)
+3
View File
@@ -13,6 +13,9 @@ class Set:
def run(self) -> None: def run(self) -> None:
scheme = get_scheme() scheme = get_scheme()
if self.args.notify:
scheme.notify = True
if self.args.random: if self.args.random:
scheme.set_random() scheme.set_random()
apply_colours(scheme.colours, scheme.mode) apply_colours(scheme.colours, scheme.mode)
+12 -17
View File
@@ -4,6 +4,7 @@ from argparse import Namespace
from datetime import datetime from datetime import datetime
from caelestia.utils import hypr from caelestia.utils import hypr
from caelestia.utils.notify import notify
from caelestia.utils.paths import screenshots_cache_dir, screenshots_dir from caelestia.utils.paths import screenshots_cache_dir, screenshots_dir
@@ -59,22 +60,16 @@ class Command:
screenshots_cache_dir.mkdir(exist_ok=True, parents=True) screenshots_cache_dir.mkdir(exist_ok=True, parents=True)
dest.write_bytes(sc_data) dest.write_bytes(sc_data)
action = subprocess.check_output( action = notify(
[ "-i",
"notify-send", "image-x-generic-symbolic",
"-i", "-h",
"image-x-generic-symbolic", f"STRING:image-path:{dest}",
"-h", "--action=open=Open",
f"STRING:image-path:{dest}", "--action=save=Save",
"-a", "Screenshot taken",
"caelestia-cli", f"Screenshot stored in {dest} and copied to clipboard",
"--action=open=Open", )
"--action=save=Save",
"Screenshot taken",
f"Screenshot stored in {dest} and copied to clipboard",
],
text=True,
).strip()
if action == "open": if action == "open":
subprocess.Popen(["swappy", "-f", dest], start_new_session=True) subprocess.Popen(["swappy", "-f", dest], start_new_session=True)
@@ -82,4 +77,4 @@ class Command:
new_dest = (screenshots_dir / dest.name).with_suffix(".png") new_dest = (screenshots_dir / dest.name).with_suffix(".png")
new_dest.parent.mkdir(exist_ok=True, parents=True) new_dest.parent.mkdir(exist_ok=True, parents=True)
dest.rename(new_dest) dest.rename(new_dest)
subprocess.run(["notify-send", "Screenshot saved", f"Saved to {new_dest}"]) notify("Screenshot saved", f"Saved to {new_dest}")
+5
View File
@@ -0,0 +1,5 @@
import subprocess
def notify(*args: list[str]) -> str:
return subprocess.check_output(["notify-send", "-a", "caelestia-cli", *args], text=True).strip()
+20
View File
@@ -3,6 +3,7 @@ import random
from pathlib import Path from pathlib import Path
from caelestia.utils.material import get_colours_for_image from caelestia.utils.material import get_colours_for_image
from caelestia.utils.notify import notify
from caelestia.utils.paths import atomic_dump, scheme_data_dir, scheme_path from caelestia.utils.paths import atomic_dump, scheme_data_dir, scheme_path
@@ -12,6 +13,7 @@ class Scheme:
_mode: str _mode: str
_variant: str _variant: str
_colours: dict[str, str] _colours: dict[str, str]
notify: bool
def __init__(self, json: dict[str, any] | None) -> None: def __init__(self, json: dict[str, any] | None) -> None:
if json is None: if json is None:
@@ -26,6 +28,7 @@ class Scheme:
self._mode = json["mode"] self._mode = json["mode"]
self._variant = json["variant"] self._variant = json["variant"]
self._colours = json["colours"] self._colours = json["colours"]
self.notify = False
@property @property
def name(self) -> str: def name(self) -> str:
@@ -37,6 +40,11 @@ class Scheme:
return return
if name not in get_scheme_names(): if name not in get_scheme_names():
if self.notify:
notify(
"Unable to set scheme",
f'"{name}" is not a valid scheme.\nValid schemes are: {get_scheme_names()}',
)
raise ValueError(f"Invalid scheme name: {name}") raise ValueError(f"Invalid scheme name: {name}")
self._name = name self._name = name
@@ -55,6 +63,12 @@ class Scheme:
return return
if flavour not in get_scheme_flavours(): if flavour not in get_scheme_flavours():
if self.notify:
notify(
"Unable to set scheme flavour",
f'"{flavour}" is not a valid flavour of scheme "{self.name}".\n'
f"Valid flavours are: {get_scheme_flavours()}",
)
raise ValueError(f'Invalid scheme flavour: "{flavour}". Valid flavours: {get_scheme_flavours()}') raise ValueError(f'Invalid scheme flavour: "{flavour}". Valid flavours: {get_scheme_flavours()}')
self._flavour = flavour self._flavour = flavour
@@ -71,6 +85,12 @@ class Scheme:
return return
if mode not in get_scheme_modes(): if mode not in get_scheme_modes():
if self.notify:
notify(
"Unable to set scheme mode",
f'"{mode}" is not a valid mode of scheme "{self.name} {self.flavour}".\n'
f"Valid modes are: {get_scheme_modes()}",
)
raise ValueError(f'Invalid scheme mode: "{mode}". Valid modes: {get_scheme_modes()}') raise ValueError(f'Invalid scheme mode: "{mode}". Valid modes: {get_scheme_modes()}')
self._mode = mode self._mode = mode