Skip to main content
added 120 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
varwidth() (( ${(P)#1} * 3 - ${#${(ml[${(P)#1} * 2])${(P)1}}} ))
functions -Ms varwidth

varwidth_noescapevarwidth_without_formatting() {
  set -o localoptions -o extendedglob
  local s=$without_formatting=${(P)1//$'\e'\[[0-9;]#m}
  (( varwidth(swithout_formatting) ))
}
functions -Ms varwidth_noescapevarwidth_without_formatting

center() {
  local text
  for 1text do
    print -r -- ${(l[(COLUMNS-varwidth_noescapevarwidth_without_formatting(1text))/2])}$1$text
  done
}

center $'\e[31mred\e[1;39mbold\e[m' \
       ${(%):-%F{green}Blah%F{yellow}blah%F{magenta}blah%f}
varwidth() (( ${(P)#1} * 3 - ${#${(ml[${(P)#1} * 2])${(P)1}}} ))
functions -Ms varwidth

varwidth_noescape() {
  set -o localoptions -o extendedglob
  local s=${(P)1//$'\e'\[[0-9;]#m}
  (( varwidth(s) ))
}
functions -Ms varwidth_noescape

center() for 1 do
  print -r -- ${(l[(COLUMNS-varwidth_noescape(1))/2])}$1
done

center $'\e[31mred\e[1;39mbold\e[m'
varwidth() (( ${(P)#1} * 3 - ${#${(ml[${(P)#1} * 2])${(P)1}}} ))
functions -Ms varwidth

varwidth_without_formatting() {
  set -o localoptions -o extendedglob
  local without_formatting=${(P)1//$'\e'\[[0-9;]#m}
  (( varwidth(without_formatting) ))
}
functions -Ms varwidth_without_formatting

center() {
  local text
  for text do
    print -r -- ${(l[(COLUMNS-varwidth_without_formatting(text))/2])}$text
  done
}

center $'\e[31mred\e[1;39mbold\e[m' \
       ${(%):-%F{green}Blah%F{yellow}blah%F{magenta}blah%f}
added 706 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

Centring text that contains colouring / bold / standout... escape sequences would be more complicated. Easiest would probably be to remove them before getting the width of the string. For instance, with zsh, using that approach to determine string width (and handling 0-width or double-width characters).

varwidth() (( ${(P)#1} * 3 - ${#${(ml[${(P)#1} * 2])${(P)1}}} ))
functions -Ms varwidth

varwidth_noescape() {
  set -o localoptions -o extendedglob
  local s=${(P)1//$'\e'\[[0-9;]#m}
  (( varwidth(s) ))
}
functions -Ms varwidth_noescape

center() for 1 do
  print -r -- ${(l[(COLUMNS-varwidth_noescape(1))/2])}$1
done

center $'\e[31mred\e[1;39mbold\e[m'

Centring text that contains colouring / bold / standout... escape sequences would be more complicated. Easiest would probably be to remove them before getting the width of the string. For instance, with zsh, using that approach to determine string width (and handling 0-width or double-width characters).

varwidth() (( ${(P)#1} * 3 - ${#${(ml[${(P)#1} * 2])${(P)1}}} ))
functions -Ms varwidth

varwidth_noescape() {
  set -o localoptions -o extendedglob
  local s=${(P)1//$'\e'\[[0-9;]#m}
  (( varwidth(s) ))
}
functions -Ms varwidth_noescape

center() for 1 do
  print -r -- ${(l[(COLUMNS-varwidth_noescape(1))/2])}$1
done

center $'\e[31mred\e[1;39mbold\e[m'
edited body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

Note that it pads both to the left and the right (so to the right edge of the scriptscreen). You can remove the right padding (along with all trailing whitespace) with:

Note that it pads both to the left and the right (so to the right edge of the script). You can remove the right padding (along with all trailing whitespace) with:

Note that it pads both to the left and the right (so to the right edge of the screen). You can remove the right padding (along with all trailing whitespace) with:

added 273 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
Loading
added 479 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
Loading
added 823 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
Loading
added 858 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k
Loading