1 pièce(s) jointe(s)
Un retour sur mon script ?
Bonsoir à vous,
je vous propose un script que j'ai fait, je voulais un petit retour dessus (si vous pouvez car je sais que ça demande du temps), je prends les améliorations, les erreurs, les bugs, les idées... afin de faire un truc propre qui pourrait servir à d'autres.
Présentation : Il permet d'afficher une "question" et une liste de proposition à la manière de la commande select mais avec :
- de la couleur ou non...
- la possibilité de choix multiple
- la possibilité de limiter le nombre de choix
- la possibilité ajouter une description ou commentaire aux propositions
- la possibilité de renvoyer la valeur, le numéro ou bien une valeur cachée
Une petite vidéo pour montrer à quoi ça ressemble : https://asciinema.org/a/Sn69EBsItyjgfEUrkoPoAwcrx
J'ai fait une version fr et en (mais vu mon petit niveau, ça doit être bourré de fautes XD )
Je mets ici le fichier ainsi que les manpages (fr et en) qu'on peut afficher via la commande man ./multiselect1.fr.1
Et sinon voici le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
| #!/bin/bash
### Fonction arrêtant la commande en mode erreur
function ExitErreur
{
# Affiche un message d'erreur si présent
[[ ${1} ]] && echo -e "${ROUGE}${1}${RAZ}" >&2
# Renvoie le texte par défaut
echo -e "${error_text}"
# Arrêt problématique
exit 1
}
### Textes
function LangTextFunction
{
# Destruction du tableau
unset LangText
# Création d'un tableau global
declare -Ag LangText
# Remplissage du tableau dans la bonne langue
if [[ ${lang} == fr ]]
then
LangText['error']="L'argument error nécessite une valeur."
LangText['input']="L'argument input nécessite une valeur."
LangText['secret']="L'argument secret nécessite une valeur."
LangText['lang']="L'argument lang nécessite une valeur."
LangText['limit']="L'argument limit nécessite une valeur."
LangText['commentary']="L'argument commentary nécessite une valeur."
LangText['title']="L'argument title nécessite une valeur."
LangText['wait']="L'argument wait nécessite une valeur."
LangText['wait_text']="Sélection : "
LangText['error_unknow']="est un argument inconnu."
LangText['error_insufficient']="Nombre de proposition insuffisant."
LangText['error_limit']="Nombre de sélection dépassant la limite qui est de"
LangText['error_number']="Erreur dans la sélection, il faut rentrer un/des chiffre(s) parmi ceux proposés."
LangText['one_choice']="Un seul choix possible."
LangText['multi_choice']="choix possibles."
LangText['help']="${BLEUFONCE}Besoin d'aide ? Utiliser ${FUSHIA}man multiselect${BLEUFONCE}.${RAZ}"
else
LangText['error']="error argument needs value."
LangText['input']="input argument needs value."
LangText['secret']="secret argument needs value."
LangText['lang']="lang argument needs value."
LangText['limit']="limit argument needs value."
LangText['commentary']="commentary argument needs value."
LangText['title']="title argument needs value."
LangText['wait']="wait argument needs value."
LangText['wait_text']="Selection: "
LangText['error_unknow']="argument is unknow."
LangText['error_insufficient']="Number of proposals insufficient."
LangText['error_limit']="Number of selections exceeding the limit of"
LangText['error_number']="Error in the selection, it is necessary to enter one/multi of the proposed ones."
LangText['one_choice']="Only one choice possible."
LangText['multi_choice']="possible choices."
LangText['help']="${BLEUFONCE}Need help? Use ${FUSHIA}man multiselect${BLEUFONCE}.${RAZ}"
fi
}
### Variables par défaut
limit=0
title=""
com_separator="@@"
error_text="Exit"
secret_separator=""
nocolor=false
number=false
input=0
lang=${LANG:0:2}
### Couleurs
RAZ="\e[m"
ROUGE="\033[1;31m"
FUSHIA="\033[1;35m"
BLEUFONCE="\033[1;34m"
VERT="\033[1;32m"
### Chargement des textes
LangTextFunction ${lang}
### Récupération des valeurs des arguments
while getopts ":-:" O "${@}"
do
case ${OPTARG,,} in
err*)
[[ ${!OPTIND} != -* ]] && { error_text="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${error_text} ]] && ExitErreur "${LangText['error']}" ;;
secret)
[[ ${!OPTIND} != -* ]] && { secret_separator="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${secret_separator} ]] && ExitErreur "${LangText['secret']}" ;;
input)
[[ ${!OPTIND} != -* ]] && { input="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${input} ]] && ExitErreur "${LangText['input']}" ;;
lang)
[[ ${!OPTIND} != -* ]] && { lang="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${lang} ]] && ExitErreur "${LangText['lang']}" || LangTextFunction ${lang} ;;
limit*)
[[ ${!OPTIND} != -* ]] && { limit="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${limit} ]] && ExitErreur "${LangText['limit']}" ;;
nc|no-color)
nocolor=true ;;
number)
number=true ;;
com*)
[[ ${!OPTIND} != -* ]] && { com_separator="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${com_separator} ]] && ExitErreur "${LangText['commentary']}" ;;
title)
[[ ${!OPTIND} != -* ]] && { title="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${title} ]] && ExitErreur "${LangText['title']}" ;;
wait)
[[ ${!OPTIND} != -* ]] && { wait_text="${!OPTIND}"; ((OPTIND++)); }
[[ -z ${LangText['wait_text']} ]] && ExitErreur "${LangText['wait']}" ;;
h|help|aide)
# Affichage de l'aide
echo -e "\n${LangText['help']}" >&2
# Arrêt normal
exit 0 ;;
*)
# Affichage de l'aide
echo -e "\n${LangText['help']}" >&2
ExitErreur "${OPTARG,,} ${LangText['error_unknow']}" ;;
esac
done
# Gestion des arguments et commandes
shift $(($OPTIND-1))
### Destruction des couleurs (dans ce sens car on peut afficher des erreurs et des infos avant d'en arriver là)
[[ ${nocolor} == true ]] && unset RAZ ROUGE FUSHIA BLEUFONCE VERT="\033[1;32m"
### Remplissage de la liste des choix
choix=("${@}")
### Verifie qu'il y a bien des propositions
[[ -z ${choix[@]} || ${#choix[@]} < 2 ]] && ExitErreur "${LangText['error_insufficient']}"
### Gestion du message d'explication
# Message apparaissant avant la liste
if [[ ${title} && ${limit} > 0 ]]
then
# Message précisant le nombre de choix possibles
if [[ ${limit} = 1 ]]
then
echo -e "${FUSHIA}${title}${RAZ} ( ${BLEUFONCE}${LangText['one_choice']}${RAZ} )\n" >&2
else
echo -e "${FUSHIA}${title}${RAZ} ( ${BLEUFONCE}${limit} ${LangText['multi_choice']}${RAZ} )\n" >&2
fi
elif [[ ${title} && ${limit} < 1 ]]
then
# Message simple
echo -e "${FUSHIA}${title}${RAZ}\n" >&2
elif [[ -z ${title} && ${limit} > 0 ]]
then
# Message indiquant uniquement le nombre de choix possible
if [[ ${limit} = 1 ]]
then
echo -e "${BLEUFONCE}${LangText['one_choice']}${RAZ}\n" >&2
else
echo -e "${BLEUFONCE}${limit} ${LangText['multi_choice']}${RAZ}\n" >&2
fi
else
# S'il n'y a pas de message, saut de ligne esthétique
echo >&2
fi
# Affichage des propositions
for num in "${!choix[@]}"
do
# Destruction des variables
unset loop_text loop_com
# Variables pour la boucle
loop_text=${choix[${num}]}
[[ "${com_separator}" && "${loop_text}" =~ "${com_separator}" ]] && { loop_com=${loop_text##*${com_separator}}; loop_text=${loop_text%%${com_separator}*}; }
[[ "${secret_separator}" && "${loop_text}" =~ "${secret_separator}" ]] && loop_text=${loop_text%%${secret_separator}*}
[[ "${secret_separator}" && "${loop_com}" =~ "${secret_separator}" ]] && loop_com=${loop_com%%${secret_separator}*}
# Recherche le signe servant de séparateur entre la valeur et la description
if [[ "${loop_com}" ]]
then
# Si séparateur présent : affichage du numéro, de la valeur du texte et de la description
echo -e "${FUSHIA}${num}${RAZ}) ${BLEUFONCE}${loop_text}${RAZ} : ${VERT}${loop_com}${RAZ}" >&2
else
# Si pas de séparateur : affichage du numéro et de la valeur du texte
echo -e "${FUSHIA}${num}${RAZ}) ${BLEUFONCE}${loop_text}${RAZ}" >&2
fi
done
# Attente des réponses
echo >&2
read -u ${input} -p "${LangText['wait_text']}" -a REPLY
### Traitement des erreurs
# Message d'erreur indiquant un nombre de reponse trop élevé
[[ ${limit} > 0 && ${#REPLY[@]} -gt ${limit:-999} ]] && ExitErreur "${LangText['error_limit']} ${limit}."
# Si la réponse n'est pas un nombre ou que ce nombre dépasse le nombre de réponse possible
for num in "${REPLY[@]}"
do
[[ -z $(grep -w ${num} <<< "${!choix[@]}") ]] && ExitErreur "${LangText['error_number']}"
done
# Traitement des retours séparés des erreurs
for num in "${REPLY[@]}"
do
# Destruction des variables
unset loop_text loop_secret_text
# Variables pour la boucle
loop_text=${choix[${num}]}
[[ "${com_separator}" && "${loop_text}" =~ "${com_separator}" ]] && loop_text=${loop_text%%${com_separator}*}
[[ "${secret_separator}" && "${loop_text}" =~ "${secret_separator}" ]] && loop_text=${loop_text%%${secret_separator}*}
[[ "${secret_separator}" && "${choix[${num}]}" =~ "${secret_separator}" ]] && loop_secret_text=${choix[${num}]##*${secret_separator}}
# Détermine le texte à renvoyer
if [[ ${number} == true && ${secret_separator} && ${loop_secret_text} ]]
then
# Renvoie le numéro de ligne avec le texte caché
echo "${num}:${loop_secret_text}"
elif [[ ${number} == true ]]
then
# Si le texte à renvoyer vaut le texte d'erreur on l'envoie et quitte en mode erreur
[[ ${error_text} == ${loop_text} ]] && ExitErreur
# Renvoie le numero de ligne avec le texte
echo "${num}:${loop_text}"
elif [[ ${secret_separator} && ${loop_secret_text} ]]
then
# Ne renvoie que le texte caché
echo "${loop_secret_text}"
else
# Si le texte à renvoyer vaut le texte d'erreur on l'envoie et quitte en mode erreur
[[ ${error_text} == ${loop_text} ]] && ExitErreur
# Ne renvoie que le texte
echo "${loop_text}"
fi
done
### S'il n'y a eu aucune réponse, renvoie le texte par défaut
[[ -z ${REPLY} ]] && ExitErreur
### Arrêt normal
exit 0 |
Commandes de la vidéo de présentation :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| multiselect --lang en --title "Hi, what do you hate ?" \
"Mondays@@Yes I hate the mondays..." \
"Gun@@Fucking murderer !" \
"Hospital@@I don't like the stings" \
"Clowns@@I think to the book It" \
"Punks and rastas@@I'm afraid of they hairs" \
"Others@@Yep, I hate so many things... I'm so sad :("
# eg with return of numbers and texts and a limit of 3 answers
multiselect --lang en --number --limit 3 --title "Hi, what do you hate ?" \
"Mondays@@Yes I hate the mondays..." \
"Gun@@Fucking murderer !" \
"Hospital@@I don't like the stings" \
"Clowns@@I think to the book It" \
"Punks and rastas@@I'm afraid of they hairs" \
"Others@@Yep, I hate so many things... I'm so sad :("
# eg with a limit of 1 answer
multiselect --lang en --limit 1 --title "Hi, what do you hate most ?" \
"Mondays@@Yes I hate the mondays..." \
"Gun@@Fucking murderer !" \
"Hospital@@I don't like the stings" \
"Clowns@@I think to the book It" \
"Punks and rastas@@I'm afraid of they hairs" \
"Other@@It's not in the list"
# eg with a limit of 1 answer and return number and hidden text
multiselect --lang en --limit 1 --secret "~=~" --number --title "Hi, what do you hate most ?" \
"Mondays@@Yes I hate the mondays...~=~mon" \
"Gun@@Fucking murderer ! ~=~gun" \
"Hospital@@I don't like the stings~=~hos" \
"Clowns@@I think to the book It~=~clo" \
"Punks and rastas@@I'm afraid of they hairs~=~pun" \
"Other@@It's not in the list~=~oth" |
Merci à vous.