#!/usr/bin/env bash # ============================================================================= # tui.sh — Browse, search and run the get.rso.dev and oneline.sh catalogs # from a terminal, in one pure-bash file you can read before running. # Usage: curl -sL get.rso.dev/tui | bash # wget -qO- get.rso.dev/tui | bash # bash tui.sh [--source=get|oneline|all] [--help] # ============================================================================= # @description Interactive terminal catalog of every get.rso.dev and oneline.sh snippet # @category System Utilities # @tags tui, catalog, menu, search, browse, oneline, interactive # @supported Ubuntu, Debian, Mint, RHEL, Rocky, Amazon Linux, Fedora, Arch, macOS # @verify true # @safety idempotent # ============================================================================= # # How it works, in reading order: # 1. fetch_sources downloads catalog.md and README.md (curl or wget). # 2. parse_catalog turns the markdown into one record per entry (awk). # 3. main_loop draws three panes and reacts to keys read from /dev/tty. # 4. run_selected shows the exact command, waits for "y", then runs it. # Nothing is executed without that confirmation, nothing is written to disk # except a temp dir that is removed on exit. # # Environment overrides (URLs or local file paths): # RSO_GET_URL default https://get.rso.dev/catalog.md # RSO_ONELINE_URL default https://oneline.sh/README.md # RSO_SOURCE get | oneline | all (initial filter) # RSO_ASCII=1 ASCII borders instead of box drawing characters # NO_COLOR=1 no colours # # Requires bash >= 3.2, stty, awk and curl or wget. Everything lives in # functions and the last line calls main, so it is safe with `curl | bash`. DEFAULT_SOURCE="get" RSO_GET_URL="${RSO_GET_URL:-https://get.rso.dev/catalog.md}" RSO_ONELINE_URL="${RSO_ONELINE_URL:-https://oneline.sh/README.md}" # Field separators used inside records (ASCII control chars, never in markdown) E=$'\033' # escape FS_REC=$'\x1c' # between fields NL=$'\x1f' # newline inside a field VS=$'\x1e' # between command variants LS=$'\x1d' # between a variant's label and its command # --------------------------------------------------------------------------- # # Records: parallel arrays, one index per catalog entry # --------------------------------------------------------------------------- # R_SRC=() R_CAT=() R_TITLE=() R_DESC=() R_META=() R_BODY=() R_VARS=() R_HAY=() # UI state SRC_FILTER="all" # get | oneline | all QUERY="" # current search string SEARCHING=0 # 1 while typing in the search box FOCUS=1 # 0 = categories, 1 = items, 2 = preview CATS=() CAT_N=() # categories visible under the current filters CAT_SEL=0 CAT_TOP=0 VIS=() # record indexes visible in the item list SEL=0 TOP=0 VAR_SEL=0 # selected command variant of the current item PV_TOP=0 # preview scroll offset STATUS="" # one-shot message in the footer ROWS=24 COLS=80 TMP="" SAVED_STTY="" # --------------------------------------------------------------------------- # # Terminal helpers # --------------------------------------------------------------------------- # die() { printf 'tui: %s\n' "$*" >&2; exit 1; } usage() { cat <<'EOF' Interactive catalog for get.rso.dev and oneline.sh Usage: curl -sL get.rso.dev/tui | bash bash tui.sh [--source=get|oneline|all] Keys: up/down j/k move tab left/right switch pane / search esc clear search 1 2 0 get.rso.dev / oneline.sh / both enter run (asks first) n/N next/previous command variant e edit command, then run c copy command s read the script source in a pager (get.rso.dev entries) pgup/pgdn page g/G top/bottom ? help q quit EOF } term_size() { local s s=$(stty size <&4 2>/dev/null) || s="24 80" ROWS=${s% *} COLS=${s#* } [ "$ROWS" -ge 10 ] 2>/dev/null || ROWS=24 [ "$COLS" -ge 40 ] 2>/dev/null || COLS=80 } term_enter() { SAVED_STTY=$(stty -g <&4 2>/dev/null) stty -echo -icanon min 1 time 0 <&4 2>/dev/null # alternate screen, hide cursor, no autowrap printf '\033[?1049h\033[?25l\033[?7l' >&3 } term_leave() { printf '\033[?7h\033[?25h\033[0m\033[?1049l' >&3 [ -n "$SAVED_STTY" ] && stty "$SAVED_STTY" <&4 2>/dev/null } cleanup() { term_leave [ -n "$TMP" ] && rm -rf "$TMP" } setup_style() { if [ -n "${NO_COLOR:-}" ]; then C_RST="" C_BOR="" C_FOC="" C_TTL="" C_DIM="" C_SEL="" C_ACC="" C_KEY="" C_GET="" C_ONE="" C_WRN="" else C_RST=$'\033[0m' C_BOR=$'\033[38;5;240m' # idle border C_FOC=$'\033[38;5;75m' # focused border C_TTL=$'\033[1;38;5;255m' # titles C_DIM=$'\033[38;5;245m' # secondary text C_SEL=$'\033[48;5;24;38;5;255m' # selected row C_ACC=$'\033[38;5;214m' # accent (commands, search) C_KEY=$'\033[1;38;5;75m' # key hints C_GET=$'\033[38;5;114m' # get.rso.dev tag C_ONE=$'\033[38;5;177m' # oneline.sh tag C_WRN=$'\033[1;38;5;203m' # warnings fi # Box drawing only when the locale is UTF-8, otherwise lengths go wrong case "${LC_ALL:-${LC_CTYPE:-${LANG:-}}}" in *[Uu][Tt][Ff]-8*|*[Uu][Tt][Ff]8*) UTF=1 ;; *) UTF=0 ;; esac [ -n "${RSO_ASCII:-}" ] && UTF=0 if [ "$UTF" = 1 ]; then B_H="─" B_V="│" B_TL="╭" B_TR="╮" B_BL="╰" B_BR="╯" ELL="…" MARK="›" else B_H="-" B_V="|" B_TL="+" B_TR="+" B_BL="+" B_BR="+" ELL="~" MARK=">" fi SPACES="$(printf '%*s' 400 '')" HLINE="" local i for ((i = 0; i < 400; i++)); do HLINE+="$B_H"; done } # fit TEXT WIDTH -> FIT: TEXT cut or padded to exactly WIDTH characters fit() { local s=$1 w=$2 [ "$w" -le 0 ] && { FIT=""; return; } if [ "${#s}" -gt "$w" ]; then FIT="${s:0:$((w - 1))}$ELL" else FIT="$s${SPACES:0:$((w - ${#s}))}" fi } # --------------------------------------------------------------------------- # # Fetching and parsing # --------------------------------------------------------------------------- # fetch() { local src=$1 out=$2 if [ -f "$src" ]; then cat "$src" >"$out" elif command -v curl >/dev/null 2>&1; then curl -fsSL --max-time 20 "$src" -o "$out" elif command -v wget >/dev/null 2>&1; then wget -qO "$out" -T 20 "$src" else return 1 fi } # Markdown -> one line per entry: # src FS cat FS title FS desc FS meta FS body FS vars FS haystack # get.rso.dev: every command line in a code block is its own variant, and a # "# alt: ..." line becomes a variant too. Other comments label the next line. # oneline.sh: each code block is one variant, labelled by the text above it. parse_catalog() { local src=$1 file=$2 awk -v SRC="$src" -v FS_REC="$FS_REC" -v NL="$NL" -v VS="$VS" -v LS="$LS" ' function clean(s) { gsub(/\t/, " ", s); gsub(/\*\*/, "", s); gsub(/`/, "", s) return s } function link(s, out, m) { # [text](url) -> text (url) while (match(s, /\[[^]]*\]\([^)]*\)/)) { m = substr(s, RSTART, RLENGTH) sub(/^\[/, "", m); sub(/\]\(/, " (", m) s = substr(s, 1, RSTART - 1) m substr(s, RSTART + RLENGTH) } return s } function add(field, line) { return field == "" ? line : field NL line } function addvar(label, cmd) { if (cmd == "") return vars = (vars == "" ? "" : vars VS) label LS cmd } function flush( hay) { if (title != "") { if (desc == "") desc = "-" hay = tolower(title " " cat " " desc " " meta) gsub(NL, " ", hay) printf "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", SRC, FS_REC, cat, FS_REC, title, FS_REC, desc, FS_REC, meta, FS_REC, body, FS_REC, vars, FS_REC, hay } title = ""; desc = ""; meta = ""; body = ""; vars = ""; para = ""; label = "" } BEGIN { incode = 0; platform = ""; cat = "General" } /^```/ { if (!incode) { incode = 1; block = ""; label = ""; next } incode = 0 if (title != "" && SRC == "oneline") addvar(para == "" ? "command" : para, block) if (title != "") body = add(body, "") next } incode { if (title == "") next line = $0; gsub(/\t/, " ", line) if (SRC == "oneline") { block = add(block, line); body = add(body, " " line); next } if (line ~ /^[ ]*$/) { label = ""; next } if (line ~ /^# alt: /) { sub(/^# alt: /, "", line); addvar("alt" (label == "" ? "" : ": " label), line); next } if (line ~ /^#/) { sub(/^#[ ]*/, "", line); label = line; next } addvar(label == "" ? "default" : label, line) next } /^# / { flush(); platform = substr($0, 3) if (SRC == "oneline") platform = "" next } /^## / { flush(); cat = substr($0, 4) if (platform != "" && platform != "Linux") cat = platform " / " cat next } /^###+ / { flush(); title = $0; sub(/^#+ /, "", title); next } title == "" { next } /^