blob: 4df2ef6e43340f96f6f211ec258e689dd12ca81e (
plain)
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
|
#!/bin/sh
## twerk: a twtxt client
# by Case Duckworth
### Entry point
usage() {
cat >&2 <<EOF
TWERK: a twtxt client
usage: twerk [options] [file]
twerk can be fed a list of URLs from standard input or from a FILE.
Each line is expected to contain a URL of a twtxt feed and (optionally) the
display name to use for that feed.
options (default):
-h Show this help and exit.
-t Include the time in each post date.
-f Force redownload of twt data.
-n LIMIT Only show LIMIT posts (25). 0 means no limit.
-w WIDTH Limit the output's width to WIDTH characters (72).
-W WIDTH Limit the username's width to WIDTH characters (12).
-i INDENT Use INDENT spaces as a hanging indent for too-long lines
([calculated]).
-p COMMAND Use COMMAND to post twts ([not set]).
This command will be fed a twtxt-formatted string via stdin.
Recommended: \`ssh <host> tee -a <file>\`.
-u USER The username to use when twting ([current user name]).
parameters:
FILE List of twtxt feeds to fetch, one feed per line.
Optionally, a string after the feed URL will be its
display name.
EOF
exit "${1:-0}"
}
configure() {
# defaults
TWERK_WIDTH="${TWERK_WIDTH:-72}"
TWERK_LIMIT="${TWERK_LIMIT:-25}"
TWERK_INCLUDE_TIME="${TWERK_INCLUDE_TIME:-0}"
TWERK_USER_WIDTH="${TWERK_USER_WIDTH:-12}"
TWERK_HANG="${TWERK_HANG:-}" # empty to calculate off other variables
TWERK_FORCE="${TWERK_FORCE:-false}"
TWERK_POST_COMMAND="${TWERK_POST_COMMAND:-:}" # no-op
TWERK_USER="$(whoami)"
while getopts htfn:w:W:i:p:c:u: opt
do
case "$opt" in
h) usage ;;
t) TWERK_INCLUDE_TIME=1 ;;
f) TWERK_FORCE=true ;;
n) TWERK_LIMIT="$OPTARG" ;;
w) TWERK_WIDTH="$OPTARG" ;;
W) TWERK_USER_WIDTH="$OPTARG" ;;
i) TWERK_HANG="$OPTARG" ;;
p) TWERK_POST_COMMAND="$OPTARG" ;;
c) TWERK_FEEDS="$OPTARG" ;;
u) TWERK_USER="$OPTARG" ;;
*) usage 1 ;;
esac
done
if test -z "$TWERK_HANG"
then
TWERK_HANG=$((TWERK_USER_WIDTH+(TWERK_INCLUDE_TIME*6)+10+3))
fi
}
main() {
TWERK_CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/twerk"
mkdir -p "$TWERK_CACHE"
TWERK_FILE="$TWERK_CACHE/:file:"
TWERK_LESSKEY="$TWERK_CACHE/:lesskey:"
lesskey_setup
if "$_TWERK_INITIAL"
then
configure "$@"
shift "$((OPTIND - 1))"
TWERK_FEEDS="$1"
fi
cat "$TWERK_FEEDS" |
fetch_posts |
sort_posts |
limit_posts "$TWERK_LIMIT" |
format_posts > "$TWERK_FILE"
read_posts "$TWERK_FILE"
# cat "$TWERK_FILE"
}
remain() {
_TWERK_INITIAL=false main "$TWERK_FEEDS"
}
### Library
fetch_posts() {
while read -r url name
do
if test -z "$name"
then
file="$TWERK_CACHE/$(url_normalize "$name")"
else
file="$TWERK_CACHE/$name"
fi
if "$TWERK_FORCE" || test -z "$(find "$file" -prune -mtime -1 2>/dev/null)"
then
printf >&2 'Downloading %s...\n' "$url"
curl -sf "$url" |
filter_lines |
while read -r date post
do
printf '%s\t%s\t%s\t%s\n' \
"$date" "$name" "$url" "$post"
done |
tee "$file"
else
cat "$file"
fi
done
}
url_normalize() {
printf '%s\n' "$1" | tr -d ':/~'
}
filter_lines() {
while read -r line
do
case "$line" in
\#*) ;;
'') ;;
*) printf '%s\n' "$line" ;;
esac
done
}
limit_posts() {
ln=0
while read -r line
do
if test "$1" -ne 0 && test "$ln" -gt "$1"
then
break
fi
printf '%s\n' "$line"
ln=$((ln+1))
done
}
sort_posts() {
sort -r
}
struncate() {
printf '%s\n' "$1" |
dd ibs=1 count="$2" 2>/dev/null
}
format_posts() {
while IFS=' ' read -r date name url post
do
if test 0 -eq "$TWERK_INCLUDE_TIME"
then
TWERK_DATE_WIDTH=10
date="${date%T*}"
else
TWERK_DATE_WIDTH=16
fi
printf "%${TWERK_DATE_WIDTH}s | %${TWERK_USER_WIDTH}s | " \
"$(struncate "$date" "$TWERK_DATE_WIDTH")" \
"$(struncate "$name" "$TWERK_USER_WIDTH")"
export url name date post TWERK_WIDTH TWERK_HANG
export linewidth="$TWERK_HANG"
printf '%s\n' "$post" |
tr ' ' '\n' |
while IFS= read -r word
do
pword=
case "$word" in
\@\<*\>)
word="${word#@<}"
word="${word%>}"
pword="[35m@${word}[0m"
word="@${word}"
;;
\@\<*)
read nextword
case "$nextword" in
*\>)
word="${word#@<}"
pword="[35m@${word}[0m"
word="@${word}"
;;
*)
word="$word $nextword"
;;
esac
;;
esac
if test $(( linewidth + ${#word} )) -ge "$TWERK_WIDTH"
then
echo
linewidth=0
printf "%${TWERK_HANG}s \` " ""
linewidth=$((linewidth+TWERK_HANG+2))
fi
printf '%s ' "${pword:-$word}"
linewidth=$((linewidth + ${#word}))
done
echo
done
}
lesskey_setup() {
# t will exit with code 48
if ! test -r "$TWERK_LESSKEY"
then
lesskey -o "$TWERK_LESSKEY" - <<EOF
#command
t quit 0
g quit 1
#line-edit
#env
EOF
fi
}
read_posts() {
less \
-k "$TWERK_LESSKEY" \
-Pm'twerk! t\: twt, g\: refresh' -m \
"$1"
case "$?" in
0) exit ;;
48) post ;;
49) refresh ;;
esac
}
post() {
printf 'Twt: ' >&2
read -r post
post="$(post_expand_ats "$post")"
if printf '%s\n' "$post" | grep -qE '^[ \t\n]$'
then
remain
fi
post="$(printf '%s\t%s\n' "$(date +'%FT%T%z')" "$post")"
eval "printf '%s\n' \"\$post\" | $TWERK_POST_COMMAND"
rm "$TWERK_CACHE/$TWERK_USER"
remain
}
post_expand_ats() {
TWERK_POST="$TWERK_CACHE/:post:"
printf '%s\n' "$@" |
tr ' ' '\n' |
while read -r word; do
case "$word" in
@*)
url="$(grep "${word#@}" "$TWERK_FEEDS" | cut -d' ' -f1)"
if test -n "$url"
then
printf '@<%s %s> ' \
"${word#@}" "$url"
else
printf '%s ' "$word"
fi
;;
*) printf '%s ' "$word";;
esac
done > "$TWERK_POST"
echo >> "$TWERK_POST"
if grep -q @ "$TWERK_POST"
then
cat "$TWERK_POST" >&2
printf '%s\n' "Correct? [Y/n] " >&2
read -r yn
case "$yn" in
n*|N*)
head -n1 "$TWERK_POST" >&2
post
;;
y*|Y*|'') ;;
*) return 3 ;;
esac
fi
head -n1 "$TWERK_POST"
}
refresh() {
TWERK_FORCE=true main "$TWERK_FEEDS"
}
###################################
_TWERK_INITIAL=true
test -z "$DEBUG" || set -x
test -z "$SOURCE" && main "$@"
|