From 8480a8cc021b135861309ff75e5e3305b830b449 Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Thu, 31 Jul 2025 11:55:15 +0200 Subject: [PATCH] umpf: highlight default choice in lists When showing a list of options, it is useful to have the default choice highlighted. So push the choices into variables and print them from function when the default choice is known. Then, print the default choice in bold. Signed-off-by: Benjamin Berg --- umpf | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/umpf b/umpf index 5c4891c..1511e26 100755 --- a/umpf +++ b/umpf @@ -430,6 +430,25 @@ nice_branch() { read -r -a replies < <(sed -r -n 's,^(remotes/([^/]*)/|heads/)?(.*),\3 \2,p' <<< "${1}") } +add_choice() { + choices+=("$1") + choice_descs+=("$2") +} + +print_choices() { + local i default + default="$1" + for i in "${!choices[@]}"; do + if [ "${default}" = "${choices[${i}]}" ]; then + echo -en '\e[1m' + echo "${choices[${i}]}) ${choice_descs[${i}]}" + echo -en '\e[0m' + else + echo "${choices[${i}]}) ${choice_descs[${i}]}" + fi; + done; +} + read_interactive() { local prompt="${1}" def="${2}" if ${DEFAULT} && [ -n "${def}" ]; then @@ -465,14 +484,19 @@ find_branch_rev() { info "Branch not found for '${remote}'. Choose alternative branch:" fi local i=0 def=0 choice + # shellcheck disable=SC2034 + local -a choices choice_descs + choices=() + choice_descs=() for branch in "${branches[@]}"; do nice_branch "${branch}" - echo "${i}) ${replies[1]:+${replies[1]}/}${replies[0]}" + add_choice "${i}" "${replies[1]:+${replies[1]}/}${replies[0]}" if [ "${GIT_FALLBACK_REMOTE}" == "${replies[1]}/" ]; then def="${i}" fi i=$((i+1)) done + print_choices "${def}" read_interactive "branch number" "${def}" nice_branch "${branches[${choice}]}" remote="${replies[1]:-refs/heads}/" @@ -1719,15 +1743,20 @@ apply_to_topic() { while [ -z "${topic}" ]; do local i=0 choice default + # shellcheck disable=SC2034 + local -a choices choice_descs + choices=() + choice_descs=() for branch in "${branch_names[@]}"; do - echo "${i}) ${branch}" + add_choice "${i}" "${branch}" if git log --pretty="format:%s" "${base}..${branches[${i}]}" | grep -q "^${match}$"; then default="${i}" fi i=$((i+1)) done - echo "s) show patch" - echo "x) skip patch" + add_choice "s" "show patch" + add_choice "x" "skip patch" + print_choices "${default}" read_interactive "Branch" "${default}" case "${choice}" in s)