Skip to content

Instantly share code, notes, and snippets.

@kumailxp
Created November 5, 2021 13:22
Show Gist options
  • Save kumailxp/23de98df761e744d96d1daab9a40567a to your computer and use it in GitHub Desktop.
Save kumailxp/23de98df761e744d96d1daab9a40567a to your computer and use it in GitHub Desktop.
menu control with bash
#! /bin/bash
NC='\033[0m' # No Color
L_GREEN='\033[1;32m'
L_RED='\033[1;31m'
B_YELLOW='\033[1;39;43m'
# move one key up: \r\e[1A
menu=("Line 1" "Line 2" "Line 3" "Line 4" "Line 5")
highlight_menu=("0" "0" "0" "0" "0")
for i in $(seq 0 $(( ${#menu[@]} - 1)))
do
echo "${menu[$i]}"
done
cursor_pos=${#menu[@]}
while true
do
read -s -n 1 -t 0.1 input # so read doesn't hang
if [[ $input == "w" && $cursor_pos -gt 0 ]]; then
if [[ ${highlight_menu[$cursor_pos]} == "0" ]]; then
echo -ne "\r$NC${menu[$cursor_pos]}$NC"
else
echo -ne "\r$B_YELLOW${menu[$cursor_pos]}$NC"
fi
echo -ne "\r\e[1A"
cursor_pos=$(( $cursor_pos - 1))
if [[ ${highlight_menu[$cursor_pos]} == "1" ]]; then
echo -ne "\r$B_YELLOW$L_RED${menu[$cursor_pos]}$NC"
else
echo -ne "\r$NC$L_RED${menu[$cursor_pos]}$NC"
fi
fi
if [[ $input == "s" && $cursor_pos -lt ${#menu[@]} ]]; then
if [[ ${highlight_menu[$cursor_pos]} == "0" ]]; then
echo -ne "\r$NC${menu[$cursor_pos]}$NC"
else
echo -ne "\r$B_YELLOW${menu[$cursor_pos]}$NC"
fi
echo -ne "\r\e[1B"
cursor_pos=$(( $cursor_pos + 1))
if [[ ${highlight_menu[$cursor_pos]} == "1" ]]; then
echo -ne "$B_YELLOW$L_RED${menu[$cursor_pos]}$NC"
else
echo -ne "$NC$L_RED${menu[$cursor_pos]}$NC"
fi
fi
if [[ $input = "f" ]]; then
echo "${menu[$cursor_pos]}" > /tmp/session.output
fi
if [[ $input = "q" ]]; then
rm -f /tmp/session.output
pkill screen
fi
if [[ $input = "1" ]]; then
if [[ ${highlight_menu[$cursor_pos]} == "0" ]]; then
echo -ne "\r$B_YELLOW${menu[$cursor_pos]}$NC"
highlight_menu[$cursor_pos]="1"
else
echo -ne "\r$NC${menu[$cursor_pos]}$NC"
highlight_menu[$cursor_pos]="0"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment