-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.sh
254 lines (222 loc) · 5.78 KB
/
ui.sh
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
#!/usr/bin/env bash
_dump() {
# using w3m to remove color... for now.
echo -e "${@}" | w3m -dump
}
_viewSinglePage() {
outfile=""
max=0
pos=0
declare -a items=()
# utils
_mvUp() { echo -ne "\x1b[1A\r"; }
_mvDown() { echo -ne "\x1b[1B\r"; }
_mvTop() {
pos=0
tput cup 0 0
}
_mvBottom() {
pos=${max}
tput cup ${max} 0
}
_printItem() { echo -ne "${items[pos]}\r" | tr '\\' ' '; }
_printItemBold() { echo -ne "\x1b[1m${items[pos]}\x1b[0m\r" | tr '\\' ' '; }
_printStatusBar() {
tput sc
tput cup $((${LINES} - 1))
echo -ne "\x1b[1m\x1b[38;5;10m[left/right]:navigate [enter/spacebar]:view [c]:clone [q]:back\x1b[0m"
tput rc
}
# parse args
while [[ ${#*} -gt 0 ]]; do
case ${1} in
-o | --outfile) # outfile is passed around as 'props'
shift
outfile="${1}"
shift
;;
*)
items+=("${1}")
shift
;;
esac
done
# empty list
[[ ${#items[@]} -eq 0 ]] && return 1
# limit range to screen height
max="$((${#items[@]} - 1))"
heightLimit=$(($(tput lines) - 4))
[[ ${max} -gt ${heightLimit} ]] &&
max=${heightLimit}
# enter altbuf
trap "tput rmcup" RETURN
trap "tput rmcup; exit 1" SIGINT
tput smcup
# render list of results
for ((i = 0; i < max; i += 1)); do
echo -e "${items[i]}" | tr '\\' ' '
done
_mvBottom # temp fix for not printing last item in list
_printItem
_mvTop
_printItemBold
_printStatusBar
# read every keystroke
while true; do
read -rsn1 keypress
case "${keypress}" in
"A") # up
if [[ pos -gt 0 ]]; then
_printItem
((pos -= 1))
_mvUp
else # loop to bottom
_printItem
_mvBottom
fi
_printItemBold
;;
"B") # down
if [[ pos -lt ${max} ]]; then
_printItem
((pos += 1))
_mvDown
else # loop to top
_printItem
_mvTop
fi
_printItemBold
;;
"C") # right
echo "NEXT_PAGE" >"${outfile}"
break
;;
"D") # left
echo "PREV_PAGE" >"${outfile}"
break
;;
"") # enter, spacebar
[[ -f ${outfile} ]] &&
_dump "${items[pos]}" >"${outfile}"
break
;;
"q" | "Q" | "$'\e'") # quit (TODO: fix ESC key)
return 1
;;
"c") # clone repo
clear
repoName=$(_dump "${items[pos]}")
gh repo clone "${repoName}" &&
echo -e "\nsaved to '$(pwd)/\x1b[1m\x1b[38;5;10m$(echo ${items[pos]} | cut -d '/' -f 2)\x1b[0m'\n"
read -rsn1 -p "press any key to continue "
# same as enter/spacebar
[[ -f ${outfile} ]] &&
_dump "${items[pos]}" >"${outfile}"
break
;;
esac
done
}
_viewMultiplePages() {
maxHeight=$(tput lines)
pageCount=0
index=0
outfile=""
declare -a items=()
declare -a pages=()
# parse args
while [[ ${#*} -gt 0 ]]; do
case ${1} in
-o | --outfile) # 'props' again
shift
outfile="${1}"
shift
;;
*)
items+=($(echo "${1}" | tr ' ' '\\')) # escape spaces
shift
;;
esac
done
# split input into multiple page arrays
while true; do
[[ ${#items[@]} -eq 0 ]] && break
pages[pageCount]="${items[*]:0:${maxHeight}}"
pageCount=$((pageCount + 1))
items=(${items[@]:${maxHeight}})
done
# browse pages
while true; do
selection=""
declare -a currentPage=(${pages[index]})
# clear previous selection
echo "" >${outfile}
# render current page
_viewSinglePage "${currentPage[@]}" -o "${outfile}" &&
selection="$(cat "${outfile}")"
# show next page if exists
if [[ ${selection} == "NEXT_PAGE" ]]; then
plusOne=$((index + 1))
nextPage=(${pages[${plusOne}]})
[[ ${#nextPage[@]} -gt 0 ]] && index=${plusOne}
# show prev page if exists
elif [[ ${selection} == "PREV_PAGE" ]]; then
minusOne=$((index - 1))
[[ ${minusOne} -ge 0 ]] &&
prevPage=(${pages[${minusOne}]}) &&
[[ ${#prevPage[@]} -gt 0 ]] &&
index=${minusOne}
# enter, quit, clone...
else
[[ ${#selection} -gt 0 ]] &&
echo "loading ${selection}"
break
fi
done
}
_viewFileTree() {
selection=""
outfile=$(mktemp) # NOT props
# using two arrays for pathname and associated url,
# because bash doesn't do nested arrays.
urlNamesJSON=$(echo "${*}" | jq '.tree[]? | .url')
pathNamesJSON=$(echo "${*}" | jq '.tree[]? | .path')
declare -a urlNames="(${urlNamesJSON})"
declare -a pathNames="(${pathNamesJSON})"
# using `sub()` filter to replace spaces w backslashes
declare -a coloredPathNames=($(echo "${*}" | jq -r '
.tree[] |
if .type == "tree" then
"\\x1b[38;5;81m" + .path + "\\x1b[0m" | sub(" "; "\\"; "g")
elif .type == "blob" then
"\\x1b[38;5;163m" + .path + "\\x1b[0m" | sub(" "; "\\"; "g")
else
empty
end
'))
# repo is empty
[[ ${#pathNames[@]} -eq 0 ]] && return 1
# repo not empty; browse files
while true; do
_viewMultiplePages "${coloredPathNames[@]}" -o "${outfile}" &&
selection=$(cat "${outfile}" | tr '\\' ' ') # un escape spaces
# quit
[[ "${#selection}" -eq 0 ]] && break
# match selected path to corresponding url
for i in "${!pathNames[@]}"; do
if [[ "${pathNames[i]}" == "${selection}" ]]; then
fileUrl="${urlNames[i]}"
tempBuffer="/tmp/$(echo -n "${pathNames[i]}" | tr '/' '_')"
# fetch+decode file contents and open in editor
gh api "${fileUrl}" |
jq -r '.content? // .tree?' |
base64 -d >"${tempBuffer}" 2>/dev/null &&
tempBufferLength="$(cat ${tempBuffer})" &&
[[ ${#tempBufferLength} -gt 0 ]] &&
${EDITOR} "${tempBuffer}" ||
# not a file; view filetree in nested directory
_viewFileTree "$(gh api ${fileUrl})"
fi
done
done
}