summary refs log tree commit diff stats
path: root/twerk
blob: 2b0dfd75fc99e9508ecbf5d0f435cb6b14e00ac4 (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
#!/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>\`.

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

	while getopts htfn:w:W:i:p:c: 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" ;;
			*) usage 1 ;;
		esac
	done

	if test -z "$TWERK_HANG"
	then
		TWERK_HANG=$((TWERK_USER_WIDTH+(TWERK_INCLUDE_TIME*6)+10+3))
		echo >&2 "$TWERK_HANG"
	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"
}

### 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
}

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 | " \
		       "$(echo "$date" | head -c$TWERK_DATE_WIDTH)" \
		       "$(echo "$name" | head -c$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 word
			do
				if test $(( linewidth + ${#word} )) -ge "$TWERK_WIDTH"
				then
					echo
					linewidth=0
					printf "%${TWERK_HANG}s \` " ""
					linewidth=$((linewidth+TWERK_HANG+2))
				fi
				printf '%s ' "$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
	echo >&2

	post="$(printf '%s\t%s\n' "$(date +'%FT%T%z')" "$post")"

	eval "printf '%s\n' \"\$post\" | $TWERK_POST_COMMAND"
	_TWERK_INITIAL=false main "$TWERK_FEEDS"
}

refresh() {
	TWERK_FORCE=true main "$TWERK_FEEDS"
}

###################################
_TWERK_INITIAL=true
test -z "$DEBUG" || set -x
test -z "$SOURCE" && main "$@"