ajout de actions_menu
This commit is contained in:
parent
ad1d15e9f4
commit
ea4fd33318
|
@ -1 +1 @@
|
|||
007012000
|
||||
007013000
|
||||
|
|
281
lib/ulib/base
281
lib/ulib/base
|
@ -3441,6 +3441,287 @@ function simple_menu() {
|
|||
set_var "$__sm_option_var" "$__sm_option"
|
||||
}
|
||||
|
||||
function actions_menu() {
|
||||
# Afficher un menu dont les éléments sont les valeurs du tableau $4(=options),
|
||||
# et une liste d'actions tirées du tableau $3(=actions). L'option choisie est
|
||||
# placée dans la variable $2(=option). L'action choisie est placée dans la
|
||||
# variable $1(=action)
|
||||
# Un choix est saisi sous la forme [action]num_option
|
||||
# -t TITLE: spécifier le titre du menu
|
||||
# -m OPT_YOUR_CHOICE: spécifier le message d'invite pour la sélection de
|
||||
# l'action et de l'option
|
||||
# -M ACT_YOUR_CHOICE: spécifier le message d'invite dans le cas où aucune option
|
||||
# n'est disponible. Dans ce cas, seules les actions vides sont possibles.
|
||||
# -e VOID_ACTION: spécifier qu'une action est vide, c'est à dire qu'elle ne
|
||||
# requière pas d'être associée à une option. Par défaut, la dernière action
|
||||
# est classée dans cette catégorie puisque c'est l'action "quitter"
|
||||
# -d DEFAULT_ACTION: choisir l'action par défaut. par défaut, c'est la première
|
||||
# action.
|
||||
# -q QUIT_ACTION: choisir l'option "quitter" qui provoque la sortie du menu sans
|
||||
# choix. par défaut, c'est la dernière action.
|
||||
# -o DEFAULT_OPTION: choisir l'option par défaut. par défaut, prendre la valeur
|
||||
# actuelle de la variable $2(=option)
|
||||
local -a __am_action_descs __am_options __am_void_actions
|
||||
local __am_tmp __am_select_action __am_select_option __am_title __am_optyv __am_actyc
|
||||
local __am_default_action=auto __am_quit_action=auto
|
||||
local __am_default_option=
|
||||
local -a __am_args
|
||||
parse_opts \
|
||||
-t: __am_title= \
|
||||
-m: __am_optyc= \
|
||||
-M: __am_actyc= \
|
||||
-e: __am_void_actions \
|
||||
-d: __am_default_action= \
|
||||
-q: __am_quit_action= \
|
||||
-o: __am_default_option= \
|
||||
@ __am_args -- "$@" && set -- "${__am_args[@]}" || { eerror "$__am_args"; return 1; }
|
||||
|
||||
__am_tmp="${1:-action}"; __am_select_action="${!__am_tmp}"
|
||||
__am_tmp="${2:-option}"; __am_select_option="${!__am_tmp}"
|
||||
[ -n "$__am_default_option" ] && __am_select_option="$__am_default_option"
|
||||
array_copy __am_action_descs "${3:-actions}"
|
||||
array_copy __am_options "${4:-options}"
|
||||
|
||||
eerror_unless [ ${#__am_action_descs[*]} -gt 0 ] "Vous devez spécifier le tableau des actions" || return
|
||||
__actions_menu || return 1
|
||||
setv "${1:-action}" "$__am_select_action"
|
||||
setv "${2:-option}" "$__am_select_option"
|
||||
}
|
||||
function __actions_menu() {
|
||||
local title="$__am_title"
|
||||
local optyc="$__am_optyc" actyc="$__am_actyc"
|
||||
local default_action="$__am_default_action"
|
||||
local quit_action="$__am_quit_action"
|
||||
local select_action="$__am_select_action"
|
||||
local select_option="$__am_select_option"
|
||||
local -a action_descs options void_actions
|
||||
array_copy action_descs __am_action_descs
|
||||
array_copy options __am_options
|
||||
array_copy void_actions __am_void_actions
|
||||
|
||||
# Calculer la liste des actions valides
|
||||
local no_options
|
||||
array_isempty options && no_options=1
|
||||
|
||||
local -a actions
|
||||
local tmp action name
|
||||
for tmp in "${action_descs[@]}"; do
|
||||
splitfsep2 "$tmp" : action name
|
||||
[ -n "$action" ] || action="${name:0:1}"
|
||||
action="$(strlower "$action")"
|
||||
array_addu actions "$action"
|
||||
done
|
||||
|
||||
# Calculer l'action par défaut
|
||||
if [ "$default_action" == auto ]; then
|
||||
# si action par défaut non spécifiée, alors prendre la première action
|
||||
default_action="$select_action"
|
||||
if [ -n "$default_action" ]; then
|
||||
array_contains actions "$default_action" || default_action=
|
||||
fi
|
||||
[ -n "$default_action" ] || default_action="${actions[0]}"
|
||||
fi
|
||||
default_action="${default_action:0:1}"
|
||||
default_action="$(strlower "$default_action")"
|
||||
|
||||
# Calculer l'action quitter par défaut
|
||||
if [ "$quit_action" == auto ]; then
|
||||
# si action par défaut non spécifiée, alors prendre la dernière action,
|
||||
# s'il y a au moins 2 actions
|
||||
if [ ${#actions[*]} -gt 1 ]; then
|
||||
quit_action="${actions[@]:$((-1)):1}"
|
||||
array_addu void_actions "$quit_action"
|
||||
fi
|
||||
fi
|
||||
quit_action="${quit_action:0:1}"
|
||||
quit_action="$(strlower "$quit_action")"
|
||||
|
||||
# Calculer la ligne des actions à afficher
|
||||
local action_title
|
||||
for tmp in "${action_descs[@]}"; do
|
||||
splitfsep2 "$tmp" : action name
|
||||
[ -n "$action" ] || action="${name:0:1}"
|
||||
[ -n "$name" ] || name="$action"
|
||||
action="$(strlower "$action")"
|
||||
if [ -n "$no_options" ]; then
|
||||
if ! array_contains void_actions "$action"; then
|
||||
array_del actions "$action"
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
[ "$action" == "$default_action" ] && name="$name*"
|
||||
action_title="${action_title:+$action_title/}$name"
|
||||
done
|
||||
if [ -n "$default_action" ]; then
|
||||
# si action par défaut invalide, alors pas d'action par défaut
|
||||
array_contains actions "$default_action" || default_action=
|
||||
fi
|
||||
if [ -n "$quit_action" ]; then
|
||||
# si action quitter invalide, alors pas d'action quitter
|
||||
array_contains actions "$quit_action" || quit_action=
|
||||
fi
|
||||
|
||||
# Type de menu
|
||||
if [ -n "$no_options" ]; then
|
||||
if array_isempty void_actions; then
|
||||
OENC="$UTF8" eerror "Aucune option n'est définie. Il faut définir le tableau des actions vides"
|
||||
return 1
|
||||
fi
|
||||
__void_actions_menu
|
||||
else
|
||||
__options_actions_menu
|
||||
fi
|
||||
}
|
||||
function __void_actions_menu() {
|
||||
eflush
|
||||
local c=0 choice
|
||||
while true; do
|
||||
if [ $c -eq 0 ]; then
|
||||
[ -n "$title" ] && tooenc "=== $title ===" 1>&2
|
||||
tooenc_ "Actions disponibles: " "$UTF8" 1>&2
|
||||
tooenc "$action_title" "$UTF8" 1>&2
|
||||
fi
|
||||
if [ -n "$actyc" ]; then
|
||||
tooenc_ "$actyc" 1>&2
|
||||
elif [ -n "$optyc" ]; then
|
||||
tooenc_ "$optyc" 1>&2
|
||||
else
|
||||
tooenc_ "Entrez l'action à effectuer" "$UTF8" 1>&2
|
||||
fi
|
||||
tooenc_ ": " "$UTF8" 1>&2
|
||||
uread choice
|
||||
if [ -z "$choice" -a -n "$default_action" ]; then
|
||||
select_action="$default_action"
|
||||
break
|
||||
fi
|
||||
|
||||
# vérifier la saisie
|
||||
choice="${choice:0:1}"
|
||||
choice="$(strlower "$choice")"
|
||||
if array_contains actions "$choice"; then
|
||||
select_action="$choice"
|
||||
break
|
||||
elif [ -n "$choice" ]; then
|
||||
OENC="$UTF8" eerror "$choice: action incorrecte"
|
||||
else
|
||||
OENC="$UTF8" eerror "vous devez saisir l'action à effectuer"
|
||||
fi
|
||||
let c=$c+1
|
||||
if [ $c -eq 5 ]; then
|
||||
# sauter une ligne toutes les 4 tentatives
|
||||
tooenc "" "$UTF8" 1>&2
|
||||
c=0
|
||||
fi
|
||||
done
|
||||
__am_select_action="$select_action"
|
||||
__am_select_option=
|
||||
}
|
||||
function __options_actions_menu() {
|
||||
eflush
|
||||
local c=0 option choice action option
|
||||
while true; do
|
||||
if [ $c -eq 0 ]; then
|
||||
[ -n "$title" ] && tooenc "=== $title ===" 1>&2
|
||||
i=1
|
||||
for option in "${options[@]}"; do
|
||||
if [ "$option" == "$select_option" ]; then
|
||||
tooenc "$i*- $option" 1>&2
|
||||
else
|
||||
tooenc "$i - $option" 1>&2
|
||||
fi
|
||||
let i=$i+1
|
||||
done
|
||||
tooenc_ "
|
||||
Actions disponibles: " "$UTF8" 1>&2
|
||||
tooenc "$action_title" "$UTF8" 1>&2
|
||||
fi
|
||||
if [ -n "$optyc" ]; then
|
||||
tooenc_ "$optyc" 1>&2
|
||||
else
|
||||
tooenc_ "Entrez l'action et le numéro de l'option choisie" "$UTF8" 1>&2
|
||||
fi
|
||||
tooenc_ ": " "$UTF8" 1>&2
|
||||
uread choice
|
||||
|
||||
# vérifier la saisie
|
||||
if [ -z "$choice" -a -n "$default_action" ]; then
|
||||
action="$default_action"
|
||||
if array_contains void_actions "$action"; then
|
||||
select_action="$action"
|
||||
select_option=
|
||||
break
|
||||
elif [ -n "$select_option" ]; then
|
||||
select_action="$action"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
action="${choice:0:1}"
|
||||
action="$(strlower "$action")"
|
||||
if array_contains actions "$action"; then
|
||||
# on commence par un code d'action valide. cool :-)
|
||||
if array_contains void_actions "$action"; then
|
||||
select_action="$action"
|
||||
select_option=
|
||||
break
|
||||
else
|
||||
option="${choice:1}"
|
||||
option="${option// /}"
|
||||
if [ -z "$option" -a -n "$select_option" ]; then
|
||||
select_action="$action"
|
||||
break
|
||||
elif [ -z "$option" ]; then
|
||||
OENC="$UTF8" eerror "vous devez saisir le numéro de l'option"
|
||||
elif isnum "$option"; then
|
||||
if [ $option -gt 0 -a $option -le ${#options[*]} ]; then
|
||||
select_action="$action"
|
||||
select_option="${options[$(($option - 1))]}"
|
||||
break
|
||||
fi
|
||||
else
|
||||
OENC="$UTF8" eerror "$option: numéro d'option incorrecte"
|
||||
fi
|
||||
fi
|
||||
elif isnum "$choice"; then
|
||||
# on a simplement donné un numéro d'option
|
||||
action="$default_action"
|
||||
if [ -n "$action" ]; then
|
||||
if array_contains void_actions "$action"; then
|
||||
select_action="$action"
|
||||
select_option=
|
||||
break
|
||||
else
|
||||
option="${choice// /}"
|
||||
if [ -z "$option" ]; then
|
||||
OENC="$UTF8" eerror "vous devez saisir le numéro de l'option"
|
||||
elif isnum "$option"; then
|
||||
if [ $option -gt 0 -a $option -le ${#options[*]} ]; then
|
||||
select_action="$action"
|
||||
select_option="${options[$(($option - 1))]}"
|
||||
break
|
||||
fi
|
||||
else
|
||||
OENC="$UTF8" eerror "$option: numéro d'option incorrecte"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
OENC="$UTF8" eerror "Vous devez spécifier l'action à effectuer"
|
||||
fi
|
||||
elif [ -n "$choice" ]; then
|
||||
OENC="$UTF8" eerror "$choice: action et/ou option incorrecte"
|
||||
else
|
||||
OENC="$UTF8" eerror "vous devez saisir l'action à effectuer"
|
||||
fi
|
||||
let c=$c+1
|
||||
if [ $c -eq 5 ]; then
|
||||
# sauter une ligne toutes les 4 tentatives
|
||||
tooenc "" "$UTF8" 1>&2
|
||||
c=0
|
||||
fi
|
||||
done
|
||||
__am_select_action="$select_action"
|
||||
__am_select_option="$select_option"
|
||||
}
|
||||
|
||||
################################################################################
|
||||
## fichiers temporaires
|
||||
|
||||
|
|
Loading…
Reference in New Issue