Browse Source

menu works again

main
deicidus 2 years ago
parent
commit
f5e8b091e4
  1. 47
      spells/cantrips/menu

47
spells/cantrips/menu

@ -19,14 +19,14 @@ description=$1
shift shift
# Parse args. Each arg is "name%command" # Parse args. Each arg is "name%command"
items="" declare -a items
commands="" # declare -a descriptions # not displayed so presently disabled
declare -a commands
while [ ${#} -gt 0 ]; do while [ ${#} -gt 0 ]; do
IFS='%' read -r item command <<EOF IFS='%' read -r -a option <<< "$1"
$1 items+=("${option[0]}")
EOF #descriptions+=("$(option[1])") # not displayed so presently disabled
items="${items}:${item}" commands+=("${option[1]}")
commands="${commands}:${command}"
shift shift
done done
@ -38,15 +38,14 @@ menu_x=$((0))
menu_y=$(fathom-cursor -y) menu_y=$(fathom-cursor -y)
# Calculate the number of rows that the menu will occupy # Calculate the number of rows that the menu will occupy
num_rows=$(echo "$items" | tr -cd ':' | wc -c) num_rows=${#items[@]}
# Calculate the maximum length of the item names # Calculate the maximum length of the item names
# Todo: abstract this out into a max-length function in another script. I gave up on trying to find an elegant way to pass a list of strings with spaces. Even separating them with newlines didn't work. # Todo: abstract this out into a max-length function in another script. I gave up on trying to find an elegant way to pass a list of strings with spaces. Even separating them with newlines didn't work.
max_name_length=0 max_name_length=0
# Find the longest line # Find the longest line
set -f IFS=':'; set -- $items; set +f for line in "${items[@]}"; do
for line; do
line_length=${#line} line_length=${#line}
if [ $line_length -gt $max_name_length ]; then if [ $line_length -gt $max_name_length ]; then
max_name_length=$line_length max_name_length=$line_length
@ -55,7 +54,7 @@ done
# Make sure the menu items don't wrap, multiline menu items are not supported # Make sure the menu items don't wrap, multiline menu items are not supported
window_width=$(fathom-terminal -w) window_width=$(fathom-terminal -w)
if [ "${max_name_length:-0}" -gt "${window_width:-80}" ]; then if [ $max_name_length -gt $window_width ]; then
max_name_length=$window_width max_name_length=$window_width
fi fi
@ -80,21 +79,17 @@ display_menu() {
fi fi
# Print the items and commands # Print the items and commands
i=0 for i in "${!items[@]}"; do
set -f IFS=':'; set -- $items; set +f # Truncate the command to the width of the screen # Todo: come up with a better solution that allows the user to see the full command.
for item; do max_command_length=$(( $window_width - $max_name_length - 3 )) # -3 is for the > and the space between item and command
set -f IFS=':'; set -- $commands; set +f truncated_command=$(echo "${commands[$i]}" | cut -c -$max_command_length)
for j in $(seq 1 $selected); do shift; done if [ $i -eq $selected ]; then
command=$1 printf "${CYAN}> %-${max_name_length}s${RESET} ${GREY}%s${RESET}\n" "${items[$i]}" "$truncated_command"
max_command_length=$(( $window_width - $max_name_length - 3 )) # -3 is for the > and the space between item and command else
truncated_command=$(echo "${command}" | cut -c -$max_command_length) # Print the menu item plus extra spaces to cover up its command, since it's not selected
if [ $i -eq $selected ]; then printf " %-${max_name_length}s %*s\n" "${items[$i]}" "${#truncated_command}"
printf "${CYAN}> %-${max_name_length}s${RESET} ${GREY}%s${RESET}\n" "${item}" "$truncated_command" fi
else done
printf " %-${max_name_length}s %*s\n" "${item}" "${#truncated_command}"
fi
i=$((i + 1))
done
} }
# Define the function to handle key presses # Define the function to handle key presses

Loading…
Cancel
Save