summaryrefslogtreecommitdiff
path: root/.local/bin/freezshot
blob: ecc26acf2122dcae548bad08ebf8e4dc4c50c9f2 (plain) (blame)
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
#!/bin/sh

tmp="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}"
tmp="$(mktemp -d "${tmp}/freezhot.XXXX")"
out="${GRIM_DEFAULT_DIR:-${XDG_PICTURES_DIR:-.}}"

note()
{
	: "${1:?}"

	command -v notify-send > /dev/null &&
		notify-send "󰩬   freezshot" "$1"
}

die()
{
	printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2
	exit "${2:-1}"
}

dep_check()
{
	for dep; do
		command -v "$dep" > /dev/null ||
			die "$dep is not installed" 127
	done

	unset dep
}

wl_res()
{
	swaymsg -pt get_outputs | while read -r line; do 
		case "$line" in 
		*"current)")
			echo "${line% px*}"
			return
		esac
	done
}

########
# MAIN #
########

dep_check grim slurp imv
[ -d "$out" ] || mkdir -p "$out"

GRIM_DEFAULT_DIR="$tmp" grim
imv -f "$tmp"/* &
view_pid=$!

if region=$(slurp) > /dev/null 2>&1; then
	if [ "${region#* }" = "$(wl_res)" ]; then
		cp "$tmp"/* "$out"
	else
		grim -g "$region"
	fi
fi

kill $view_pid
rmdir "$tmp"