-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot
executable file
·163 lines (127 loc) · 3.52 KB
/
screenshot
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
# simple bash script for taken screenshot
# - pretty mode (add shadow, corner radious, white backdrop)
# - normal mode
# - full desktop
#
# https://github.com/flameshot-org/flameshot
PROG=$(basename "$0")
DEPENDENCIES=(flameshot magick)
DEF_LOCATION="${XDG_PICTURES_DIR:-$HOME/shares/pic}"/screenshots
function log_err {
printf "%s: %s\n" "$PROG" "$*" >&2
exit 1
}
function pre_exit {
local image="$1"
if [[ ! -f "$image" ]]; then
log_err "'${image}' not found"
fi
printf "%s: %s\n" "$PROG" "cleaning up file '${image}'..."
rm -f "$image"
exit 0
}
function send_notification {
local mesg="$1"
notify-send -i flameshot "$PROG" "$mesg"
}
function open {
local image="$1"
[[ ! -f "${image}" ]] && log_err "'${image}' not found"
xdg-open "${image}" 2>/dev/null
}
function set_path {
local path="$1"
if [[ -n "$path" ]]; then
path=$(realpath "$path")
else
path=$DEF_LOCATION
fi
echo "$path"
}
function get_last_created {
local path="$1"
local image
image=$(find "${path}" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d ' ' -f2-)
echo "${image}"
}
function pretty {
local filesize image image_tmp path
image_tmp=$(mktemp --tmpdir "$PROG.XXXXX")
shift
path=$(set_path "$1")
# shellcheck disable=SC2064
trap "pre_exit $image_tmp" EXIT
flameshot gui --clipboard --raw >"$image_tmp" 2>/dev/null
filesize=$(stat -c%s "$image_tmp")
if [[ "$filesize" -le 0 ]]; then
echo "$PROG: aborting..."
exit 1
fi
# add corners
magick "$image_tmp" \
\( +clone -alpha extract \
-draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
\( +clone -flip \) -compose Multiply -composite \
\( +clone -flop \) -compose Multiply -composite \
\) -alpha off -compose CopyOpacity -composite "$image_tmp"
# shadow
magick "$image_tmp" \
\( +clone -background black -shadow 40x5+0+0 \) \
+swap -background none -layers merge +repage "$image_tmp"
# white backdrop
magick "$image_tmp" -bordercolor white -border 10 "$image_tmp"
# copy image
image="${path}/pretty-$(date +%Y-%m-%d_%H:%M:%S).png"
cp -v "$image_tmp" "${image}"
send_notification "Screenshot saved at <b>${path}</b>"
open "${image}"
}
function normal {
local retcode path
shift
path=$(set_path "$1")
flameshot gui --clipboard --path "${path}" 2>/dev/null
retcode="$?"
if [[ "${retcode}" -ne 0 ]]; then
exit "${retcode}"
fi
open "$(get_last_created "$path")"
}
function desktop {
local retcode filename path
shift
path=$(set_path "$1")
filename="${path}/$(date +%Y-%m-%d_%H:%M:%S)_desktop.png"
flameshot full --clipboard --path "$filename" 2>/dev/null
retcode="$?"
if [[ "${retcode}" -ne 0 ]]; then
exit "${retcode}"
fi
open "$filename"
}
function help {
cat <<-_EOF
Usage: $PROG [options]
options:
-p, --pretty Add shadow, round corners and white backdrop
-n, --normal Normal screenshot with flameshot GUI
-d, --desktop Desktop screenshot
-h, --help Show this help
_EOF
exit
}
function main {
for cmd in "${DEPENDENCIES[@]}"; do
if ! command -v "$cmd" >/dev/null; then
log_err "'$cmd' command not found"
fi
done
case "$1" in
-n | *normal) normal "$@" ;;
-p | *pretty) pretty "$@" ;;
-d | *desktop | *full) desktop "$@" ;;
*) help ;;
esac
}
main "$@"