commit 7a6090b81508ae3bffb0370d52b815a9c96d643e
parent eca11c4e9c79032fa9ecfe41d1075a2e55910383
Author: mokou <mokou@posteo.de>
Date: Thu, 16 Apr 2020 21:19:10 +0200
Add screenshotting executable
Diffstat:
2 files changed, 83 insertions(+), 2 deletions(-)
diff --git a/dot_config/sway/config b/dot_config/sway/config
@@ -116,8 +116,8 @@ bindsym $mod+Shift+d floating toggle
bindsym $mod+d focus mode_toggle
# Screenshots
-bindsym $mod+p exec slurp | grim -t png -c -g - ~/screenshot.png
-bindsym $mod+Shift+p exec grim -t png -c ~/screenshot.png
+bindsym print exec ~/.config/sway/screen.sh -s
+bindsym Shift+print exec ~/.config/sway/screen.sh
# Volume
bindsym XF86AudioMute exec pamixer -t
diff --git a/dot_config/sway/executable_screen.sh b/dot_config/sway/executable_screen.sh
@@ -0,0 +1,81 @@
+#!/usr/bin/env bash
+IFS=$'\n'
+
+usage() {
+ cat >&2 << EOF
+$(basename "$0") [ -w | -s | -d ] [ FILE ]
+Take a screenshot
+ -w screenshot current active window
+ -s screenshot selection
+ -d screenshot display (default)
+ -h show this help
+ FILE destination for screenshot
+The last provided flag before [ FILE ] will be used,
+or display by default.
+EOF
+}
+
+# === ENVIRONMENT VARIABLES ===
+# using xdg-open is a good second guess
+# simply set $XIVIEWER in .xinitrc / .profile to change
+viewer=${XIVIEWER:-xdg-open}
+editor=${XIEDITOR:-gimp}
+tmpdir="${SCREENSHOT_TMPDIR:=${TMPDIR:=${XDG_RUNTIME_DIR:-/tmp}}}/screenshots"
+ssdir=${SCREENSHOT_DIRECTORY:-$HOME/pics/screenshots}
+
+# === GETOPTS ===
+# if no opt provided, don't shift
+opt=d
+while getopts ":dhsw" o; do
+ case "$o" in
+ [dsw]) opt="$o" ;;
+ h ) usage && exit 0 ;;
+ * ) usage && exit 1 ;;
+ esac
+done
+shift $(( OPTIND - 1 ))
+
+# === IMAGE LOCATION ===
+if (( $# )); then
+ # create path if it doesn't exist
+ mkdir -p "$(dirname "$1")"
+ img="$1"
+else
+ mkdir -p "$tmpdir"
+ img=$(mktemp "${tmpdir}/$(date +%Y-%m-%d_%T).XXX" --suffix=.png)
+fi
+
+# === TAKE SCREENSHOT ===
+case $opt in # active window / selection / whole screen
+ w)
+ x="$(swaymsg -t get_tree | jq -r \
+ '.nodes[].nodes[]|select(.name!="__i3_scratch")|
+ .floating_nodes[],recurse(.nodes[])|select(.focused)|.rect|
+ (.x|tostring)+","+(.y|tostring)+" "+(.width|tostring)+"x"+(.height|tostring)'
+ )"
+ grim -g "${x%%*$'\n'}" "$img"
+ ;;
+ s) grim -g "$(slurp)" "$img" ;;
+ d) grim "$img" ;;
+esac
+
+# === TAKE ACTION ON FILE ===
+# in a loop, user may want to take many actions
+# while
+action="$(zenity --title="[Grim] Took screenshot: $img" --width=600 --height=300 --list \
+ --column='Command' --column='Choose an action:' <<- EOF
+ wl-copy < "\$img"
+ Copy image to clipboard
+ rm "\$img"; break
+ Delete image and exit
+ $viewer "\$img"
+ View image with $viewer
+ $editor "\$img"
+ Edit image with $editor
+ mkdir -p "\$ssdir"; cp "\$img" "\$ssdir"
+ Save image to $ssdir
+EOF
+)"
+#do
+ eval "$action"
+#done