summaryrefslogtreecommitdiff
path: root/eclass
diff options
context:
space:
mode:
authorjohnjay <johnjay@localhost>2006-07-28 04:33:26 +0000
committerjohnjay <johnjay@localhost>2006-07-28 04:33:26 +0000
commit56cbf2034f0d64858b0027a5c6cd968204b09ce0 (patch)
treeb37427be44e95f853eaa1841432971dc46037e40 /eclass
parentdev-db/kpogre: Version bump to v1.3.9 (diff)
downloadexperimental-56cbf2034f0d64858b0027a5c6cd968204b09ce0.tar.gz
experimental-56cbf2034f0d64858b0027a5c6cd968204b09ce0.tar.bz2
experimental-56cbf2034f0d64858b0027a5c6cd968204b09ce0.zip
eclass/einput.eclass - added einput_multi_prompt function
svn path=/experimental/; revision=36
Diffstat (limited to 'eclass')
-rw-r--r--eclass/einput.eclass72
1 files changed, 69 insertions, 3 deletions
diff --git a/eclass/einput.eclass b/eclass/einput.eclass
index dde78a8..136e7dc 100644
--- a/eclass/einput.eclass
+++ b/eclass/einput.eclass
@@ -40,10 +40,15 @@ EINPUT_COLORS=(
)
# Stores the user input received by the last einput function called (with the
-# exception of einput_confirm(), which returns the user's choice as its own exit
+# exception of einput_confirm(), which stores the user's choice and its own exit
# status).
EINPUT_ANSWER=""
+# Stores the user input received by the last einput function called (with the
+# exception of einput_confirm(), which stores the user's choice and its own exit
+# status). This answer is always in lower-case.
+EINPUT_IANSWER=""
+
# Set EINPUT_NOCOLOR to "0" to disable colorized output, or "1" to allow it.
# After inheriting this eclass ebuilds may set this var directly, but it's
# preferred to rely on the value supplied here by portageq.
@@ -64,7 +69,8 @@ fi
# This default can be set with the optional default_value parameter; accepted
# values are "0" (corresponding to "no") and "1" (for "yes").
#
-# Returns: Exit status 0 for "no", exit status 1 for "yes"
+# Returns: Exit status 0 for "no", exit status 1 for "yes", stores user answer
+# in $EINPUT_ANSWER and for case insensitive comparisons in $EINPUT_IANSWER
# Default: default_value if given, else 1
#
# Example: einput_confirm "Are you sure you want to rm -rf /?" "0"
@@ -95,13 +101,20 @@ einput_confirm() {
echo
case "$answer" in
[yY]*)
+ EINPUT_ANSWER=${string_yes}
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]`
return 1 ;;
[nN]*)
+ EINPUT_ANSWER=${string_no}
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]`
return 0 ;;
"")
- [[ "$2" == "0" ]] && return 0
+ [[ "$2" == "0" ]] && EINPUT_ANSWER=${string_no} && \
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]` && return 0
+ EINPUT_ANSWER=${string_yes}
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]`
return 1 ;;
*)
@@ -121,6 +134,7 @@ einput_confirm() {
# Matching of user input against option strings is case-sensitive.
#
# Returns: The chosen option as a string, stored in global $EINPUT_ANSWER
+# and lower case for case-insensitive comparisons in global $EINPUT_IANSWER.
# Default: none
#
# Example: einput_list "1" "List Entry" "2" "List File" "Choose a listing style"
@@ -150,6 +164,7 @@ einput_list() {
done
done
EINPUT_ANSWER="$answer"
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]`
}
# Usage: einput_prompt prompt [ default_value ]
@@ -160,6 +175,7 @@ einput_list() {
# is given, the brackets will be empty.
#
# Returns: The user's response as a string, stored in global $EINPUT_ANSWER
+# and lower case for case-insensitive comparisons in global $EINPUT_IANSWER.
# Default: default_value if given, else an empty string ""
#
# Example: einput_prompt "Is Gentoo a good Linux distro?" "Yes it is Jim"
@@ -173,6 +189,7 @@ einput_prompt() {
else
EINPUT_ANSWER="$answer"
fi
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]`
}
# Usage: einput_prompt_secret prompt
@@ -181,6 +198,7 @@ einput_prompt() {
# for when some operation requires a different set of user privileges.
#
# Returns: The user's response as a string, stored in global $EINPUT_ANSWER
+# and lower case for case-insensitive comparisons in global $EINPUT_IANSWER.
# Default: none
#
# Example: einput_prompt_secret "Please enter your root password"
@@ -191,4 +209,52 @@ einput_prompt_secret() {
echo
echo
EINPUT_ANSWER="$answer"
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]`
+}
+
+# Usage: einput_multi_prompt prompt option1 [ option2 ... ] default_value
+#
+# Display an in-line input prompt with a set of options.
+# If default_value is specified, its value will be displayed between brackets
+# at the end of the prompt, highlighted in green
+# letters (unless the environment variable NOCOLOR is set). If no default value
+# is given, the brackets will be empty.
+#
+# Returns: The user's response as a string, stored in global $EINPUT_ANSWER
+# and lower case for case-insensitive comparisons in global $EINPUT_IANSWER
+# Default: default_value if given, else an empty string ""
+#
+# Example: einput_multi_prompt "Please select your bindings" "C" "C++" "Python" "C"
+#
+einput_multi_prompt() {
+ local choices=( )
+ local num_choices=$(( ($# - 2) ))
+ local prompt="${BOLD}${1}${NORMAL}"
+ shift
+ local display_choices=""
+ for (( i = 0, j = 0 ; i < num_choices ; i++, j++ )) ; do
+ (( j == ${#EINPUT_COLORS[*]} )) && j=0
+ display_choices="${display_choices}${EINPUT_COLORS[${j}]}${1}"
+ choices[$i]="$1"
+ shift
+ if [ $i -lt $# ];then
+ display_choices="${display_choices}${NORMAL}/"
+ fi
+ done
+ echo -n "${prompt} ${NORMAL}<${display_choices}${NORMAL}> "
+ while true ; do
+ echo -n "${NORMAL}[${GREEN}${1}${NORMAL}]"
+ read -rp ": " answer
+ echo
+ for choice in ${choices[*]} ; do
+ if [[ "$choice" == "$answer" ]] ; then
+ break 2
+ elif [[ "$choice" == "${choices[$((num_choices - 1))]}" ]] ; then
+ echo "!!! Invalid answer, try again."
+ echo
+ fi
+ done
+ done
+ EINPUT_ANSWER="$answer"
+ EINPUT_IANSWER=`echo -n ${EINPUT_ANSWER} | tr [:upper:] [:lower:]`
}