From 7436b1b59d6bf6aa23bb54e92bff9620453498b7 Mon Sep 17 00:00:00 2001 From: classabbyamp Date: Mon, 18 Mar 2024 02:29:51 -0400 Subject: [PATCH] zfsbootmenu/lib/zfsbootmenu-ui.sh: add modal prompt --- zfsbootmenu/lib/zfsbootmenu-ui.sh | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/zfsbootmenu/lib/zfsbootmenu-ui.sh b/zfsbootmenu/lib/zfsbootmenu-ui.sh index 3177ea150..2f7e40fbe 100755 --- a/zfsbootmenu/lib/zfsbootmenu-ui.sh +++ b/zfsbootmenu/lib/zfsbootmenu-ui.sh @@ -664,3 +664,53 @@ populate_be_list() { done return $ret } + +# arg1: header/title text +# arg2..N: prompt options in the form "action:display text" +# prints: selected action +# returns: nothing + +draw_modal_prompt() { + local -i ROWS COLS LMARGIN TMARGIN maxlen=10 nopts=0 + local PROMPT="" OUTPUT TITLE disp action text + + TITLE=" ${1} " + if (( "${#TITLE}" > maxlen )); then + maxlen="${#TITLE}" + fi + shift + + for itm; do + disp="${itm#*:}" + if (( "${#disp}" > maxlen )); then + maxlen="${#disp}" + fi + PROMPT+="${itm}"$'\n' + nopts=$(( nopts + 1 )) + done + PROMPT+=":Cancel" + nopts=$(( nopts + 1 )) + + ROWS="$(tput lines)" + COLS="$(tput cols)" + + LMARGIN=$(( (COLS - maxlen - 6) / 2 )) + TMARGIN=$(( (ROWS - nopts - 3) / 2 )) + + if OUTPUT="$(echo -e "${PROMPT}" | \ + fzf --no-info \ + --with-nth="2.." \ + --delimiter=":" \ + --no-multi \ + --no-sort \ + ${HAS_BORDER:+--border=sharp} \ + ${HAS_BORDER:+--border-label="${TITLE}"} \ + --prompt="> " \ + --layout=default \ + --no-scrollbar \ + --margin "${TMARGIN},${LMARGIN}")"; then + # shellcheck disable=SC2034 + IFS=":" read -r action text <<< "${OUTPUT}" + printf "%s" "$action" + fi +}