fix: finally a working screenshot script

This commit is contained in:
2025-11-26 15:22:47 +01:00
parent 1c39877f14
commit 920b4451d9
5 changed files with 28 additions and 22 deletions

View File

@@ -169,7 +169,7 @@ bind = Super+Shift, Page_Up, movetoworkspace, -1 # [hidden]
bind = Super+Alt, S, movetoworkspacesilent, special:s bind = Super+Alt, S, movetoworkspacesilent, special:s
bind = Super, P, pin bind = Super, P, pin
bind = Alt, Tab, cyclenext bind = Alt, Tab, cyclenext
bind = Super, Tab, hyprexpo:expo, toggle # can be: toggle, select, off/disable or on/enable # bind = Super, Tab, hyprexpo:expo, toggle # can be: toggle, select, off/disable or on/enable
bind = Super+Ctrl, T, exec, workspace-new # Create new workspace bind = Super+Ctrl, T, exec, workspace-new # Create new workspace
bind = Super, M, exit bind = Super, M, exit

View File

@@ -296,6 +296,7 @@ debug {
render-drm-device "/dev/dri/card0" render-drm-device "/dev/dri/card0"
} }
// screenshot-path "~/Pictures/Screenshots/niri_screenshot_%Y-%m-%d_%H-%M-%S.png"
screenshot-path "~/Pictures/Screenshots/.niri_screenshot.png" screenshot-path "~/Pictures/Screenshots/.niri_screenshot.png"
// gestures { // gestures {

View File

@@ -6,7 +6,7 @@
# #
# Requirements: # Requirements:
# - hyprshot (for Hyprland) # - hyprshot (for Hyprland)
# - - (niri has screenshot functionality built-in) # - grim + slurp (for Niri)
# - gradia (for editing) # - gradia (for editing)
# - glib bindings for python # - glib bindings for python
@@ -48,11 +48,11 @@ def wait_until_file_exists(filepath: Path, timeout: int = 1):
def take_screenshot(filepath: Path, typeStr: str): def take_screenshot(filepath: Path, typeStr: str):
lock_fd = open("/tmp/screenshot-script.lock", "w") lockFD = open("/tmp/screenshot-script.lock", "w")
try: try:
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.flock(lockFD, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError: except IOError:
lock_fd.close() lockFD.close()
raise RuntimeError("Another screenshot is currently being taken.") raise RuntimeError("Another screenshot is currently being taken.")
try: try:
@@ -70,19 +70,23 @@ def take_screenshot(filepath: Path, typeStr: str):
raise RuntimeError("Failed to take screenshot: hyprshot command failed.") raise RuntimeError("Failed to take screenshot: hyprshot command failed.")
if not wait_until_file_exists(filepath): if not wait_until_file_exists(filepath):
raise RuntimeError("Failed to take screenshot: output file not found after hyprshot command.") raise RuntimeError("Failed to take screenshot: output file not found after hyprshot command.")
elif "niri" in currentDesktop: elif "niri" in currentDesktop:
niriScreenshotPath = SCREENSHOT_DIR / ".niri_screenshot.png"
cmd = { cmd = {
# niri's built-in screenshot commands are asynchronous, which does not wait for the user to select the area.
# and the selection ui is drawn inside of niri without its state exposed to external programs.
# so we use grim + slurp for area mode and niri's built-in commands for others.
ScreenshotType.FULL: "niri msg action screenshot-screen", ScreenshotType.FULL: "niri msg action screenshot-screen",
ScreenshotType.AREA: "niri msg action screenshot", ScreenshotType.AREA: f" grim -g \"$(slurp)\" -t png {niriScreenshotPath}",
ScreenshotType.WINDOW: "niri msg action screenshot-window", ScreenshotType.WINDOW: "niri msg action screenshot-window",
} }
niriScreenshotPath = SCREENSHOT_DIR / ".niri_screenshot.png"
if niriScreenshotPath.exists(): if niriScreenshotPath.exists():
niriScreenshotPath.unlink() niriScreenshotPath.unlink()
# if os.system(cmd[type]):
process = subprocess.run(cmd[type], shell=True) process = subprocess.run(cmd[type], shell=True)
if process.returncode != 0: if process.returncode != 0:
raise RuntimeError("Failed to take screenshot: niri built-in screenshot command failed.") print(process.returncode)
raise RuntimeError("Failed to take screenshot: niri screenshot command failed.")
if wait_until_file_exists(niriScreenshotPath): if wait_until_file_exists(niriScreenshotPath):
# niriScreenshotPath.rename(filepath) # niriScreenshotPath.rename(filepath)
@@ -91,12 +95,13 @@ def take_screenshot(filepath: Path, typeStr: str):
raise RuntimeError("Failed to take screenshot: output file not found after niri command.") raise RuntimeError("Failed to take screenshot: output file not found after niri command.")
if not wait_until_file_exists(filepath): if not wait_until_file_exists(filepath):
raise RuntimeError("Failed to take screenshot: output file not found after copying.") raise RuntimeError("Failed to take screenshot: output file not found after copying.")
else: else:
# print("Unsupported desktop environment.") # print("Unsupported desktop environment.")
raise RuntimeError("Unsupported desktop environment.") raise RuntimeError("Unsupported desktop environment.")
finally: finally:
fcntl.flock(lock_fd, fcntl.LOCK_UN) fcntl.flock(lockFD, fcntl.LOCK_UN)
lock_fd.close() lockFD.close()
def edit_screenshot(filepath: Path): def edit_screenshot(filepath: Path):
@@ -130,10 +135,6 @@ if __name__ == "__main__":
# take screenshot # take screenshot
take_screenshot(filepath, args.type) take_screenshot(filepath, args.type)
# check if successful
if not filepath.exists():
raise RuntimeError("Failed to take screenshot: screenshot file not found.")
# create loop instance # create loop instance
loop = GLib.MainLoop() loop = GLib.MainLoop()
editing = False editing = False

View File

@@ -100,4 +100,8 @@ if type -q git
end end
git push git push
end end
if type -q wl-paste
alias gc="git clone \$(wl-paste)"
end
end end

View File

@@ -1,10 +1,10 @@
require("yaziline"):setup({ -- require("yaziline"):setup({
separator_style = "angly", -- separator_style = "angly",
select_symbol = "", -- select_symbol = "",
yank_symbol = "󰆐", -- yank_symbol = "󰆐",
filename_max_length = 24, -- trim when filename > 24 -- filename_max_length = 24, -- trim when filename > 24
filename_trim_length = 6, -- trim 6 chars from both ends -- filename_trim_length = 6, -- trim 6 chars from both ends
}) -- })
-- require("starship"):setup { -- require("starship"):setup {
-- config_file = "~/.config/yazi/starship.toml", -- config_file = "~/.config/yazi/starship.toml",
-- } -- }