Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
informatique:outils:bash [2025/04/24 08:31] – [Fonctions utiles] bn8informatique: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>declare -p variableName</code> <code bash>declare -p variableName</code>
  
 +=== sprint ===
 +
 +**Pré-requis :** [[#implode]]
 +
 +<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" ]] && output+=" "
 +        [[ "${#styles}" -gt 0 ]] && \
 +            output+="\e[$(implode ';' "${styles[@]}")m" && \
 +            styles=()
 +        output+="${text[*]}"
 +        text=()
 +    }
 +    while [[ $idx -le $# ]]; do
 +        opt=${!idx}
 +        case $opt in
 +            -c|--color)
 +                [[ "${#text}" -gt 0 ]] && __sprint
 +                ((idx++))
 +                value="${!idx,,}"
 +                if [[ "${COLORS[$value]:-null}" == "null" ]]; then
 +                    echo -n "sprint: invalid color '${!idx}'"
 +                    return 1
 +                fi
 +                styles+=( "${COLORS[$value]}" )
 +                ;;
 +            -b|--bg)
 +                [[ "${#text}" -gt 0 ]] && __sprint
 +                ((idx++))
 +                value="${!idx,,}"
 +                if [[ "${BACKGROUND_COLORS[$value]:-null}" == "null" ]]; then
 +                    echo -n "sprint: invalid background color '${!idx}'"
 +                    return 1
 +                fi
 +                styles+=( "${BACKGROUND_COLORS[$value]}" )
 +                ;;
 +            -s|--style)
 +                [[ "${#text}" -gt 0 ]] && __sprint
 +                ((idx++))
 +                value="${!idx,,}"
 +                if [[ "${TEXT_STYLES[$value]:-null}" == "null" ]]; then
 +                    echo -n "sprint: invalid text style '${!idx}'"
 +                    return 1
 +                fi
 +                styles+=( "${TEXT_STYLES[$value]}" )
 +                ;;
 +            -r|--reset)
 +                [[ "${#text}" -gt 0 ]] && __sprint
 +                ((idx++))
 +                value="${!idx,,}"
 +                if [[ "${RESET_STYLES[$value]:-null}" == "null" ]]; then
 +                    echo -n "sprint: invalid reset option '${!idx}'"
 +                    return 1
 +                fi
 +                output+="\e[${RESET_STYLES[$value]}m"
 +                ;;
 +            -n)
 +                no_newline=1
 +                ;;
 +            -h|--help)
 +                echo "usage: sprint [-n] [-c color] [-b color] [-s style] [words] [-r what] [...]"
 +                echo "  -c / --color [color]   Colored text. Available colors:"
 +                implode ", " "${!COLORS[@]}" | \
 +                    fold -w 53 -s | sed "s/^/                         /g"
 +                echo
 +                echo "  -b / --bg [color]      Text background color. Available colors:"
 +                implode ", " "${!BACKGROUND_COLORS[@]}" | \
 +                    fold -w 53 -s | sed "s/^/                         /g"
 +                echo
 +                echo "  -s / --style [style]   Text style. Available styles:"
 +                implode ", " "${!TEXT_STYLES[@]}" | \
 +                    fold -w 53 -s | sed "s/^/                         /g"
 +                echo
 +                echo "  -r / --reset [what]    Reset some previously specified styles. Available reset clauses:"
 +                implode ", " "${!RESET_STYLES[@]}" | \
 +                    fold -w 53 -s | sed "s/^/                         /g"
 +                echo
 +                echo "  -n                     Do not add new line"
 +                echo "  [words]                Words that compose the text"
 +                ;;
 +            *)
 +                text+=( "$opt" )
 +        esac
 +        ((idx++))
 +    done
 +    __sprint
 +    output+="\e[${RESET_STYLES[all]}m"
 +    if [[ "$no_newline" -eq 1 ]]; then
 +        echo -en "$output"
 +    else
 +        echo -e "$output"
 +    fi
 +}
 +</code>
 +
 +**Exemple :**
 +<code bash>
 +sprint -c red '[' -s blink ERROR -r blink ']' -r all Really bad error occurred -s dim -s italic '(see log for details)'
 +</code>
 +
 +Résultat : 
 +
 +{{:informatique:outils:sprint.svg?500|}}
 ===== Gestion des paramètres ===== ===== Gestion des paramètres =====