-
Notifications
You must be signed in to change notification settings - Fork 2
/
foldercheck
executable file
·225 lines (198 loc) · 4.13 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
#!/bin/sh
# 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"
date_fmt=$(mhdr -h FolderCheckDateFmt $profile)
[ -z "$date_fmt" ] && date_fmt="%Y-%m-%d %H:%M"
sleep=$(mhdr -h FolderCheckSleep $profile)
[ -z "$sleep" ] && sleep=30
default_sleep=$sleep
TEMPFILES=""
if [ $# -gt 0 ]; then
sleep=$1
shift
fi
################################################
remember_tempfile () {
local fn=$1
case $TEMPFILES in
*${fn}*) ;;
*) TEMPFILES="${TEMPFILES} $fn" ;;
esac
}
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
}
stty_size_field () {
stty -a | perl -lne '/(\d+)\s+'$1'/ && print qq|$1\n|'
}
tty_rows () {
echo $(stty_size_field rows)
}
tty_cols () {
echo $(stty_size_field columns)
}
center_msg () {
local x y str mid_len
str="$*"
mid_len=${#str}
mid_len=$(expr $mid_len / 2)
x=$(tty_cols)
y=$(tty_rows)
y=$(expr $y / 2)
y=$(expr $y - 1)
x=$(expr $x / 2)
x=$(expr $x - $mid_len)
tput clear
tput cup $y $x bold
echo -n $str
tput sgr0 civis
}
count_file_lines () {
wc -l $1 | awk '{print $1}'
}
# 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 () {
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 sc
if [ $n -lt $(expr $rows - 1) ]; then
cat $rs
else
local rm1=$(expr $rows - 2) r=0
local last=$(expr $n - $rm1)
# scroll the list forward slowly
while [ $r -le $last ]; do
# rc=return to saved position
# ed=clear to end of screen
tput rc ed
if [ $r -eq 0 ]; then
head -$rm1 $rs
else
sed -e "1,${r}d" <$rs | head -$rm1
fi
sleep 2
thee_sleep=$(expr $thee_sleep - 2)
r=$(expr $r + 1)
done
sleep 1 # pause between
thee_sleep=$(expr $thee_sleep - 1)
r=$(expr $r - 1)
while [ $r -ge 0 ]; do
tput rc ed
if [ $r -eq 0 ]; then
head -$rm1 $rs
else
sed -e "1,${r}d" <$rs | head -$rm1
fi
sleep 2
thee_sleep=$(expr $thee_sleep - 2)
r=$(expr $r - 1)
done
case $thee_sleep in
-*) thee_sleep="" ;;
*)
if [ $thee_sleep -gt 5 ]; then
thee_sleep=2
fi
;;
esac
fi
forget_tempfile $rs
rm -f $rs
sleep=$thee_sleep
}
check () {
local cols tmpf=$1
cols=$(tty_cols)
[ -z "$cols" ] && cols=$COLUMNS
[ $had -eq 0 ] && center_msg '~looking...~'
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
}
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
}
trap 'interrupted' INT TERM
trap 'wakeup; restart' INFO WINCH QUIT
tmpf=${TMPDIR-/tmp}/foldercheck.$$
while true; do
check $tmpf
[[ -n "$sleep" ]] && shleep $sleep
sleep=$default_sleep
done