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
|
#!/bin/sh
## twerk: a twtxt client in sh
# by Case Duckworth
### Entry point
usage() {
cat >&2 <<EOF
TWERK: a twtxt client
usage: twerk [options] [file]
options:
-h show this help and quit
-t include time in post date
-T don't include time in post date (default)
-n NUMBER limit posts to NUMBER (100)
-w WIDTH limit the width of the display (72)
-H WIDTH define the hanging indent of twts
-u WIDTH limit the width of the user display (12)
EOF
exit "${1:-0}"
}
configure() {
TWERK_INCLUDE_TIME=0
TWERK_WIDTH="${COLUMNS:-72}"
TWERK_USER_WIDTH=12
while getopts hn:w:H:t opt; do
case "$opt" in
h) usage ;;
n) TWERK_N="$OPTARG" ;;
w) TWERK_WIDTH="$OPTARG" ;;
H) TWERK_HANG="$OPTARG" ;;
u) TWERK_USER_WIDTH="$OPTARG" ;;
t) TWERK_INCLUDE_TIME=1 ;;
T) TWERK_INCLUDE_TIME=0 ;;
*) usage 1 ;;
esac
done
if test -z "$TWERK_HANG"; then
TWERK_HANG=$((TWERK_USER_WIDTH+(TWERK_INCLUDE_TIME*6)+10+3))
fi
}
main() {
configure "$@"; shift "$((OPTIND - 1))"
cat "${@:--}" |
curl_from_file
}
### Library
curl_from_file() {
while read -r url name; do
curl -sf "$url" |
filter_posts |
awk -v url="$url" -v name="$name" \
'{ printf "%s\t%s\t%s\n", name, url, $0; }'
done |
sort_posts |
format_posts
}
filter_posts() {
filter_comments | limit_posts "${TWERK_N:-100}"
}
filter_comments() {
awk '/^[ \t]*#/{next;}/^$/{next;}{print;}'
}
limit_posts() {
head -n "$1"
}
sort_posts() {
sort -r
}
format_posts() {
awk \
-v UW="$TWERK_USER_WIDTH" \
-v IT="$TWERK_INCLUDE_TIME" \
-v WIDTH="$TWERK_WIDTH" \
-v HANG="$TWERK_HANG" \
'BEGIN {FS="\t";}
!IT { sub(/T.*$/, "", $3); }
IT {
sub(/+.*$/, "", $3);
sub(/T/, " ", $3);
sub(/:..$/,"",$3);
}
{ printf("%s | %" UW "s | ", $3, substr($1,1,UW-1)); }
# wrap lines
{
w = HANG # width
ls = 0 # lines
split($2, words, /[ \t]/)
for (i=1; i<=length(words); i++) {
if (w+length(words[i]) >= WIDTH) {
print ""
w = 0
if (++ls) {
printf "%" HANG "s ` ", ""
w += HANG + 2
}
}
printf "%s ", words[i]
w += length(words[i])
}
print ""
}
'
}
###
test -z "$DEBUG" || set -x
test -z "$SOURCE" && main "$@"
|