-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
344 lines (289 loc) · 7.17 KB
/
run.sh
File metadata and controls
344 lines (289 loc) · 7.17 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env bash
set -euo pipefail
# Pure-bash, low-flicker TUI runner for scripts hosted on GitHub Pages.
# Requirements: bash, curl, tput (optional), stty
# Designed to behave safely over SSH (always restores terminal state).
BASE_URL="${BASE_URL:-https://arcooon.github.io}"
SCRIPTS_LIST_URL="${SCRIPTS_LIST_URL:-$BASE_URL/scripts.txt}"
SAFE_MODE="${SAFE_MODE:-0}" # 1 = do not execute
TTY="/dev/tty" # Always read/write to the real terminal
# ---------- Terminal helpers ----------
enter_alt() { tput smcup 2>/dev/null || true; }
exit_alt() { tput rmcup 2>/dev/null || true; }
hide_cursor(){ tput civis 2>/dev/null || printf '\033[?25l'; }
show_cursor(){ tput cnorm 2>/dev/null || printf '\033[?25h'; }
rev() { tput rev 2>/dev/null || printf '\033[7m'; }
norm() { tput sgr0 2>/dev/null || printf '\033[0m'; }
clear_eos() { tput ed 2>/dev/null || printf '\033[J'; }
move() { tput cup "$1" "$2" 2>/dev/null || printf '\033[%d;%dH' "$(( $1 + 1 ))" "$(( $2 + 1 ))"; }
lines() { tput lines 2>/dev/null || echo 24; }
STTY_OLD=""
ALT_ACTIVE=0
term_setup() {
[[ -r "$TTY" ]] || { echo "No TTY available at $TTY"; exit 1; }
# Save current tty settings
STTY_OLD="$(stty -g <"$TTY" 2>/dev/null || true)"
# Raw-ish mode: no echo, no canonical buffering, non-blocking reads
stty -echo -icanon time 0 min 0 <"$TTY" 2>/dev/null || true
# Alternate screen reduces scrollback spam / flicker
enter_alt
ALT_ACTIVE=1
hide_cursor
}
term_restore() {
# Always attempt to restore sane settings FIRST
norm
show_cursor
if (( ALT_ACTIVE == 1 )); then
exit_alt
ALT_ACTIVE=0
fi
if [[ -n "${STTY_OLD:-}" ]]; then
stty "$STTY_OLD" <"$TTY" 2>/dev/null || stty sane <"$TTY" 2>/dev/null || true
else
stty sane <"$TTY" 2>/dev/null || true
fi
# Put cursor at a clean spot
printf '\033[0m\033[?25h\033[H\033[J' >"$TTY" 2>/dev/null || true
}
cleanup() {
# Called on any exit path
term_restore
}
# Ensure cleanup runs on normal exit and most signals
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
trap 'exit 129' HUP
# ---------- Data ----------
ITEMS=()
COUNT=0
fetch_list() {
curl -fsSL "$SCRIPTS_LIST_URL"
}
load_items() {
local list
list="$(fetch_list || true)"
if [[ -z "${list//[[:space:]]/}" ]]; then
# restore terminal so the error is readable
term_restore
echo "No scripts found."
echo "Expected list at: $SCRIPTS_LIST_URL"
exit 1
fi
# Read lines safely (preserve spaces, no word-splitting hell)
ITEMS=()
while IFS= read -r line; do
[[ -z "${line//[[:space:]]/}" ]] && continue
ITEMS+=("$line")
done <<<"$list"
COUNT="${#ITEMS[@]}"
if (( COUNT == 0 )); then
term_restore
echo "No scripts found in list."
exit 1
fi
}
# ---------- Input ----------
# Returns: up, down, pgup, pgdn, home, end, enter, quit, refresh, other
read_key() {
local k rest
IFS= read -rsn1 k <"$TTY" || true
[[ -z "${k:-}" ]] && { echo other; return 0; }
if [[ "$k" == $'\x1b' ]]; then
IFS= read -rsn1 rest <"$TTY" || rest=""
[[ "$rest" != "[" ]] && { echo other; return 0; }
IFS= read -rsn1 rest <"$TTY" || rest=""
case "$rest" in
A) echo up ;;
B) echo down ;;
H) echo home ;;
F) echo end ;;
5) IFS= read -rsn1 _ <"$TTY" || true; echo pgup ;; # ~
6) IFS= read -rsn1 _ <"$TTY" || true; echo pgdn ;; # ~
*) echo other ;;
esac
return 0
fi
case "$k" in
q|Q) echo quit ;;
r|R) echo refresh ;;
j) echo down ;;
k) echo up ;;
g) echo home ;;
G) echo end ;;
$'\x0a'|$'\x0d') echo enter ;;
*) echo other ;;
esac
}
# ---------- UI state ----------
IDX=0
TOP=0
HEADER_LINES=6
FOOTER_LINES=2
LIST_START_ROW=0
LIST_ROWS=0
recalc_layout() {
local h
h="$(lines)"
LIST_START_ROW=$HEADER_LINES
LIST_ROWS=$(( h - HEADER_LINES - FOOTER_LINES ))
(( LIST_ROWS < 3 )) && LIST_ROWS=3
}
clamp_viewport() {
(( COUNT == 0 )) && return 0
(( IDX < 0 )) && IDX=0
(( IDX >= COUNT )) && IDX=$(( COUNT - 1 ))
if (( IDX < TOP )); then TOP=$IDX; fi
if (( IDX >= TOP + LIST_ROWS )); then TOP=$(( IDX - LIST_ROWS + 1 )); fi
(( TOP < 0 )) && TOP=0
local max_top=$(( COUNT - LIST_ROWS ))
(( max_top < 0 )) && max_top=0
(( TOP > max_top )) && TOP=$max_top
}
draw_header() {
move 0 0
clear_eos
echo "Remote Scripts (pure bash, low flicker)"
echo "Source: $SCRIPTS_LIST_URL"
echo "Controls: Up/Down or j/k, Enter=run, r=refresh, q=quit"
echo "Nav: PgUp/PgDn, Home/End"
echo "SAFE_MODE=$SAFE_MODE"
echo
}
draw_footer() {
local h
h="$(lines)"
move $(( h - 2 )) 0
clear_eos
echo "Selected: ${ITEMS[IDX]:-(none)}"
echo "Tip: If arrows act up, use j/k."
}
draw_list() {
recalc_layout
clamp_viewport
move "$LIST_START_ROW" 0
clear_eos
local i row item
for (( i=0; i<LIST_ROWS; i++ )); do
row=$(( TOP + i ))
(( row >= COUNT )) && break
item="${ITEMS[row]}"
if (( row == IDX )); then
rev
printf "> %s\n" "$item"
norm
else
printf " %s\n" "$item"
fi
done
}
redraw() {
draw_header
draw_list
draw_footer
}
confirm_run() {
(( COUNT == 0 )) && return 0
local path url confirm
path="${ITEMS[IDX]}"
url="$BASE_URL$path"
# Temporarily restore line input for prompt
stty echo icanon <"$TTY" 2>/dev/null || true
move $(( LIST_START_ROW + LIST_ROWS + 1 )) 0
clear_eos
echo "Selected: $path"
echo "URL: $url"
echo
read -r -p "Run it now? (y/N) " confirm <"$TTY" || confirm=""
# Back to raw-ish mode
stty -echo -icanon time 0 min 0 <"$TTY" 2>/dev/null || true
confirm="${confirm,,}"
[[ "$confirm" != "y" ]] && return 1
if [[ "$SAFE_MODE" == "1" ]]; then
move $(( LIST_START_ROW + LIST_ROWS + 1 )) 0
clear_eos
echo "SAFE_MODE=1: not executing."
echo "Press any key to return..."
read -rsn1 _ <"$TTY" || true
return 0
fi
# Run it in normal screen so output is readable
term_restore
echo "Running: $url"
echo
bash <(curl -fsSL "$url")
echo
read -r -p "Done. Press Enter to return..." _ <"$TTY" || true
# Back to TUI
term_setup
return 0
}
wrap_down() {
(( COUNT == 0 )) && return 0
(( IDX++ ))
if (( IDX >= COUNT )); then IDX=0; fi
}
wrap_up() {
(( COUNT == 0 )) && return 0
(( IDX-- ))
if (( IDX < 0 )); then IDX=$(( COUNT - 1 )); fi
}
main() {
load_items
term_setup
redraw
while true; do
case "$(read_key)" in
up)
wrap_up
draw_list
draw_footer
;;
down)
wrap_down
draw_list
draw_footer
;;
pgup)
IDX=$(( IDX - LIST_ROWS ))
(( IDX < 0 )) && IDX=0
draw_list
draw_footer
;;
pgdn)
IDX=$(( IDX + LIST_ROWS ))
(( IDX >= COUNT )) && IDX=$(( COUNT - 1 ))
draw_list
draw_footer
;;
home)
IDX=0
draw_list
draw_footer
;;
end)
IDX=$(( COUNT - 1 ))
draw_list
draw_footer
;;
refresh)
load_items
(( IDX >= COUNT )) && IDX=$(( COUNT - 1 ))
TOP=0
redraw
;;
enter)
confirm_run || true
redraw
;;
quit)
exit 0
;;
*)
sleep 0.02
;;
esac
done
}
main