Skip to content

Instantly share code, notes, and snippets.

@oktroco
Last active July 30, 2020 14:18
Show Gist options
  • Save oktroco/493dc13ae6d90f1f8c42f7a902ff26b6 to your computer and use it in GitHub Desktop.
Save oktroco/493dc13ae6d90f1f8c42f7a902ff26b6 to your computer and use it in GitHub Desktop.
shell select code
#!/bin/sh
ESC=`printf "\033"`;
input_key() {
read -s -n3 INPUT;
echo $INPUT;
}
check_selected() {
if [ $1 = $2 ];
then echo " => "
else echo " "
fi
}
select_menu() {
SELECTED=1;
INPUT="";
MIN_MENU=1;
MAX_MENU=$#;
while true;
do
for (( i=1; i<=$#; i++))
do
printf "$ESC[2K$(check_selected $i $SELECTED) $i. ${!i}\n";
done
printf "\n$ESC[2KUse Arrow key to select and input Enter to select\n$ESC[2K(selected : ${!SELECTED})\n";
INPUT=$(input_key);
if [[ $INPUT = "" ]];
then break;
fi
if [[ $INPUT = $ESC[A ]];
then SELECTED=$(expr $SELECTED - 1);
elif [[ $INPUT = $ESC[B ]];
then SELECTED=$(expr $SELECTED + 1);
fi
if [[ $SELECTED -lt $MIN_MENU ]];
then SELECTED=${MIN_MENU};
elif [[ $SELECTED -gt $MAX_MENU ]];
then SELECTED=${MAX_MENU};
fi
printf "$ESC[$(expr $# + 3)A";
done
return `expr ${SELECTED} - 1`;
}
select_test() {
arr_params=("value1" "value2" "value3");
echo "Select Value";
select_menu "${arr_params[@]}";
local SELECTED=$?;
SELECTED_MODE=${arr_params[${SELECTED}]};
}
main() {
select_test;
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment