Files
caelestia-cli/src/caelestia/subcommands/screenshot.py
T
Kalagmitan b00c601d0a refactor: enforce stricter type hints (#91)
LSP was screaming at me so I decided to just address it to get it off my
screen.

+ Fixed the type hints
:= Modified and added type hints for certain functions and variables in
most of the files in the utils/ folder (and some in the subcommands/
folder) for clarity and so pyright's type checker wouldn't cry.
:+ To resolve certain type issues, I had to add a bit more tiny
additional code such as, additional checks if a variable is None, a tiny
class in utils/material/generator.py to resolve the constructor usage
mismatch between what the DynamicScheme accepts and what the code
actually passes, and etc.
- Renamed certain functions and variables for clarity and also for some
to not collide with pre-existing definitions from well-known library
imports.
+ PIL has reorganized their code a bit, so the code is made to reflect
their new definitions.
= Reorganized the single import statement for "colourfulness" in
utils/wallpaper.py to be close to the top.
(I think that's it)

Side Effects?:
Everything should work the same as no logic change was done whatsover
(unless we consider the added if statements for type checking as a logic
change). I've tested it, everything seems to be in urdir.
2026-03-15 22:56:05 +11:00

62 lines
2.0 KiB
Python

import subprocess
from argparse import Namespace
from datetime import datetime
from caelestia.utils.notify import notify
from caelestia.utils.paths import screenshots_cache_dir, screenshots_dir
class Command:
args: Namespace
def __init__(self, args: Namespace) -> None:
self.args = args
def run(self) -> None:
if self.args.region:
self.region()
else:
self.fullscreen()
def region(self) -> None:
if self.args.region == "slurp":
subprocess.run(
["qs", "-c", "caelestia", "ipc", "call", "picker", "openFreeze" if self.args.freeze else "open"]
)
else:
sc_data = subprocess.check_output(["grim", "-l", "0", "-g", self.args.region.strip(), "-"])
swappy = subprocess.Popen(["swappy", "-f", "-"], stdin=subprocess.PIPE, start_new_session=True)
# Ensure stdin is not None for the type checker
if swappy.stdin:
swappy.stdin.write(sc_data)
swappy.stdin.close()
def fullscreen(self) -> None:
sc_data = subprocess.check_output(["grim", "-"])
subprocess.run(["wl-copy"], input=sc_data)
dest = screenshots_cache_dir / datetime.now().strftime("%Y%m%d%H%M%S")
screenshots_cache_dir.mkdir(exist_ok=True, parents=True)
dest.write_bytes(sc_data)
action = notify(
"-i",
"image-x-generic-symbolic",
"-h",
f"STRING:image-path:{dest}",
"--action=open=Open",
"--action=save=Save",
"Screenshot taken",
f"Screenshot stored in {dest} and copied to clipboard",
)
if action == "open":
subprocess.Popen(["swappy", "-f", dest], start_new_session=True)
elif action == "save":
new_dest = (screenshots_dir / dest.name).with_suffix(".png")
new_dest.parent.mkdir(exist_ok=True, parents=True)
dest.rename(new_dest)
notify("Screenshot saved", f"Saved to {new_dest}")