Skip to content

Instantly share code, notes, and snippets.

@oppianmatt
Created April 28, 2024 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oppianmatt/dcc6f19542b080973e6164c7108d9a5a to your computer and use it in GitHub Desktop.
Save oppianmatt/dcc6f19542b080973e6164c7108d9a5a to your computer and use it in GitHub Desktop.
Makefile menu
# menu.sh
#!/bin/bash
# Arrays for storing targets and descriptions
declare -a target_order
declare -a description_order
# Parse the Makefile
max_len=0
index=0
while IFS= read -r line; do
if [[ ${line} =~ ^([a-zA-Z0-9_-]+):.*#\s*(.*)$ ]]; then
target="${BASH_REMATCH[1]}"
description="${BASH_REMATCH[2]}"
target_order[index]="${target}"
description_order[index]="${description}"
len=${#description}
if (( len > max_len )); then
max_len=$len
fi
index=$((index+1))
fi
done < Makefile
menu_items=()
for index in "${!target_order[@]}"; do
echo "Target: ${target_order[index]}"
echo "Description: ${description_order[index]}"
menu_items+=("${target_order[index]}" "${description_order[index]}")
done
# Show the dialog menu
height=$(tput lines)
width=$(tput cols)
CHOICE=$(dialog --clear \
--backtitle "Command Menu" \
--title "Choose a Target" \
--menu "Select an option:" $((height - 8)) $((width - 8)) $((height - 15)) \
"${menu_items[@]}" \
2>&1 >/dev/tty)
clear
# Run the selected make target
if [[ -n "${CHOICE}" ]]; then
make "${CHOICE}"
# Wait for the user to press a key
read -n 1 -s -r -p "Press any key to continue"
# reshow the menu
make menu
else
echo "No selection made or cancelled."
fi
# End of script
@oppianmatt
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment