adapt screenshot script to niri
This commit is contained in:
@@ -32,7 +32,7 @@ elif [ "$XDG_CURRENT_DESKTOP" = "niri" ]; then
|
|||||||
killall swaybg
|
killall swaybg
|
||||||
killall swww
|
killall swww
|
||||||
|
|
||||||
cache_dir="$HOME/.cache/swaybg"
|
cache_dir="$HOME/.local/share/swaybg"
|
||||||
mkdir -p "$cache_dir" || (
|
mkdir -p "$cache_dir" || (
|
||||||
notify-send "Error" "Could not create cache directory"
|
notify-send "Error" "Could not create cache directory"
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from enum import Enum
|
||||||
|
import time
|
||||||
|
|
||||||
# autopep8: off
|
# autopep8: off
|
||||||
import gi
|
import gi
|
||||||
@@ -10,18 +12,61 @@ gi.require_version("Notify", "0.7")
|
|||||||
from gi.repository import Notify, GLib
|
from gi.repository import Notify, GLib
|
||||||
# autopep8: on
|
# autopep8: on
|
||||||
|
|
||||||
|
|
||||||
|
class ScreenshotType(Enum):
|
||||||
|
FULL = "full"
|
||||||
|
AREA = "area"
|
||||||
|
WINDOW = "window"
|
||||||
|
|
||||||
|
|
||||||
SCREENSHOT_DIR = os.path.join(
|
SCREENSHOT_DIR = os.path.join(
|
||||||
os.environ.get("XDG_PICTURES_DIR", os.path.expanduser("~/Pictures")),
|
os.environ.get("XDG_PICTURES_DIR", os.path.expanduser("~/Pictures")),
|
||||||
"Screenshots"
|
"Screenshots"
|
||||||
)
|
)
|
||||||
# full cmd: {cmd}{filename}
|
|
||||||
SCREENSHOT_CMDS = {
|
|
||||||
"full": f"hyprshot -z -m output -m active -o {SCREENSHOT_DIR} -f ", # since I only have one monitor
|
def wait_until_file_exists(filepath: str, timeout: int = 5):
|
||||||
"area": f"hyprshot -z -m region -o {SCREENSHOT_DIR} -f ",
|
"""Wait until a file exists or timeout."""
|
||||||
"window": f"hyprshot -z -m window -o {SCREENSHOT_DIR} -f ",
|
start_time = time.time()
|
||||||
}
|
while not os.path.isfile(filepath):
|
||||||
# full cmd: {cmd}{filename}
|
if time.time() - start_time > timeout:
|
||||||
EDIT_CMD = f"gradia {SCREENSHOT_DIR}{os.sep}"
|
return False
|
||||||
|
time.sleep(0.1)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def take_screenshot(typeStr: str, filepath: str):
|
||||||
|
type = ScreenshotType(typeStr)
|
||||||
|
currentDesktop = os.environ.get("XDG_CURRENT_DESKTOP", "")
|
||||||
|
if "Hyprland" in currentDesktop:
|
||||||
|
cmd = {
|
||||||
|
ScreenshotType.FULL: f"hyprshot -z -m output -m active -o {SCREENSHOT_DIR} -f ", # since I only have one monitor
|
||||||
|
ScreenshotType.AREA: f"hyprshot -z -m region -o {SCREENSHOT_DIR} -f ",
|
||||||
|
ScreenshotType.WINDOW: f"hyprshot -z -m window -o {SCREENSHOT_DIR} -f ",
|
||||||
|
}
|
||||||
|
os.system(f"{cmd[type]}{filepath}")
|
||||||
|
wait_until_file_exists(filepath)
|
||||||
|
elif "niri" in currentDesktop:
|
||||||
|
cmd = {
|
||||||
|
ScreenshotType.FULL: f"niri msg action screenshot-screen",
|
||||||
|
ScreenshotType.AREA: f"niri msg action screenshot",
|
||||||
|
ScreenshotType.WINDOW: f"niri msg action screenshot-window",
|
||||||
|
}
|
||||||
|
niriScreenshotPath = SCREENSHOT_DIR + os.sep + ".niri_screenshot.png"
|
||||||
|
if os.path.isfile(niriScreenshotPath):
|
||||||
|
os.remove(niriScreenshotPath)
|
||||||
|
os.system(cmd[type])
|
||||||
|
wait_until_file_exists(niriScreenshotPath)
|
||||||
|
if os.path.isfile(niriScreenshotPath):
|
||||||
|
os.rename(niriScreenshotPath, filepath)
|
||||||
|
wait_until_file_exists(filepath)
|
||||||
|
else:
|
||||||
|
print("Unsupported desktop environment.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def edit_screenshot(filepath: str):
|
||||||
|
os.system(f"gradia {SCREENSHOT_DIR}{os.sep}{filepath}")
|
||||||
|
|
||||||
|
|
||||||
def file_name(dir, prefix="screenshot", ext=".png"):
|
def file_name(dir, prefix="screenshot", ext=".png"):
|
||||||
@@ -33,7 +78,7 @@ if __name__ == "__main__":
|
|||||||
parser = argparse.ArgumentParser(description="Take screenshots with hyprshot.")
|
parser = argparse.ArgumentParser(description="Take screenshots with hyprshot.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"type",
|
"type",
|
||||||
choices=SCREENSHOT_CMDS.keys(),
|
choices=[t.value for t in ScreenshotType],
|
||||||
help="Type of screenshot to take.",
|
help="Type of screenshot to take.",
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@@ -44,12 +89,7 @@ if __name__ == "__main__":
|
|||||||
filepath = os.path.join(SCREENSHOT_DIR, filename)
|
filepath = os.path.join(SCREENSHOT_DIR, filename)
|
||||||
|
|
||||||
# take screenshot
|
# take screenshot
|
||||||
cmd = f"{SCREENSHOT_CMDS[args.type]} {filename}"
|
take_screenshot(args.type, filepath)
|
||||||
os.system(cmd)
|
|
||||||
|
|
||||||
# sleep for a short interval
|
|
||||||
# neccessary for "window" mode
|
|
||||||
GLib.usleep(300000) # 0.3 seconds
|
|
||||||
|
|
||||||
# check if successful
|
# check if successful
|
||||||
if not os.path.isfile(filepath):
|
if not os.path.isfile(filepath):
|
||||||
@@ -64,7 +104,7 @@ if __name__ == "__main__":
|
|||||||
def edit_callback(n, action, user_data):
|
def edit_callback(n, action, user_data):
|
||||||
global editing
|
global editing
|
||||||
editing = True
|
editing = True
|
||||||
os.system(f"{EDIT_CMD}{filename}")
|
edit_screenshot(filename)
|
||||||
n.close()
|
n.close()
|
||||||
loop.quit()
|
loop.quit()
|
||||||
|
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ cursor {
|
|||||||
hide-when-typing
|
hide-when-typing
|
||||||
}
|
}
|
||||||
|
|
||||||
screenshot-path "~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png"
|
screenshot-path "~/Pictures/Screenshots/.niri_screenshot.png"
|
||||||
|
|
||||||
// gestures {
|
// gestures {
|
||||||
// hot-corners {
|
// hot-corners {
|
||||||
@@ -270,9 +270,9 @@ binds {
|
|||||||
Mod+V { spawn-sh "pkill rofi || cliphist list | rofi -dmenu -config ~/.config/rofi/dmenu.rasi -display-columns 2 -i | cliphist decode | wl-copy"; }
|
Mod+V { spawn-sh "pkill rofi || cliphist list | rofi -dmenu -config ~/.config/rofi/dmenu.rasi -display-columns 2 -i | cliphist decode | wl-copy"; }
|
||||||
Mod+Period { spawn-sh "pkill rofi || rofi-emoji"; }
|
Mod+Period { spawn-sh "pkill rofi || rofi-emoji"; }
|
||||||
Ctrl+Alt+Delete { spawn-sh "pkill wlogout || wlogout -p layer-shell"; }
|
Ctrl+Alt+Delete { spawn-sh "pkill wlogout || wlogout -p layer-shell"; }
|
||||||
Print { screenshot-screen; }
|
Print { spawn-sh "screenshot full"; }
|
||||||
Mod+Shift+S { screenshot; }
|
Mod+Shift+S { spawn-sh "screenshot area"; }
|
||||||
Mod+Ctrl+Shift+S { screenshot-window; }
|
Mod+Ctrl+Shift+S { spawn-sh "screenshot window"; }
|
||||||
Mod+Shift+C { spawn-sh "hyprpicker -a"; }
|
Mod+Shift+C { spawn-sh "hyprpicker -a"; }
|
||||||
|
|
||||||
// Session
|
// Session
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ cursor {
|
|||||||
hide-when-typing
|
hide-when-typing
|
||||||
}
|
}
|
||||||
|
|
||||||
screenshot-path "~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png"
|
screenshot-path "~/Pictures/Screenshots/.niri_screenshot.png"
|
||||||
|
|
||||||
// gestures {
|
// gestures {
|
||||||
// hot-corners {
|
// hot-corners {
|
||||||
@@ -270,9 +270,9 @@ binds {
|
|||||||
Mod+V { spawn-sh "pkill rofi || cliphist list | rofi -dmenu -config ~/.config/rofi/dmenu.rasi -display-columns 2 -i | cliphist decode | wl-copy"; }
|
Mod+V { spawn-sh "pkill rofi || cliphist list | rofi -dmenu -config ~/.config/rofi/dmenu.rasi -display-columns 2 -i | cliphist decode | wl-copy"; }
|
||||||
Mod+Period { spawn-sh "pkill rofi || rofi-emoji"; }
|
Mod+Period { spawn-sh "pkill rofi || rofi-emoji"; }
|
||||||
Ctrl+Alt+Delete { spawn-sh "pkill wlogout || wlogout -p layer-shell"; }
|
Ctrl+Alt+Delete { spawn-sh "pkill wlogout || wlogout -p layer-shell"; }
|
||||||
Print { screenshot-screen; }
|
Print { spawn-sh "screenshot full"; }
|
||||||
Mod+Shift+S { screenshot; }
|
Mod+Shift+S { spawn-sh "screenshot area"; }
|
||||||
Mod+Ctrl+Shift+S { screenshot-window; }
|
Mod+Ctrl+Shift+S { spawn-sh "screenshot window"; }
|
||||||
Mod+Shift+C { spawn-sh "hyprpicker -a"; }
|
Mod+Shift+C { spawn-sh "hyprpicker -a"; }
|
||||||
|
|
||||||
// Session
|
// Session
|
||||||
|
|||||||
Reference in New Issue
Block a user