From 9be893d07fe14302899677f18888619ca5d751a1 Mon Sep 17 00:00:00 2001 From: Michel Date: Thu, 18 Jan 2024 19:54:07 +0100 Subject: [PATCH] feat(zsh): whiptail helpers --- .zshrc | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/.zshrc b/.zshrc index 3d9f0ce..0609f9f 100644 --- a/.zshrc +++ b/.zshrc @@ -311,16 +311,61 @@ bindkey -M viins '\en' nnn-file-widget # Whiptail helper # -menu_choose() { +# see https://stackoverflow.com/questions/1970180/whiptail-how-to-redirect-output-to-environment-variable + +whiptail_checklist() { + autoload zmathfunc; zmathfunc # min, max, sum + eval $(resize) # COLUMNS, LINES + local res title="$1" text="$2" + (( width=max(30, $#text) )) shift 2 + # Make tags compatible with whiptail local -a tags - for tag; - { tags+=($tag $tag) } + for tag; do + tags+=($tag $tag 0) + (( width=max(width, $#tag) )) + done - # see https://stackoverflow.com/questions/1970180/whiptail-how-to-redirect-output-to-environment-variable - tag=$(whiptail --title $title --menu $text 25 40 15 $tags --notags 3>&1 1>&2 2>&3 3>&-) + # Compute "best" width & height + (( list_height=min(20, ${#*}, LINES * .5) )) + (( height=min(list_height+8, LINES) )) + (( width=min(width + 9, COLUMNS * .75) )) + + tag=$(whiptail --title $title \ + --checklist $text $height $width $list_height $tags \ + --separate-output --notags 3>&1 1>&2 2>&3 3>&-) + res=$? + + # Use res=${(f)"$(whiptail_checklist...)"} to ignore spaces + echo $tag + return $res +} + +whiptail_menu() { + autoload zmathfunc; zmathfunc # min, max, sum + eval $(resize) # COLUMNS, LINES + + local res title="$1" text="$2" + (( width=max(30, $#text) )) + shift 2 + + # Make tags compatible with whiptail + local -a tags + for tag; do + tags+=($tag $tag) + (( width=max(width, $#tag) )) + done + + # Compute "best" width & height + (( menu_height=min(20, ${#*}, LINES * .5) )) + (( height=min(menu_height+8, LINES) )) + (( width=min(width + 5, COLUMNS * .75) )) + + tag=$(whiptail --title $title \ + --menu $text $height $width $menu_height $tags \ + --notags 3>&1 1>&2 2>&3 3>&-) res=$? echo $tag