-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetread
executable file
·166 lines (147 loc) · 4.26 KB
/
tweetread
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
#!/bin/bash
#
# Read Twitter stuffs from a file.
# Twitter -> bitlbee -> irssi -> file -> this script for reading large amounts of lines
# Display lines one by one with a scroll up/down. Toggle save to keep for later. And xclip integration for visiting links
#
# Reads data from $in
# space/enter/j/n for next
# k/p for previous
# s toggles save (displays on quit for later use)
# q quits
# u to put URLs one by one into xclip
# l to refresh. Lazy way out of issue with long lines that wrap around...
#
#
# TODO
# Should have more robust handling around input file. And output... Save to input file with confirmation?
#
# On TERM, make cursor visible
trap 'tput cnorm' EXIT
file="${1:-./twitter}"
offset="${2:- 0}"
tmout="$TMOUT"
unset TMOUT
_size=$(tput lines)
_red=$(tput setaf 1)
_blue=$(tput setaf 4)
_magenta=$(tput setaf 5)
_cyan=$(tput setaf 6)
_def=$(tput setaf 9)
_cur_reset=$(tput clear;tput cup $((_size/3)) 1)
cur_reset () { printf "%s" "$_cur_reset" ;}
_init="$(tput civis; tput setaf 9)" # Make cursor invisible, reset color
init () { printf "%s" "$_init" ;}
type xclip &>/dev/null && has_xclip=1 || has_xclip=0
# Print a new feed item over the old line
print () {
cur_reset
[[ ${save[index]} ]] && color="$_cyan" || color="$_def"
printf "%s%d/%d %s%s%s\n" "$_red" $((index+1)) $max "$color" "${lines[index]}" "$_def" | fmt -w $(tput cols)
# Find all URLs and xclip them
urls=()
for word in ${lines[index]} ; do
! [[ $word = http* || $word = www* ]] && continue
urls+=("$word")
done
[[ $urls ]] && {
((has_xclip)) && xclip -i -selection clipboard <<< "${urls[@]}"
((has_xclip)) && xclip -i -selection primary <<< "${urls[@]}"
for i in "${urls[@]}" ; do printf "%s URL copied: %s%s\n" "$_magenta" "$i" "$_def" ; done
}
(( ${#save[@]} )) && printf "%d saved items" ${#save[@]}
}
reset_screen () {
init
print
}
if ! [[ -r $file ]] ; then
echo "File $file not readable."
exit 1
fi
lines=()
save=()
# The tweets to serve up
while read -r l ; do [[ $l ]] && lines+=("$l") ; done < "$file"
lines+=("${_red}No more items$_def")
index="$offset"
max="${#lines[@]}"
# Starting point and first item
reset_screen
#shopt -s nocasematch
while read -sN1 ; do
case "$REPLY" in
"" | [nj] | " " ) # Next
((index < max - 1 && index++))
print
;;
[pk]) # Back/prev
((index > 0 && index--))
print
;;
[of]) # Open with ff
for i in "${urls[@]}" ; do firefox "$i" ; done
((index < max - 1 && index++)) # Advance to next item on save
print
;;
s) # Toggle save an item. Displayed on quit
[[ ${save[index]} ]] && unset save[index] || save[index]=1
((index < max - 1 && index++)) # Advance to next item on save
print
;;
S) # Set everything as saved
for ((i = 0; i < max; i++)) ; do save[i]=1 ; done
print
;;
A) # Set everything as saved - from here to the end
for ((i = index; i < max; i++)) ; do save[i]=1 ; done
print
;;
d) # Skip down to next divider line. Look for "===========". Save as we go
for ((i = index; i < max; i++)) ; do save[i]=1 ; [[ ${lines[i]} = *'=========='* ]] && break ; done
index=$((i + 1))
print
;;
l) # Reset/refresh screen
reset_screen
;;
q | ) # Quit, printing saved items
printf "\n\n"
if (( ${#save[@]} )) ; then
# Put together the saved entries
out=()
for i in "${!save[@]}" ; do out+=( "${lines[i]}" ) ; done
# Write out saved entires to STDOUT or $file
read -n1 -p "Write out ${#save[@]} saved entries to file $file? [y/n] "
printf "\n"
if ! [[ $REPLY = [yYnN] ]] ; then
print
continue
fi
[[ $REPLY = [yY] ]] && printf "%s\n" "${out[@]}" > "$file" || printf "%s\n" "${out[@]}"
else
read -n1 -p "Clear file $file? [y/n] "
printf "\n"
if ! [[ $REPLY = [yYnN] ]] ; then
print
continue
fi
[[ $REPLY = [yY] ]] && : > "$file"
fi
tput cnorm # Make cursor visible again
TMOUT="$tmout"
exit
;;
u ) # Move a URL to clip
for word in ${lines[index]} ; do # Yay! Intentional word splitting
! [[ $word = http* || $word = www* ]] && continue
((has_xclip)) && xclip -i -selection clipboard <<< "$word"
((has_xclip)) && xclip -i -selection primary <<< "$word"
cur_reset
printf "URL %s%s%s -> press any key to continue" "$_blue" "$word" "$_def"
read -N1
done
print
;;
esac
done