#!/bin/bash
tput civis
echo -e "\033[6n"
read -sdR CURPOS
CURPOS=${CURPOS#*[}
ROW=${CURPOS%;*}
LINE=`tput lines`
options=("$@")
max="${#}"
current=0
update=1
for (( i=0 ; i<max ; i++ ));do
selected[${i}]=false
done
function keyboard(){
update=1
IFS= read -r -sn1 t
if [[ $t == A ]]; then
[[ "$current" == "0" ]] || current=$((current - 1))
elif [[ $t == B ]]; then
[[ "$current" == "$1" ]] || current=$((current + 1))
elif [[ $t == " " ]];then
[[ "${selected[${current}]}" == false ]] && selected[${current}]=true || selected[${current}]=false
elif [[ $t == "" ]];then
return 1
else
update=0
fi
}
function display(){
if [ $(($ROW+$max)) -le $LINE ];then
tput cup $ROW 0
fi
for (( i=0 ; i<max ; i++ )); do
if [[ ${current} == "${i}" && ${selected[${i}]} == true ]];then
printf "\e[0;90m[\e[0;92m*\e[0;90m] \e[0m\e[0;92m%s\e[0m\n" "${options[$i]}"
elif [[ ${current} == "${i}" && ${selected[${i}]} == false ]];then
printf "\e[0;90m[ ] \e[0m\e[0;92m%s\e[0m\n" "${options[$i]}"
elif [[ ${selected[${i}]} == true ]];then
printf "\e[0;90m[\e[0;92m*\e[0;90m] \e[0m\e[1;77m%s\e[0m\n" "${options[$i]}"
elif [[ ${selected[${i}]} == false ]];then
printf "\e[0;90m[ ] \e[0m\e[1;77m%s\e[0m\n" "${options[$i]}"
fi
done
if [ $(($ROW+$max)) -gt $LINE ];then
for (( i=0 ; i<$max ; i++ )); do
tput cuu1
done
fi
}
while true; do
if [[ $update -eq 1 ]];then
display "$@"
fi
keyboard $((max-1))
if [[ $? -eq 1 ]]; then
no_selected=true
for (( i=0 ; i<max ; i++ )); do
if [[ ${selected[$i]} == true ]]; then
no_selected=false
fi
done
if [ $no_selected == true ]; then
continue
fi
if [ $(($ROW+$max)) -gt $LINE ];then
tput cup $ROW 0
tput cnorm
fi
break
fi
done
current=-1
export selected # used in lupa_envsetup.sh
export max # used in lupa_envsetup.sh