feat(zsh): whiptail helpers

This commit is contained in:
Michel 2024-01-18 19:54:07 +01:00
parent d45e45e011
commit 9be893d07f
1 changed files with 50 additions and 5 deletions

55
.zshrc
View File

@ -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