Closed closedLoop closed 1 month ago
This is the best example of getting the dropdown menu working, however I cannot get the command to populate as READLINE_LINE
doesn't appear to update the text around the cursor
#!/bin/bash
# Function to capture key presses
get_key() {
IFS= read -rsn1 key 2>/dev/null >&2
if [[ $key == $'\x1b' ]]; then
read -rsn2 key
if [[ $key == [A ]]; then echo up; fi
if [[ $key == [B ]]; then echo down; fi
fi
}
# Function to display the menu
show_menu() {
echo "Choose an option:"
for i in "${!options[@]}"; do
if [[ $i == $selected ]]; then
echo -e "\e[1;32m> ${options[i]}\e[0m"
else
echo " ${options[i]}"
fi
done
}
# Options array
options=("ls" "git push" "htop" "echo Hello, World!")
# Initial selected option
selected=0
# Save the current cursor position
tput sc
# Infinite loop to handle user input
while true; do
# Restore the cursor position
tput rc
tput ed
show_menu
key=$(get_key)
case $key in
up)
((selected--))
if ((selected < 0)); then
selected=$((${#options[@]} - 1))
fi
;;
down)
((selected++))
if ((selected >= ${#options[@]})); then
selected=0
fi
;;
"")
break
;;
esac
done
# Send the selected command to the terminal input buffer
selected_command="${options[selected]}"
printf "\r\033[K%s" "$selected_command"
stty -echo # Turn off terminal echo
read -r -d '' -s -n 1 -p "$selected_command" # Read the input silently
stty echo # Turn terminal echo back on
eval "$selected_command"
get currently selected line contents or clipboard?
Closing this and moving above to #1
Feedback from my Reddit post: