0

I need to display

--------------------------- word ----------------------------

with the word right in the middle of the horizontal line and set the length of the horizontal line to that it fits the used terminal width.

I thought about using a similar expression than

printf '%*s\n' "$(tput cols)" '' | tr ' ' -
user123456
  • 5,018

1 Answers1

0

Here is a version with but I am sure there is a more compact way of doing it

#!/bin/bash

WORD=$@
NUMBER_OF_SPACES_SURROUNDING_THE_WORD=1  

TERMINAL_WIDTH=$(tput cols)
WORD_SIZE=$(echo $WORD | wc -c )
NUMBER_OF_SPACES_SURROUNDING_THE_WORD=1
NUMBER_OF_SYMBOLS=$((TERMINAL_WIDTH -   $WORD_SIZE - 2*$NUMBER_OF_SPACES_SURROUNDING_THE_WORD))
LENGTH_OF_RIGHT_PART=$((NUMBER_OF_SYMBOLS/2 + $NUMBER_OF_SYMBOLS%2 ))
LENGTH_OF_LEFT_PART=$(( $TERMINAL_WIDTH - 2*$NUMBER_OF_SPACES_SURROUNDING_THE_WORD - $WORD_SIZE - $LENGTH_OF_RIGHT_PART      ))


printf '%*s' "$LENGTH_OF_RIGHT_PART" '' | tr " " -
printf '%*s' $NUMBER_OF_SPACES_SURROUNDING_THE_WORD
printf $WORD
printf '%*s' $NUMBER_OF_SPACES_SURROUNDING_THE_WORD
printf '%*s\n' "$LENGTH_OF_RIGHT_PART" '' | tr " " -
user123456
  • 5,018