diff options
author | nea <romangraef@gmail.com> | 2022-01-13 00:01:56 +0100 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-01-13 00:01:56 +0100 |
commit | d9031203e22e4ef381c482b4594521323647487e (patch) | |
tree | 1ea7dcfb53840a42f10d19cf25317138e497911a /choose | |
download | scripts-d9031203e22e4ef381c482b4594521323647487e.tar.gz scripts-d9031203e22e4ef381c482b4594521323647487e.tar.bz2 scripts-d9031203e22e4ef381c482b4594521323647487e.zip |
Initial commit
Diffstat (limited to 'choose')
-rwxr-xr-x | choose | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -0,0 +1,63 @@ +#!/bin/bash + +if [ "a$1" == "a--help" ] || [ "a$1" == "a-h" ]; then + cat <<EOF +Usage: $0 [-i] \`tty\` "Option A" "Option B" +Displays the user in the provided tty all the options and lets the user select an option with their keys. + +Prints the selected option (or the (0-based) index of that option with the -i flag) to the default stdout. + +Exits with code 1 if the user aborted the selection with q. +EOF +fi + +printidx=0 +if [ "a$1" == "a-i" ]; then + printidx=1 + shift +fi + +tty="$1" +shift +options=("${@}") +optionslength=${#options[@]} +sel=0 +printf "\e[s" >"$tty" +while true; do + printf "\e[u\e[s\e[K" >"$tty" + for ((i = 0; i < optionslength; i++)); do + if [ "$i" -eq $sel ]; then + printf "\e[30;47m" >"$tty" + fi + printf "%s\e[40;37m " "${options[$i]}" >"$tty" + done + case "$(./readkey <"$tty")" in + left) + sel=$((sel - 1)) + ;; + right) + sel=$((sel + 1)) + ;; + q) + printf "\e[u\e[KSelection aborted\n" >"$tty" + exit 1 + ;; + enter) + break + ;; + esac + if ((sel < 0)); then + sel=0 + fi + if ((sel >= optionslength)); then + sel=$((optionslength - 1)) + fi + +done +printf "\e[u\e[KSelected %s\n" "${options[$sel]}" >"$tty" + +if [ "$printidx" -eq 1 ]; then + echo "$sel" +else + echo "${options[$sel]}" +fi |