/
grimcap
executable file
·97 lines (82 loc) · 2.69 KB
/
grimcap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
# grimcap
# wrapper around grim, wf-recorder & slurp
# handles screenshots (to clipboard) & recording
# each dependency could be considered optional, depending on use-case
# dependencies:
# grim - capture screenshot
# slurp - select geometry
# wf-recorder - capture video
# wl-clipboard - copy
# hyprctl OR swaymsg - active window detection
# (swayimg) - open image
# (mpv) - play video
# jq - json
# references:
# 1. https://wiki.archlinux.org/title/Screen_capture#Wayland
# 2. https://github.com/hyprwm/contrib/blob/main/grimblast/grimblast
# 3. https://github.com/swaywm/sway/blob/master/contrib/grimshot
# a simplified take on grimblast but we retain swayland compatability from grimshot, implement screen recording, & more
# this don't process or edit captures in any way; simply captures, stores & optionally opens if swayimg is installed & a file was saved
# created by following the wayland section of the archwiki
usage() {
cat <<EOF
Usage: grimcap <ACTION> <TARGET> [DIR]
Actions:
snap: screenshot to DIR
snap-copy: screenshot to clipboard
rec: record to DIR
Targets:
active: active window
area: select a region
screen: entire display
EOF
exit 1
}
_into() {
capDir="${3:-$HOME/.caps}"
[[ $capDir =~ /.caps ]]&& [[ ! -d $capDir ]]&& mkdir "$capDir"
printf "${capDir%/}/%(%Y-%m-%d_%H-%M-%S)T_$2.$1"
}
active_win() {
if hash hyprctl 2>/dev/null; then
hyprctl -j activewindow | \
jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"'
elif hash swaymsg 2>/dev/null; then
swaymsg -t get_tree | \
jq -r '.. | select(.focused?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"'
fi
}
case "$1" in
'snap')
capPath="$(_into 'png' "$2" "$3")"
case "$2" in
'active') grim -l 9 -g "$(active_win)" "$capPath";;
'area') slurp | grim -l 9 -g - "$capPath";;
'screen') grim -l 9 "$capPath";;
*) usage;;
esac
printf '%s\n' "$capPath"
hash swayimg 2>/dev/null&& swayimg "$capPath"
;;
'snap-copy')
case "$2" in
'active') grim -l 9 -g "$(active_win)" - | wl-copy;;
'area') slurp | grim -l 9 -g - - | wl-copy;;
'screen') grim -l 9 - | wl-copy;;
*) usage;;
esac
;;
'rec')
capPath="$(_into 'mp4' "$2" "$3")"
case "$2" in
'active') wf-recorder -g "$(active_win)" -f "$capPath";;
'area') wf-recorder -g "$(slurp)" -f "$capPath";;
'screen') wf-recorder -f "$capPath";;
*) usage;;
esac
printf '%s\n' "$capPath"
hash mpv 2>/dev/null&& mpv "$capPath"
;;
*) usage;;
esac