-
Notifications
You must be signed in to change notification settings - Fork 0
/
chronosnap
executable file
·205 lines (159 loc) · 4.5 KB
/
chronosnap
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# ------------------------------------------------------------------------------
# settings settings
# ------------------------------------------------------------------------------
set -o errtrace # any trap on ERR is inherited by shell functions,
# command substitutions, and commands executed in a subshell
# environment
set -o nounset # treat unset variables and parameters other than the special
# parameters "@" and "*" as an error when performing
# parameter expansion
set -o pipefail # the return value of a pipeline is the value of the last
# (rightmost) command to exit with a non-zero status, or
# zero if all commands in the pipeline exit successfully
# set language
export LANG="en_US.UTF-8"
# use dot as decimal separator
export LC_NUMERIC="en_US.UTF-8"
# terminal colors
readonly NONE=$(tput sgr0)
readonly RED=$(tput setaf 1)
readonly GREEN=$(tput setaf 2)
readonly YELLOW=$(tput setaf 3)
readonly BLUE=$(tput setaf 4)
readonly MAGENTA=$(tput setaf 5)
readonly CYAN=$(tput setaf 6)
readonly WHITE=$(tput setaf 7)
# ------------------------------------------------------------------------------
# functions functions
# ------------------------------------------------------------------------------
# -------
# failmsg
# -------
#
# This function prints a red colored message via stderr.
#
function failmsg()
{
# push to stderr
echo -e "${RED}[FAIL]${NONE} $1" 1>&2
}
# -------
# warnmsg
# -------
#
# This function prints a yellow colored message via stderr.
#
function warnmsg()
{
# push to stderr
echo -e "${YELLOW}[WARN]${NONE} $1" 1>&2
}
# -------
# infomsg
# -------
#
# This function prints a blue colored message via stderr.
#
function infomsg()
{
# push to stderr
echo -e "${BLUE}[INFO]${NONE} $1" 1>&2
}
# -------
# donemsg
# -------
#
# This function prints a green colored message via stderr.
#
function donemsg()
{
# push to stderr
echo -e "${GREEN}[DONE]${NONE} $1" 1>&2
}
# ----------
# screenshot
# ----------
#
# $2 crop settings (geometry: WxH+X+Y)
#
function screenshot()
{
# set output filename
local FILENAME=$(mktemp --suffix=".png")
# get screenshot of the entire screen and crop
import -window "root" \
-crop "$1" \
-colorspace "sRGB" \
-depth "8" \
-type "TrueColorMatte" \
-define "png:bit-depth=8" \
-define "png:color-type=6" \
-define "png:format=png32" \
-quality "100" \
"PNG32:$FILENAME" \
&>"/dev/null"
# check path and size
if [ ! -s "$FILENAME" ] ; then
# remove empty file
rm -f "$FILENAME"
# signalize trouble
return 0
fi
# push filename to stdout
echo "$FILENAME"
# signalize success
return 1
}
# ------------------------------------------------------------------------------
# commands commands
# ------------------------------------------------------------------------------
# get crop geometry
readonly GEOMETRY=$(chronocfg -g 2>"/dev/null")
# check return value
if [ -z "$GEOMETRY" ] ; then
# notify user
failmsg "no crop geometry given"
# signalize trouble
exit 1
fi
# get output directory
readonly TARGETDIR=$(chronocfg -d 2>"/dev/null")
# check return value
if [ -z "$TARGETDIR" ] ; then
# notify user
failmsg "no output directory given"
# signalize trouble
exit 1
fi
# check directory
if [ ! -d "$TARGETDIR" ] ; then
# try to create target directory
mkdir --parents "$TARGETDIR"
# check if target directory is still missing
if [ ! -d "$TARGETDIR" ] ; then
# notify user
failmsg "unable to create directory: \"$TARGETDIR\""
# signalize trouble
exit 1
fi
fi
# get final filename first (timestamp)
readonly OUTFILE="$(chrononext "$TARGETDIR").png"
# create screenshot
readonly TEMPFILE=$(screenshot "$GEOMETRY")
# check filename
if [ -z "$TEMPFILE" ] ; then
# notify user
failmsg "unable to create screenshot"
# signalize trouble
exit 1
fi
# move temporary file
mv --force "$TEMPFILE" "$OUTFILE"
# show created filename
donemsg "file created: $(basename "$OUTFILE")"
# show created filename
chronosend "file created: $(basename "$OUTFILE")"
# signalize success
exit 0