(→caractère sur la largeur du terminal) |
|||
| Ligne 23 : | Ligne 23 : | ||
printf '%s\n' ${spaces// /#} | printf '%s\n' ${spaces// /#} | ||
</pre> | </pre> | ||
| + | |||
| + | ==Titre + remplissage + gestion longueur impaire== | ||
| + | |||
| + | <pre> | ||
| + | printtitre () { | ||
| + | c=$1 | ||
| + | if (( ${#2} % 2 > 0 )) ; then printf "$c"; fi | ||
| + | if (( $(tput cols) % 2 > 0 )) ; then printf "$c" ; fi | ||
| + | printf "%*s" $(( ($(tput cols) - ${#2} - 2) / 2 )) '' | sed "s/ /$c/g" | ||
| + | printf "%s" " $2 " | ||
| + | printf "%*s" $(( ($(tput cols) - ${#2} - 2) / 2 )) '' | sed "s/ /$c/g" | ||
| + | printf "\n" | ||
| + | } | ||
Sommaire |
la commande tput cols renvoi la largeur actuelle du terminal en nombre de caractère, soit le nombre de colonne.
printf -v spaces '%*s' $(tput cols) ''; printf '%s\n' ${spaces// /#}
autre solution:
printf "#%.0s" {$(seq $(tput cols) )}
avec toto variable contenant le titre.
printf -v spaces '%*s' $((($(tput cols)-${#toto}-2)/2)) ''
printf '%s' ${spaces// /#};printf " $toto "
printf '%s\n' ${spaces// /#}
printtitre () {
c=$1
if (( ${#2} % 2 > 0 )) ; then printf "$c"; fi
if (( $(tput cols) % 2 > 0 )) ; then printf "$c" ; fi
printf "%*s" $(( ($(tput cols) - ${#2} - 2) / 2 )) '' | sed "s/ /$c/g"
printf "%s" " $2 "
printf "%*s" $(( ($(tput cols) - ${#2} - 2) / 2 )) '' | sed "s/ /$c/g"
printf "\n"
}