Skip to content

Instantly share code, notes, and snippets.

@Hayao0819
Last active November 1, 2022 01:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hayao0819/d7554f55ccd84a53b5973764497d5714 to your computer and use it in GitHub Desktop.
Save Hayao0819/d7554f55ccd84a53b5973764497d5714 to your computer and use it in GitHub Desktop.
インタラクティブな選択メニュー
#!/usr/bin/env bash
CaptureSpecialKeys(){
local SELECTION rest
IFS= read -r -n1 -s SELECTION
#echo "$SELECTION" | hexdump >&2
if [[ $SELECTION == $'\x1b' ]]; then
read -r -n2 -s rest
SELECTION+="$rest"
else
if [[ "$SELECTION" == '' ]] ;then
echo "Enter"
return 0
else
read -r rest
echo "$SELECTION$rest"
return 0
fi
fi
case $SELECTION in
$'\x1b\x5b\x41') #up arrow
echo "Up"
;;
$'\x1b\x5b\x42') #down arrow
echo "Down"
;;
$'\x1b\x5b\x43') #right arrow
echo "Right"
;;
$'\x1b\x5b\x44') #left arrow
echo "Left"
;;
$'\x20') #space
echo "Space"
;;
esac
}
ClearLine(){ printf "\033[2K"; }
MoveCursorUp(){ printf "\033[%dA" "$1"; }
ResetStyle(){ printf "\033[0m"; }
ClearUpperLines(){
# shellcheck disable=SC2034
for i in $(seq 1 "$1"); do
MoveCursorUp 1
ClearLine
done
}
TextUnderLine(){ printf "\033[4m" ; }
TextBold(){ printf "\033[1m" ; }
#----- ここから処理 -----
Choices=(HOGE FUGA PIYO)
CurrentChoice=0
Key=""
ShowMenu(){
for i in "${!Choices[@]}"; do
if [[ "$i" = "$CurrentChoice" ]]; then
TextUnderLine
TextBold
echo " * $i: ${Choices[$i]}"
else
echo " $i: ${Choices[$i]}"
fi
ResetStyle
done
}
UpdateMenuScreen(){
ClearUpperLines "${#Choices[@]}"
ShowMenu
}
ShowMenu
while [[ "$Key" != "Enter" ]]; do
Key="$(CaptureSpecialKeys)"
case "$Key" in
Up)
if (( "$CurrentChoice" != 0 )); then
CurrentChoice=$((CurrentChoice - 1))
UpdateMenuScreen
fi
;;
Down)
if (( "$CurrentChoice" != "${#Choices[@]}" - 1 )); then
CurrentChoice=$((CurrentChoice + 1))
UpdateMenuScreen
fi
;;
esac
done
echo "選択されたのは${Choices[$CurrentChoice]}です"
#!/usr/bin/env bash
Choices=(HOGE FUGA PIYO)
ClearScreen(){
printf "\033[2J"
}
ClearRight(){
printf "\033[0K"
}
ClearLeft(){
printf "\033[1K"
}
ClearLine(){
printf "\033[2K"
}
MoveCursor(){
printf "\033[%d;%dH" "$1" "$2"
}
MoveCursorUp(){
printf "\033[%dA" "$1"
}
MoveCursorDown(){
printf "\033[%dB" "$1"
}
MoveCursorRight(){
printf "\033[%dC" "$1"
}
MoveCursorLeft(){
printf "\033[%dD" "$1"
}
SaveCursor(){
printf "\033[s"
}
ResetStyle(){
printf "\033[0m"
}
CurrentChoice=0
ShowMenu(){
for i in "${!Choices[@]}"; do
if [ $i -eq $CurrentChoice ]; then
printf "\033[7m"
fi
echo " $i: ${Choices[$i]}"
ResetStyle
done
}
UpdateMenuScreen(){
MoveCursorUp 1
ClearLine
for i in "${!Choices[@]}"; do
MoveCursorUp 1
ClearLine
done
ShowMenu
}
ShowMenu
while IFS= read -r -n1 -s SELECTION && [[ -n "$SELECTION" ]]; do
if [[ $SELECTION == $'\x1b' ]]; then
read -r -n2 -s rest
SELECTION+="$rest"
fi
case $SELECTION in
$'\x1b\x5b\x41') #up arrow
echo "上"
if [ $CurrentChoice -gt 0 ]; then
CurrentChoice=$((CurrentChoice-1))
fi
UpdateMenuScreen
;;
$'\x1b\x5b\x42') #down arrow
echo "下"
if [ $CurrentChoice -lt $((${#Choices[@]}-1)) ]; then
CurrentChoice=$((CurrentChoice+1))
fi
UpdateMenuScreen
;;
$'\x1b\x5b\x43') #right arrow
echo "右"
;;
$'\x1b\x5b\x44') #left arrow
echo "左"
;;
$'\x20') #space
echo "スペース"
;;
esac
done
echo "選択されたのは${Choices[$CurrentChoice]}です"
#!/usr/bin/env bash
CaptureSpecialKeys(){
local SELECTION rest
IFS= read -r -n1 -s SELECTION
#echo "$SELECTION" | hexdump >&2
if [[ $SELECTION == $'\x1b' ]]; then
read -r -n2 -s rest
SELECTION+="$rest"
else
if [[ "$SELECTION" == '' ]] ;then
echo "Enter"
return 0
else
read -r rest
echo "$SELECTION$rest"
return 0
fi
fi
case $SELECTION in
$'\x1b\x5b\x41') #up arrow
echo "Up"
;;
$'\x1b\x5b\x42') #down arrow
echo "Down"
;;
$'\x1b\x5b\x43') #right arrow
echo "Right"
;;
$'\x1b\x5b\x44') #left arrow
echo "Left"
;;
$'\x20') #space
echo "Space"
;;
esac
}
ClearScreen(){
printf "\033[2J"
}
ClearRight(){
printf "\033[0K"
}
ClearLeft(){
printf "\033[1K"
}
ClearLine(){
printf "\033[2K"
}
MoveCursor(){
printf "\033[%d;%dH" "$1" "$2"
}
MoveCursorUp(){
printf "\033[%dA" "$1"
}
MoveCursorDown(){
printf "\033[%dB" "$1"
}
MoveCursorRight(){
printf "\033[%dC" "$1"
}
MoveCursorLeft(){
printf "\033[%dD" "$1"
}
SaveCursor(){
printf "\033[s"
}
ResetStyle(){
printf "\033[0m"
}
ClearUpperLines(){
# shellcheck disable=SC2034
for i in $(seq 1 "$1"); do
MoveCursorUp 1
ClearLine
done
}
#----- ここから処理 -----
Choices=(HOGE FUGA PIYO)
CurrentChoice=0
Key=""
ShowMenu(){
for i in "${!Choices[@]}"; do
if [[ "$i" = "$CurrentChoice" ]]; then
echo " * $i: ${Choices[$i]}"
else
echo " $i: ${Choices[$i]}"
fi
done
}
UpdateMenuScreen(){
ClearUpperLines "${#Choices[@]}"
ShowMenu
}
ShowMenu
while [[ -z "$Key" ]]; do
Key="$(CaptureSpecialKeys)"
case "$Key" in
Up)
if (( "$CurrentChoice" != 0 )); then
CurrentChoice=$((CurrentChoice - 1))
UpdateMenuScreen
fi
;;
Down)
if (( "$CurrentChoice" != "${#Choices[@]}" - 1 )); then
CurrentChoice=$((CurrentChoice + 1))
UpdateMenuScreen
fi
;;
Enter)
break
;;
esac
Key=""
done
echo "選択されたのは${Choices[$CurrentChoice]}です"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment