Last active
March 25, 2022 12:11
-
-
Save gnowland/f71d92e4590ee3eebe71301a04cd0add to your computer and use it in GitHub Desktop.
interactive semver target script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Portions of this script are Copyright (C) 2017 Ingo Hollmann - All Rights Reserved | |
# Permission to copy and modify is granted under the Creative Commons Attribution 4.0 license | |
# https://www.bughunter2k.de/blog/cursor-controlled-selectmenu-in-bash | |
# Last revised 2020-11-12 by Gifford Nolwland <[email protected]> | |
# Call this file as so: `./select.sh "${ARRAY[@]}"` | |
# per https://stackoverflow.com/a/36808100/2764290 | |
ESC=$(printf "\e") | |
CURR=0 | |
declare -a MENU=("$@") | |
draw_menu() { | |
for i in "${MENU[@]}"; do | |
if [[ ${MENU[$CURR]} == $i ]]; then | |
tput setaf 2; echo " > $i"; tput sgr0 | |
else | |
echo " $i"; | |
fi | |
done | |
} | |
clear_menu() { | |
for i in "${MENU[@]}"; do tput cuu1; done | |
tput ed | |
} | |
# Draw initial menu | |
draw_menu | |
while read -rs -n1 key; do # 1 char (not delimiter), silent | |
# empty echo simulates <enter> which loads the input into the variable (due to lack of automatic echo including new line from read -s) | |
# see: https://www.tutorialkart.com/bash-shell-scripting/bash-read-username-and-password/ | |
# echo | |
# echo $key | |
if [[ "$key" == "" ]]; then break; fi # accept choice on enter or space | |
# match input keys | |
# see: https://stackoverflow.com/questions/10679188/casing-arrow-keys-in-bash/11759139 | |
if [[ "$key" == $ESC ]]; then # an escape sequence was pressed | |
read -rs -n2 -t1 key; # read 2 more characters, silent. Abort if no characters read after 1 second (mac minimum) | |
case "$key" in | |
'[A'|'0A'|'[D'|'0D') # cursor up or left: previous item | |
((CURR > 0)) && ((CURR--)) | |
;; | |
'[B'|'0B'|'[C'|'0C') # cursor down or right: next item | |
((CURR < ${#MENU[@]}-1)) && ((CURR++)) | |
;; | |
$'[1~'|$'[H'|$'0H') # home: first item | |
((CURR=0)) | |
;; | |
$'[4~'|$'[F'|$'0F') # end: last item | |
((CURR=${#MENU[@]}-1)) | |
;; | |
*) # esc key pressed, abort | |
clear_menu | |
echo "Aborted!" | |
exit 1 | |
;; | |
esac | |
fi | |
# Redraw menu | |
clear_menu | |
draw_menu | |
done | |
# echo "Selected item $CURR: [${MENU[$CURR]}] \"${MENU[$CURR]}\""; | |
# echo ${MENU[$CURR]} > /tmp/semver # send out | |
clear_menu | |
tput setaf 2; echo " > ${MENU[$CURR]}"; tput sgr0 | |
echo # add a new line after final output | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment