Feat(sway): add script for screenshots

This commit is contained in:
Bertrand Benjamin 2023-01-29 06:12:51 +01:00
parent 757c0d3585
commit bcc2772268
2 changed files with 95 additions and 1 deletions

View File

@ -62,6 +62,11 @@ input type:touchpad {
tap enabled
}
input type:pointer {
accel_profile flat
pointer_accel 0
}
#
# Example configuration:
#
@ -88,6 +93,7 @@ input type:touchpad {
# Start your launcher
bindsym $mod+d exec $menu
bindsym $mod+p exec rofi-pass
bindsym Print exec ~/.local/bin/sway/rofi_screenshot.sh 2> ~/tmp/rofi_debug.log
# Drag floating windows by holding down $mod and left mouse button.
# Resize them with right mouse button + $mod.
@ -240,7 +246,8 @@ bindsym XF86MonBrightnessDown exec lightctl down
# Decoration
#
default_border pixel 1
default_border pixel 2
# class border backgr. text indicator child_border
client.focused #d65d0e #be5046 #ffffff #be5046 #be5046

View File

@ -0,0 +1,87 @@
#! /usr/bin/zsh
set -e
PICTURES_DIR="$(xdg-user-dir PICTURES)"
ZK_NOTEBOOK_DIR="/home/lafrite/Nextcloud/Documents/zettelkasten/"
monitor="$(swaymsg -t get_outputs | jq '[.[].focused] | index(true)')"
alias myrofi="rofi -dmenu -matching fuzzy -i -sort -sorting-method fzf -monitor $monitor"
select_window() {
declare -A windows
swaymsg -t get_tree |
jq -r '..|.nodes? + .floating_nodes?|arrays|select(length > 0)|.[]|select((.nodes + .floating_nodes | length) == 0)|select(.visible)|(.app_id + ": " + .name + "\n" + (.rect.x | tostring) + "," + (.rect.y | tostring) + " " + (.rect.width | tostring) + "x" + (.rect.height | tostring))' |
while read window_name; read geometry; do
windows[$window_name]="$geometry"
done
echo ${windows[$(print -l ${(@k)windows} | myrofi)]}
}
main() {
actions="Copy region to clipboard\nSave region to $PICTURES_DIR\nCopy window to clipboard\nSave window to $PICTURES_DIR\nSave region to zk"
if [[ "$(swaymsg -t get_outputs | jq length)" == "1" ]]; then
actions+="\nCopy screen to clipboard\nSave screen to $PICTURES_DIR"
else
actions+="\nCopy current monitor to clipboard\nSave current monitor to $PICTURES_DIR\nCopy all monitors to clipboard\nSave all monitors to $PICTURES_DIR"
fi
selection="$(echo "$actions" | myrofi -p "Take a screenshot")"
filename="$(date +'screenshot_%Y%m%d%H%M%s.png')"
imgpath="${PICTURES_DIR}/$filename"
zkpath="${ZK_NOTEBOOK_DIR}/fig/$filename"
case "$selection" in
"Copy region to clipboard")
grim -g "$(slurp)" - | wl-copy
notify-send "Region copied to clipboard"
;;
"Save region to $PICTURES_DIR")
grim -g "$(slurp)" "$imagepath"
reply=$(notify-send -A 'open,Open' -i "$imagepath" "Screenshot saved")
;;
"Save region to zk")
grim -g "$(slurp)" "$zkpath"
sh_path="../fig/${zkpath##*/}"
wl-copy $sh_path
reply=$(notify-send -A 'open,Open' -i "$imagepath" "Screenshot saved" "$sh_path is copied")
;;
"Copy window to clipboard")
grim -g "$(select_window)" - | wl-copy
notify-send "Window copied to clipboard"
;;
"Save window to $PICTURES_DIR")
grim -g "$(select_window)" "$imagepath"
reply=$(notify-send -A 'open,Open' -i "$imagepath" "Screenshot saved")
;;
"Copy current monitor to clipboard")
monitor="$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')"
grim -o $monitor - | wl-copy
notify-send "Monitor $monitor copied to clipboard"
;;
"Save current monitor to $PICTURES_DIR")
grim -o "$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')" "$imagepath"
reply=$(notify-send -A 'open,Open' -i "$imagepath" "Screenshot saved")
;;
"Copy screen to clipboard"|"Copy all monitors to clipboard")
grim - | wl-copy
notify-send "Screen copied to clipboard"
;;
"Save screen to $PICTURES_DIR"|"Save all monitors to $PICTURES_DIR")
grim "$imagepath"
reply=$(notify-send -A 'open,Open' -i "$imagepath" "Screenshot saved")
;;
*)
exit 1
;;
esac
if [[ "$reply" == "2" ]]; then
nautilus -s $imagepath
fi
}
main "$@"