Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédente | |||
informatique:outils:bash [2025/04/24 08:31] – [Fonctions utiles] bn8 | informatique:outils:bash [2025/07/07 18:04] (Version actuelle) – [Fonctions utiles] bn8 | ||
---|---|---|---|
Ligne 31: | Ligne 31: | ||
==== Fonctions utiles ==== | ==== Fonctions utiles ==== | ||
- | |||
=== in_array === | === in_array === | ||
Ligne 301: | Ligne 300: | ||
<code bash> | <code bash> | ||
+ | === sprint === | ||
+ | |||
+ | **Pré-requis :** [[# | ||
+ | |||
+ | <code bash> | ||
+ | # | ||
+ | # Styled text printing helper | ||
+ | # | ||
+ | declare -rA COLORS=( | ||
+ | [black]=30 [red]=31 [green]=32 [brown]=33 [blue]=34 [purple]=35 [cyan]=36 [light_grey]=37 | ||
+ | [default]=39 [dark_grey]=90 [light_red]=91 [light_green]=92 [yellow]=93 [light_blue]=94 | ||
+ | [light_purple]=95 [light_cyan]=96 [white]=97 | ||
+ | ) | ||
+ | declare -rA BACKGROUND_COLORS=( | ||
+ | [black]=40 [red]=41 [green]=42 [brown]=43 [blue]=44 [purple]=45 [cyan]=46 [ligth_grey]=47 | ||
+ | [default]=49 [dark_grey]=100 [light_red]=101 [light_green]=102 [yellow]=103 [light_blue]=104 | ||
+ | [light_purple]=105 [light_cyan]=106 [white]=107 | ||
+ | ) | ||
+ | declare -rA TEXT_STYLES=( | ||
+ | [normal]=0 [bold]=1 [dim]=2 [italic]=3 [underline]=4 [blink]=5 [inverted_colors]=7 [hidden]=8 | ||
+ | [strikethrough]=9 | ||
+ | ) | ||
+ | declare -rA RESET_STYLES=( | ||
+ | [all]=0 [bold]=21 [dim]=22 [underline]=24 [blink]=25 [inverted_colors]=27 [hidden]=28 | ||
+ | ) | ||
+ | |||
+ | function sprint() { | ||
+ | local idx=1 opt value no_newline=0 output="" | ||
+ | local -a styles=() text=() | ||
+ | |||
+ | __sprint() { | ||
+ | [[ -n " | ||
+ | [[ " | ||
+ | output+=" | ||
+ | styles=() | ||
+ | output+=" | ||
+ | text=() | ||
+ | } | ||
+ | while [[ $idx -le $# ]]; do | ||
+ | opt=${!idx} | ||
+ | case $opt in | ||
+ | -c|--color) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | styles+=( " | ||
+ | ;; | ||
+ | -b|--bg) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | styles+=( " | ||
+ | ;; | ||
+ | -s|--style) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | styles+=( " | ||
+ | ;; | ||
+ | -r|--reset) | ||
+ | [[ " | ||
+ | ((idx++)) | ||
+ | value=" | ||
+ | if [[ " | ||
+ | echo -n " | ||
+ | return 1 | ||
+ | fi | ||
+ | output+=" | ||
+ | ;; | ||
+ | -n) | ||
+ | no_newline=1 | ||
+ | ;; | ||
+ | -h|--help) | ||
+ | echo " | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | implode ", " " | ||
+ | fold -w 53 -s | sed " | ||
+ | echo | ||
+ | echo " | ||
+ | echo " | ||
+ | ;; | ||
+ | *) | ||
+ | text+=( " | ||
+ | esac | ||
+ | ((idx++)) | ||
+ | done | ||
+ | __sprint | ||
+ | output+=" | ||
+ | if [[ " | ||
+ | echo -en " | ||
+ | else | ||
+ | echo -e " | ||
+ | fi | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Exemple :** | ||
+ | <code bash> | ||
+ | sprint -c red ' | ||
+ | </ | ||
+ | |||
+ | Résultat : | ||
+ | |||
+ | {{: | ||
===== Gestion des paramètres ===== | ===== Gestion des paramètres ===== | ||