makes no sense
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
<img src="https://git.uyani.de/Uyanide/dotfiles/raw/branch/main/assets/works-on-my-machines.png" alt="Works on my machine(s)" width="200" />
|
<img src="https://raw.githubusercontent.com/Uyanide/dotfiles/refs/heads/main/assets/works-on-my-machines.png" alt="Works on my machine(s)" width="200" />
|
||||||
|
|
||||||
## How it looks like...
|
## How it looks like...
|
||||||
|
|
||||||
|
|||||||
@@ -7,5 +7,7 @@ for child in $(pgrep -P "$pid" 2>/dev/null); do
|
|||||||
kill "$child"
|
kill "$child"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
sleep 0.3
|
||||||
|
|
||||||
kill "$pid"
|
kill "$pid"
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
import fcntl
|
||||||
from os import environ
|
from os import environ
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
@@ -36,7 +37,7 @@ class ScreenshotType(Enum):
|
|||||||
SCREENSHOT_DIR = Path.home() / "Pictures" / "Screenshots"
|
SCREENSHOT_DIR = Path.home() / "Pictures" / "Screenshots"
|
||||||
|
|
||||||
|
|
||||||
def wait_until_file_exists(filepath: Path, timeout: int = 5):
|
def wait_until_file_exists(filepath: Path, timeout: int = 1):
|
||||||
"""Wait until a file exists or timeout."""
|
"""Wait until a file exists or timeout."""
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
while not filepath.exists():
|
while not filepath.exists():
|
||||||
@@ -47,41 +48,55 @@ def wait_until_file_exists(filepath: Path, timeout: int = 5):
|
|||||||
|
|
||||||
|
|
||||||
def take_screenshot(filepath: Path, typeStr: str):
|
def take_screenshot(filepath: Path, typeStr: str):
|
||||||
type = ScreenshotType(typeStr)
|
lock_fd = open("/tmp/screenshot-script.lock", "w")
|
||||||
currentDesktop = environ.get("XDG_CURRENT_DESKTOP", "")
|
try:
|
||||||
if "Hyprland" in currentDesktop:
|
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
cmd = {
|
except IOError:
|
||||||
ScreenshotType.FULL: f"hyprshot -z -m output -m active -o {SCREENSHOT_DIR} -f ", # since I only have one monitor
|
lock_fd.close()
|
||||||
ScreenshotType.AREA: f"hyprshot -z -m region -o {SCREENSHOT_DIR} -f ",
|
raise RuntimeError("Another screenshot is currently being taken.")
|
||||||
ScreenshotType.WINDOW: f"hyprshot -z -m window -o {SCREENSHOT_DIR} -f ",
|
|
||||||
}
|
try:
|
||||||
process = subprocess.run(f"{cmd[type]}{filepath.name}", shell=True)
|
type = ScreenshotType(typeStr)
|
||||||
if process.returncode != 0:
|
currentDesktop = environ.get("XDG_CURRENT_DESKTOP", "")
|
||||||
raise RuntimeError("Failed to take screenshot: hyprshot command failed.")
|
if "Hyprland" in currentDesktop:
|
||||||
wait_until_file_exists(filepath)
|
cmd = {
|
||||||
elif "niri" in currentDesktop:
|
# since I only have one monitor
|
||||||
cmd = {
|
ScreenshotType.FULL: f"hyprshot -z -m output -m active -o {SCREENSHOT_DIR} -f ",
|
||||||
ScreenshotType.FULL: "niri msg action screenshot-screen",
|
ScreenshotType.AREA: f"hyprshot -z -m region -o {SCREENSHOT_DIR} -f ",
|
||||||
ScreenshotType.AREA: "niri msg action screenshot",
|
ScreenshotType.WINDOW: f"hyprshot -z -m window -o {SCREENSHOT_DIR} -f ",
|
||||||
ScreenshotType.WINDOW: "niri msg action screenshot-window",
|
}
|
||||||
}
|
process = subprocess.run(f"{cmd[type]}{filepath.name}", shell=True)
|
||||||
niriScreenshotPath = SCREENSHOT_DIR / ".niri_screenshot.png"
|
if process.returncode != 0:
|
||||||
if niriScreenshotPath.exists():
|
raise RuntimeError("Failed to take screenshot: hyprshot command failed.")
|
||||||
niriScreenshotPath.unlink()
|
if not wait_until_file_exists(filepath):
|
||||||
# if os.system(cmd[type]):
|
raise RuntimeError("Failed to take screenshot: output file not found after hyprshot command.")
|
||||||
process = subprocess.run(cmd[type], shell=True)
|
elif "niri" in currentDesktop:
|
||||||
if process.returncode != 0:
|
cmd = {
|
||||||
raise RuntimeError("Failed to take screenshot: niri built-in screenshot command failed.")
|
ScreenshotType.FULL: "niri msg action screenshot-screen",
|
||||||
wait_until_file_exists(niriScreenshotPath)
|
ScreenshotType.AREA: "niri msg action screenshot",
|
||||||
if niriScreenshotPath.exists():
|
ScreenshotType.WINDOW: "niri msg action screenshot-window",
|
||||||
# niriScreenshotPath.rename(filepath)
|
}
|
||||||
copy2(niriScreenshotPath, filepath)
|
niriScreenshotPath = SCREENSHOT_DIR / ".niri_screenshot.png"
|
||||||
|
if niriScreenshotPath.exists():
|
||||||
|
niriScreenshotPath.unlink()
|
||||||
|
# if os.system(cmd[type]):
|
||||||
|
process = subprocess.run(cmd[type], shell=True)
|
||||||
|
if process.returncode != 0:
|
||||||
|
raise RuntimeError("Failed to take screenshot: niri built-in screenshot command failed.")
|
||||||
|
|
||||||
|
if wait_until_file_exists(niriScreenshotPath):
|
||||||
|
# niriScreenshotPath.rename(filepath)
|
||||||
|
copy2(niriScreenshotPath, filepath)
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Failed to take screenshot: output file not found after niri command.")
|
||||||
|
if not wait_until_file_exists(filepath):
|
||||||
|
raise RuntimeError("Failed to take screenshot: output file not found after copying.")
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("Failed to take screenshot: screenshot file nto found after niri command.")
|
# print("Unsupported desktop environment.")
|
||||||
wait_until_file_exists(filepath)
|
raise RuntimeError("Unsupported desktop environment.")
|
||||||
else:
|
finally:
|
||||||
# print("Unsupported desktop environment.")
|
fcntl.flock(lock_fd, fcntl.LOCK_UN)
|
||||||
raise RuntimeError("Unsupported desktop environment.")
|
lock_fd.close()
|
||||||
|
|
||||||
|
|
||||||
def edit_screenshot(filepath: Path):
|
def edit_screenshot(filepath: Path):
|
||||||
|
|||||||
Reference in New Issue
Block a user