-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfoldercheck
executable file
·366 lines (330 loc) · 6.68 KB
/
foldercheck
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/ksh
# I run this in a small pane in my tmux mail window. I resize it
# sometimes so it must conform itself to its current size to look
# respectable.
ORIG_CMD="$0 $*"
MBLAZE=${MBLAZE-$HOME/.mblaze}
profile="$MBLAZE/profile"
cfglook () {
mhdr -h $* | envsubst
}
date_fmt=$(cfglook FolderCheckDateFmt $profile)
[ -z "$date_fmt" ] && date_fmt="%Y-%m-%d %H:%M"
sleep=$(cfglook FolderCheckSleep $profile)
[ -z "$sleep" ] && sleep=30
default_sleep=$sleep
SLOWLY=$(cfglook FolderCheckScrollSleep $profile)
[ -z "$SLOWLY" ] && SLOWLY=2
TEMPFILES=""
CUR_MSG=""
CUR_MSG_Y=""
siginfo=""
if [ $# -gt 0 ]; then
sleep=$1
shift
if [ $# -gt 0 ]; then
SLOWLY=$1
shift
fi
echo "[sleep=$sleep SLOWLY=$SLOWLY]" 1>&2
fi
# bsd polyfill
case $(uname) in
*BSD) netstat="netstat -na -f inet"
[ -z "$siginfo" ] && siginfo="INFO"
stty_size_field () {
stty -a | perl -lne '/(\d+)\s+'$1'/ && print $1'
}
colorer=color_a
setaf=setaf
setab=setab
;;
*) netstat="netstat -na4"
stty_size_field () {
stty -a | perl -lne '/'$1'\s+(\d+);/ && print $1'
}
colorer=color_
setaf=setf
setab=setb
;;
esac
cursor_invis=true
cursor_vis=true
if tput civis >/dev/null; then
cursor_invis="tput civis"
cursor_vis="tput cnorm"
fi
tty_rows () {
echo $(stty_size_field rows)
}
tty_cols () {
echo $(stty_size_field columns)
}
mung_colors () {
local bg=$1
if [ -n "$bg" ]; then
tput $setaf $fg
tput $setab $bg
fi
}
center_msg () {
local str="$*" w=$(tty_cols) h=$(tty_rows) x y mid_len
if [ -n "$CUR_MSG" ]; then
# erase previous message
tput cup $CUR_MSG_Y 0
echo -n "$(blanks $w)"
fi
mid_len=${#str}
mid_len=$(expr $mid_len / 2)
y=$(expr $h / 2)
if [ $h -gt 3 ]; then
y=$(expr $y - 1)
fi
x=$(expr $w / 2)
x=$(expr $x - $mid_len)
tput cup $y $x
tput bold
echo -n "$str"
tput sgr0
CUR_MSG="$str"
CUR_MSG_Y=$y
}
color_a () {
# N.B. this is for seta[fb] c.f. terminfo(5)
local c n=$(echo $1 | tr A-Z a-z)
case $n in
black) c=0 ;;
red) c=1 ;;
green) c=2 ;;
yellow) c=3 ;;
blue) c=4 ;;
magenta)c=5 ;;
cyan) c=6 ;;
white) c=7 ;;
*) die "wtf color is $1 ?" ;;
esac
echo $c
}
color_ () {
# N.B. this is for set[fb] c.f. terminfo(5)
local c n=$(echo $1 | tr A-Z a-z)
case $n in
black) c=0 ;;
blue) c=1 ;;
green) c=2 ;;
cyan) c=3 ;;
red) c=4 ;;
magenta)c=5 ;;
yellow) c=6 ;;
white) c=7 ;;
*) die "wtf color is $1 ?" ;;
esac
echo $c
}
blanks () {
perl -e "print ' ' x $1"
}
blank_screen () {
local rows=$(tty_rows) cols=$(tty_cols) bgc=$1
local one_row=$(blanks $cols) row=0
# debug "blank_screen colorer $colorer $1: $setaf $fg $setab $bg"
tput clear
while [ $row -lt $rows ]; do
echo -n "$one_row"
row=$(expr 1 + $row)
done
}
################################################
remember_tempfile () {
local fn=$1
case $TEMPFILES in
*${fn}*) ;;
*) TEMPFILES="${TEMPFILES} $fn" ;;
esac
}
perlx () {
perl -Mv5.10 -MTime::HiRes=time -e "say $*"
}
microtime () {
local t=$(perlx time)
# debug "microtime $t"
echo $t
}
forget_tempfile () {
local fn=$1
TEMPFILES=$(echo $TEMPFILES | perl -lpe "s,${fn},,gs; s,^\s+,,; s,\s+\$,,;")
}
ts () {
date +"$date_fmt"
}
debug () {
echo "$(ts) DEBUG: $*" 1>&2
}
count_file_lines () {
wc -l $1 | awk '{print $1}'
}
squish_last_newline () {
perl -e 'undef($/);$x=<STDIN>;$x=~s/\n+$//;print $x'
}
# ticker-tape-style file display in a potentially small window
# if the output of rs doesn't fit nicely in the window, we
# scroll it slowly down and then back up.
# note: all this tput'ery works in a small tmux pane on OpenBSD, which
# is where I run this... YMMV
had=0
display_file () {
# debug "display_file $*"
local fn=$1 cols=$(tty_cols) rows=$(tty_rows) n=0 rs nlines thee_sleep=$sleep
[ -z "$cols" ] && cols=$COLUMNS
[ -z "$rows" ] && rows=$ROWS
tput clear
nlines=$(count_file_lines $fn)
if [ $nlines -eq 0 ]; then
center_msg '~no mail~'
had=0
return 0
fi
had=1
rs=${fn}.rs.txt
rs -e -w$cols <$fn >$rs
remember_tempfile $rs
n=$(count_file_lines $rs)
# suss out title
local title="~ $nlines w/new mail ~" mid_len x
mid_len=${#title}
mid_len=$(expr $mid_len / 2)
x=$(expr $cols / 2)
x=$(expr $x - $mid_len)
if [ $x -gt 0 ]; then
x=$(expr $x - 1)
title=$(printf %${x}s%s " " "$title")
fi
# looks purty
tput bold
echo "$title"
# sgr0=cancel effects(bold), sc=save position
tput sgr0
tput sc
if [ $n -lt $(expr $rows - 1) ]; then
# done!
cat $rs
else
# scrolly bits
local rm1=$(expr $rows - 1) r=0
local last=$(expr $n - $rm1)
# first scroll the list forward slowly
while [ $r -le $last ]; do
# rc=return to saved position
# ed=clear to end of screen
tput rc
tput ed
if [ $r -eq 0 ]; then
head -$rm1 $rs
else
sed -e "1,${r}d" <$rs | head -$rm1
fi | squish_last_newline
sleep $SLOWLY
thee_sleep=$(perlx "$thee_sleep - $SLOWLY")
r=$(expr $r + 1)
done
sleep 2.5
# now do it backwards
thee_sleep=$(perlx "$thee_sleep - 1")
r=$(expr $r - 1)
if [ $r -gt 0 ]; then
r=$(expr $r - 1)
fi
while [ $r -ge 0 ]; do
tput rc
tput ed
if [ $r -gt 0 ]; then
head -$rm1 $rs
else
sed -e "1,${r}d" <$rs | head -$rm1
fi | squish_last_newline
sleep $SLOWLY
thee_sleep=$(perlx "$thee_sleep - $SLOWLY")
r=$(expr $r - 1)
done
case $thee_sleep in
-*) thee_sleep="" ;; # negative sleep is a thing
*) ;;
esac
fi
forget_tempfile $rs
rm -f $rs
sleep=$thee_sleep
}
first_time=1
check () {
local cols tmpf=$1
# debug "in check $tmpf"
cols=$(tty_cols)
[ -z "$cols" ] && cols=$COLUMNS
if [ $had -eq 0 ]; then
if [ $first_time -eq 1 ]; then
blank_screen
first_time=0
fi
center_msg '~looking...~'
fi
mnewdirs > ${tmpf}
if [ ! -s "$tmpf" ]; then
tput clear
center_msg '~no unread mail~'
had=0
sleep=$default_sleep
else
display_file $tmpf
fi
}
sleeper_pid=0
shleep () {
local secs=$1
sleep $secs & sleeper_pid=$!
wait $sleeper_pid 2>/dev/null
sleeper_pid=0
}
wakeup () {
[ $sleeper_pid -ne 0 ] && \
kill $sleeper_pid >/dev/null 2>&1 && \
wait $sleeper_pid
sleep=0
}
restart () {
tput clear sgr0 cvvis
exec $ORIG_CMD
}
refresh () {
tput clear sgr0 cvvis
[ -n "${tmpf}" -a -f "${tmpf}" ] && display_file ${tmpf}
}
interrupted () {
wakeup
if [ -n "$TEMPFILES" ]; then
echo "$(ts) cleaning up tempfiles: $TEMPFILES"
rm -rf $TEMPFILES
fi
rm -f ${tmpf}
tput sgr0 cvvis
echo ''
echo "$(ts) exiting ..."
exit 1
}
# ^C or SIGTERM kills us dead
trap 'interrupted' INT TERM
trap 'wakeup; restart' WINCH QUIT
[ -n "$siginfo" ] && trap 'wakeup; refresh' $siginfo
tmpf=${TMPDIR-/tmp}/folder###check.$$
while true; do
ttime=0
t0=$(microtime)
check $tmpf
sleep=$default_sleep
while [[ $(perlx "($ttime < $sleep)? 1: 0") -eq 1 ]]; do
display_file $tmpf
ttime=$(perlx "time - $t0")
sleep 2.5
done
sleep 0.1
done