Improve the dialog(1) API in dialog.subr by adding f_dialog_default_store()

and f_dialog_default_fetch(). Operating similar to functions introduced by
SVN r251236 and r251242, these functions operate as a pair for helping track
the default-item data (for the --menu, --checklist, and --radiolist
widgets).

This replaces the direct usage of a global to store the data with an
abstract method for readability and to centralize the code.
This commit is contained in:
Devin Teske 2013-06-02 09:02:12 +00:00
parent ec7120b5b2
commit 775997786b
15 changed files with 207 additions and 126 deletions

View File

@ -146,6 +146,7 @@ dialog_menu_main()
local btitle="$DIALOG_BACKTITLE"
local prompt="$msg_menu_text"
local menu_list
local defaultitem= # Calculated below
menu_list="
'X' '$msg_exit' '$msg_exit_bsdconfig'
@ -187,6 +188,9 @@ dialog_menu_main()
\"\" \
$menu_list
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--clear \
@ -198,7 +202,7 @@ dialog_menu_main()
--help-button \
--help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--default-item \"\$DEFAULTITEM_$$\" \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
@ -209,7 +213,7 @@ dialog_menu_main()
f_dialog_menutag_store "$menu_choice"
# Only update default-item on success
[ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$menu_choice"
[ $retval -eq 0 ] && f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -51,6 +51,7 @@ dialog_menu_main()
local menu_list
local hline="$hline_configure_system_console_settings"
local prompt="$msg_console_menu_text"
local defaultitem= # Calculated below
menu_list="
'X $msg_exit' '$msg_exit_this_menu'
@ -70,23 +71,26 @@ dialog_menu_main()
\"\$hline\" \
$menu_list
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_data_sanitize menu_choice
f_dialog_menutag_store "$menu_choice"
setvar DEFAULTITEM_$$ "$menu_choice"
f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -51,6 +51,7 @@ dialog_menu_main()
local menu_list
local hline=""
local prompt="$msg_menu_text"
local defaultitem= # Calculated below
menu_list="
'X $msg_exit' '$msg_exit_this_menu'
@ -69,23 +70,26 @@ dialog_menu_main()
\"\$hline\" \
$menu_list
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_data_sanitize menu_choice
f_dialog_menutag_store "$menu_choice"
setvar DEFAULTITEM_$$ "$menu_choice"
f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -50,6 +50,7 @@ dialog_menu_main()
{
local menu_list
local hline="$hline_arrows_tab_enter"
local defaultitem= # Calculated below
menu_list="
'X' '$msg_exit'
@ -67,23 +68,26 @@ dialog_menu_main()
\"\$hline\" \
$menu_list
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\" \
$height $width $rows \
$menu_list \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\" \
$height $width $rows \
$menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_data_sanitize menu_choice
f_dialog_menutag_store "$menu_choice"
setvar DEFAULTITEM_$$ "$menu_choice"
f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -54,7 +54,7 @@ dialog_menu_main()
local prompt="$msg_menu_text"
# Obtain default-item (adjusted below for dynamic tags)
f_getvar DEFAULTITEM_$$ defaultitem
f_dialog_default_fetch defaultitem
local ditem="${defaultitem%%[$IFS]*}"
menu_list="
@ -123,7 +123,7 @@ dialog_menu_main()
f_dialog_menutag_store "$menu_choice"
# Only update default-item on success
[ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$menu_choice"
[ $retval -eq 0 ] && f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -1757,6 +1757,45 @@ f_dialog_menutag_fetch()
return $SUCCESS
}
# f_dialog_default_store [-s] $text
#
# Store some text to be used later as the --default-item argument to dialog(1)
# (or Xdialog(1)) for --menu, --checklist, and --radiolist widgets. Retrieve
# the text later with f_dialog_menutag_fetch(). If the first argument is `-s',
# the text is sanitized before being stored.
#
f_dialog_default_store()
{
local sanitize=
[ "$1" = "-s" ] && sanitize=1 && shift 1 # -s
local text="$1"
# Sanitize the defaulitem before storing it if desired
[ "$sanitize" ] && f_dialog_data_sanitize text
setvar DEFAULTITEM_$$ "$text"
}
# f_dialog_default_fetch [$var_to_set]
#
# Obtain text to be used with the --default-item argument of dialog(1) (or
# Xdialog(1)) (previously stored with f_dialog_default_store() above). If
# $var_to_set is NULL or missing, output is printed to stdout (which is less
# recommended due to performance degradation; in a loop for example).
#
f_dialog_default_fetch()
{
local __var_to_set="$1" __cp
debug= f_getvar DEFAULTITEM_$$ "${__var_to_set:-__cp}" # get the data
setvar DEFAULTITEM_$$ "" # scrub memory in case data was sensitive
# Return the data on standard-out if desired
[ "$__var_to_set" ] || echo "$__cp"
return $SUCCESS
}
# f_dialog_menutag2item $tag_chosen $tag1 $item1 $tag2 $item2 ...
#
# To use the `--menu' option of dialog(1) you must pass an ordered list of

View File

@ -61,7 +61,7 @@ dialog_menu_main()
local prompt="$msg_miscellaneous_menu_text"
# Obtain default-item (adjusted below for dynamic tags)
f_getvar DEFAULTITEM_$$ defaultitem
f_dialog_default_fetch defaultitem
local ditem="${defaultitem%%[$IFS]*}"
menu_list="
@ -289,7 +289,7 @@ dialog_menu_main()
local retval=$?
f_dialog_data_sanitize menu_choice
f_dialog_menutag_store "$menu_choice"
setvar DEFAULTITEM_$$ "$menu_choice"
f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -81,6 +81,7 @@ dialog_menu_main()
{
local hline="$hline_arrows_tab_enter"
local prompt=""
local defaultitem= # Calculated below
RCCONF_MENU_LIST="
'X $msg_exit' '$msg_exit_desc'
@ -170,20 +171,23 @@ dialog_menu_main()
\"\$hline\" \
$RCCONF_MENU_LIST
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
--help-label \"\$msg_details\" \
${SHOW_DESC:+--item-help} \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$prompt\" \
$height $width $rows \
$RCCONF_MENU_LIST \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
--help-label \"\$msg_details\" \
${SHOW_DESC:+--item-help} \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$RCCONF_MENU_LIST \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
@ -191,7 +195,7 @@ dialog_menu_main()
f_dialog_menutag_store "$menu_choice"
# Only update default-item on success
[ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$menu_choice"
[ $retval -eq 0 ] && f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -148,6 +148,7 @@ dialog_menu_main()
{
local hline="$hline_arrows_tab_enter"
local prompt=""
local defaultitem= # Calculated below
#
# [Re-]Accent the menu list before incorporating it
@ -205,21 +206,24 @@ dialog_menu_main()
\"\$hline\" \
$menu_list
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--keep-tite \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
--help-label \"\$msg_details\" \
${SHOW_DESC:+--item-help} \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--keep-tite \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
--help-label \"\$msg_details\" \
${SHOW_DESC:+--item-help} \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
@ -227,7 +231,7 @@ dialog_menu_main()
f_dialog_menutag_store "$menu_choice"
# Only update default-item on success
[ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$menu_choice"
[ $retval -eq 0 ] && f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -67,6 +67,7 @@ dialog_menu_main()
{
local hline="$hline_arrows_tab_enter"
local prompt=""
local defaultitem= # Calculated below
RCVAR_MENU_LIST="
'X $msg_exit' '$msg_exit_this_menu'
@ -134,25 +135,28 @@ dialog_menu_main()
\"\$hline\" \
$RCVAR_MENU_LIST
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--keep-tite \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
${SHOW_DESC:+--item-help} \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$prompt\" \
$height $width $rows \
$RCVAR_MENU_LIST \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--keep-tite \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
${SHOW_DESC:+--item-help} \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$RCVAR_MENU_LIST \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_data_sanitize menu_choice
f_dialog_menutag_store "$menu_choice"
setvar DEFAULTITEM_$$ "$menu_choice"
f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -51,6 +51,7 @@ dialog_menu_main()
local menu_list
local hline="$hline_arrows_tab_enter"
local prompt=""
local defaultitem= # Calculated below
menu_list="
'X' '$msg_exit'
@ -67,23 +68,26 @@ dialog_menu_main()
\"\$hline\" \
$menu_list
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_data_sanitize menu_choice
f_dialog_menutag_store "$menu_choice"
setvar DEFAULTITEM_$$ "$menu_choice"
f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -83,6 +83,7 @@ dialog_menu_main()
local title="$DIALOG_TITLE"
local btitle="$DIALOG_BACKTITLE"
local prompt="$msg_select_region"
local defaultitem= # Calculated below
local height width rows
eval f_dialog_menu_size height width rows \
@ -92,22 +93,25 @@ dialog_menu_main()
\"\" \
$continent_menu_list
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$title\" \
--backtitle \"\$btitle\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$prompt\" \
$height $width $rows \
$continent_menu_list \
--title \"\$title\" \
--backtitle \"\$btitle\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\$prompt\" \
$height $width $rows \
$continent_menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
f_dialog_data_sanitize menu_choice
f_dialog_menutag_store "$menu_choice"
setvar DEFAULTITEM_$$ "$menu_choice"
f_dialog_default_store "$menu_choice"
return $retval
}

View File

@ -228,21 +228,22 @@ while :; do
\"\$hline\" \
$menu_items
f_dialog_default_fetch defaultitem
mtag=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$menu_text\" \
$height $width $rows \
$menu_items \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\$menu_text\" \
$height $width $rows \
$menu_items \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
retval=$?
f_dialog_data_sanitize mtag
setvar DEFAULTITEM_$$ "$mtag"
f_dialog_default_store "$mtag"
f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
# Exit if user has either pressed ESC or chosen Cancel/No

View File

@ -391,21 +391,22 @@ while :; do
\"\$hline\" \
$menu_items
f_dialog_default_fetch defaultitem
mtag=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\$menu_text\" \
$height $width $rows \
$menu_items \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--default-item \"\$defaultitem\" \
--menu \"\$menu_text\" \
$height $width $rows \
$menu_items \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
retval=$?
f_dialog_data_sanitize mtag
setvar DEFAULTITEM_$$ "$mtag"
f_dialog_default_store "$mtag"
f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
# Exit if user has either pressed ESC or chosen Cancel/No

View File

@ -53,6 +53,7 @@ dialog_menu_main()
{
local menu_list
local hline="$hline_arrows_tab_enter"
local defaultitem= # Calculated below
menu_list="
'X' '$msg_exit'
@ -76,20 +77,23 @@ dialog_menu_main()
# When using Xdialog(1) we need to bump the width for the buttons
[ "$USE_XDIALOG" ] && width=40
# Obtain default-item from previously stored selection
f_dialog_default_fetch defaultitem
local menu_choice
menu_choice=$( eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
--help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--default-item \"\$DEFAULTITEM_$$\" \
--menu \"\" \
$height $width $rows \
$menu_list \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--help-button \
--help-label \"\$msg_help\" \
${USE_XDIALOG:+--help \"\"} \
--default-item \"\$defaultitem\" \
--menu \"\" \
$height $width $rows \
$menu_list \
2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
)
local retval=$?
@ -97,7 +101,7 @@ dialog_menu_main()
f_dialog_menutag_store "$menu_choice"
# Only update default-item on success
[ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$menu_choice"
[ $retval -eq 0 ] && f_dialog_default_store "$menu_choice"
return $retval
}