From a08e15af13b9cc43cbbacc3e25bbaa75d1e52093 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 27 Oct 2015 14:39:22 -0400 Subject: cli_show_menu(): New function --- diff --git a/src/ui/cli.sh b/src/ui/cli.sh index a396625..a8af8dd 100644 --- a/src/ui/cli.sh +++ b/src/ui/cli.sh @@ -46,3 +46,40 @@ cli_err() printf "Error: ${fmt}\n" "${@}" >&2 } + +cli_show_menu() +{ + local title="${1}" + shift 1 + local i= + local l= + local label= + + printf '%s\n' "${title}" >"$(tty)" + { printf '%s\n' "${title}" | sed 's/./-/g'; } >"$(tty)" + + while :; do + printf '\n' >"$(tty)" + i=0 + l=${#} + for label in "${@}"; do + i=$(($i + 1)) + printf ' %4d: %s\n' "${i}" "${label}" >"$(tty)" + done + printf '\n> ' >"$(tty)" + if ! read -r i <"$(tty)"; then + continue + fi + case "${i}" in + *[!0-9]* | '') + continue + ;; + esac + if [ ${i} -le 0 ] || [ ${i} -gt ${l} ]; then + continue + fi + i=$(($i - 1)) + printf '%d' "${i}" + return 0 + done +} -- cgit v0.9.1