feat(sway): add screenshot functionality

This commit is contained in:
Yorick Barbanneau 2025-08-29 12:53:26 +02:00
parent c66ff4d68b
commit cc2a158732
Signed by: ephase
GPG key ID: 4447A19BBEDB8DBA

View file

@ -87,8 +87,11 @@ in
"${mod}+Shift+space" = "floating toggle";
"${mod}+space" = "focus mode_toggle";
"${mod}+a" = "focus parent";
"${mod}+Shift+p" = "move scratchpad";
"${mod}+p" = "scratchpad show";
"${mod}+Shift+minus" = "move scratchpad";
"${mod}+minus" = "scratchpad show";
"${mod}+p" = "exec screenshot window";
"${mod}+Shift+p" = "exec screenshot screen";
"${mod}+Alt+p" = "mode screenshot";
# Media stuff
"${mod}+F1" = "exec ${pkgs.brightnessctl}/bin/brightnessctl s 1%-";
"${mod}+F2" = "exec ${pkgs.brightnessctl}/bin/brightnessctl s +1%";
@ -118,6 +121,17 @@ in
"r" = "exec ${pkgs.mako}/bin/makoctl restore";
"Escape" = "mode default";
};
"screenshot" = {
"s" = "exec screenshot screen; mode default";
"Shift+s" = "exec screenshot screen -f; mode default";
"r" = "exec screenshot region; mode default";
"Shift+r" = "exec screenshot region -f; mode default";
"w" = "exec screenshot window; mode default";
"Shift+w" = "exec screenshot window -f; mode default";
"Return" = "mode default";
"Escape" = "mode default";
};
};
colors = {
focused = {
@ -214,5 +228,69 @@ in
title_align right
'';
};
home.file.".local/bin/screenshot" = {
executable = true;
text = ''
#!/usr/bin/env bash
set -eu -o pipefail
SWAYMSG="${pkgs.sway}/bin/swaymsg"
JQ="${pkgs.jq}/bin/jq"
WLCOPY="${pkgs.wl-clipboard}/bin/wl-copy"
GRIM="${pkgs.grim}/bin/grim"
SLURP="${pkgs.slurp}/bin/slurp"
# What we want to screenshot
case ''${1:-} in
region)
RECT=$(''$SLURP)
APPNAME="-region"
OUTPUT=$(''${SWAYMSG} -t get_outputs | ''${JQ} -r '.[] | select(.focused) | "\(.name)"')
COMMAND=(
"''${GRIM}"
-g
"''$RECT"
)
;;
screen)
OUTPUT=$(''${SWAYMSG} -t get_outputs | ''${JQ} -r '.[] | select(.focused) | "\(.name)"')
COMMAND=(
"''${GRIM}"
-o
"''${OUTPUT}"
)
;;
window)
RECT=$(''${SWAYMSG} -t get_tree | ''${JQ} -r '.. | ((.nodes? // empty) + (.floating_nodes? // empty))[] | select(.visible and .pid and .focused) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' )
APPNAME=$(''${SWAYMSG} -t get_tree | ''${JQ} -r '.. | ((.nodes? // empty) + (.floating_nodes? // empty))[] | select(.visible and .pid and .focused) | "\(.app_id)"')
COMMAND=(
"''${GRIM}"
-g
"''${RECT}"
)
;;
*)
>&2 printf "Can't understand what you need\n"
exit 1
esac
# Where we want to put it
case ''${2:-"-c"} in
-c|--clipboard)
COMMAND+=(-)
"''${COMMAND[@]}" | ''${WLCOPY}
;;
-f|--file)
PICTURES_DIR="''${XDG_PICTURES_DIR}/screenshots"
mkdir -p "''${PICTURES_DIR}"
printf -v DATE '%(%F_%H.%M.%S)T'
printf -v FILE "%s/%s-%s%s.png" "''${PICTURES_DIR}" "''${DATE}" "''${OUTPUT:-""}" "''${APPNAME:-""}"
"''${COMMAND[@]}" "''${FILE//\"/}"
;;
esac
exit 0
'';
};
};
}