Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Created July 12, 2018 20:23
Show Gist options
  • Save stephenmm/0f77bc92b850cef8b810cc3766c42bf4 to your computer and use it in GitHub Desktop.
Save stephenmm/0f77bc92b850cef8b810cc3766c42bf4 to your computer and use it in GitHub Desktop.
Interactive and visual selection of array with arrows or 'j' 'k' keys. Can also edit (with autocomplete) the selection
#!/usr/bin/env bash
# Started from this code: https://unix.stackexchange.com/a/415155/14014
# Renders a text based list of options that can be selected by the
# user using up, down, 'j', 'k', and enter keys and returns the
# string in provided file.
# 'e' to edit the current string
# 'q' to quit
#
# Arguments : list of options, maximum of 256
# "file_to_return_string_in.txt" "opt1" "opt2" ...
# Return value:
# Ex:
# files=( $( ls -at -1 /sims/$PROJ/results/$USER/*.svcf ) )
# return_val=$( mktemp )
# select_option $return_val "${files[@]}"
# echo "Returned str = " $( cat $return_val )
function select_option {
return_val=$1 && shift
args=( $@ )
# little helpers for terminal print control and key input
ESC=$( printf "\033")
cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; }
print_option() { printf " $1 "; }
print_selected() { printf " $ESC[7m $1 $ESC[27m"; }
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
key_input() { read -r -s -n1 key 2>/dev/null >&2
if [[ $key = $ESC ]]; then
read -r -s -n1 key 2>/dev/null >&2
if [[ $key = [ ]]; then
read -r -s -n1 key 2>/dev/null >&2
if [[ $key = A ]]; then echo up; fi
if [[ $key = B ]]; then echo down; fi
fi
fi
if [[ $key = k ]]; then echo up; fi
if [[ $key = j ]]; then echo down; fi
if [[ $key = "" ]]; then echo enter; fi
if [[ $key = e ]]; then echo edit; fi
if [[ $key = q ]]; then echo quit; fi
}
# initially print empty new lines (scroll down if at bottom of screen)
for opt; do printf "\n"; done
# determine current screen position for overwriting the options
local lastrow=`get_cursor_row`
local startrow=$(($lastrow - $#))
# ensure cursor and input echoing back on upon a ctrl+c during read -s
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
cursor_blink_off
local edit=''
local selected=0
while true; do
# print options by overwriting the last lines
local idx=0
for opt; do
cursor_to $(($startrow + $idx))
if [ $idx -eq $selected ]; then
print_selected "$opt"
else
print_option "$opt"
fi
((idx++))
done
# user key control
case `key_input` in
enter) break;;
quit) return 0;;
edit) edit='true' && break;;
up) ((selected--));
if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi;;
down) ((selected++));
if [ $selected -ge $# ]; then selected=0; fi;;
esac
done
# cursor position back to normal
cursor_to $lastrow
printf "\n"
cursor_blink_on
output="${args[$selected]}"
if [[ $edit == true ]]; then
read -e -p "Edit selection: " -i $output output
fi
echo "$output" >> $return_val
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment