Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending terminal colors to the end of line

Tags:

I have a bash script which generates a motd. The problem is depending on some terminal settings which I am not sure about the color will extend to the end of the line. Othertimes it doesn't:

e.g.

square

v.s.

jagged

IIRC one is just the normal gnome-terminal and the other is my tmux term. So my question is how can I get this to extend to 80 character (or really to the terminal width). Of course I can pad to 80 chars but that really doesn't solve the problem.

Here is a snip of my code which generates the motd:

TC_RESET="^[[0m"                                                                
TC_SKY="^[[0;37;44m"                                                            
TC_GRD="^[[0;30;42m"                                                            
TC_TEXT="^[[38;5;203m"   

echo -n "${TC_SKY}                                                                              

... lots of printing..." 
echo -e "\n                           Welcome to Mokon's Linux!                           \n"

echo -n "${TC_GRD}"                                                             
nodeinfo # Just prints the info seen below...                                                                       
echo ${TC_RESET} 

How can I programmatically from bash change the terminal settings or something change the color to the end of the line?

like image 734
David Mokon Bond Avatar asked Nov 11 '13 20:11

David Mokon Bond


People also ask

How do I change the color of a terminal in Linux command line?

“tput setaf” sets foreground color, “tput setab” sets background color, and “tput sgr0” resets all the settings to terminal default.

What is the Colour of the terminal?

Terminals traditionally take an input of bytes and display them as white text on a black background. If the input contains specific “control characters,” then the terminal may alter certain display properties of the text, such as the color or font. Old terminals could only display a maximum of 8 colors.

Which character is used to continue a command to the next line?

Continuing a Long Command on Another Line To make the commands easier to understand, use the shell escape character, which is a backslash, to continue a command on the next line.


3 Answers

Maybe use the Escape sequence to clear-to-EOL

For some reason (on my MacOS terminal!) I only needed specify this sequence and then it worked for all the lines but for completeness I list it for all

TC_RESET=$'\x1B[0m'
TC_SKY=$'\x1B[0;37;44m'
TC_GRD=$'\x1B[0;30;42m'
TC_TEXT=$'\x1B[38;5;203m'
CLREOL=$'\x1B[K'

echo -n "${TC_SKY}${CLREOL}"
echo -e "\n           ABC${CLREOL}\n"
echo -e "\n              DEFG${CLREOL}\n"

echo -n "${TC_GRD}"
echo -e "\n           ABC${CLREOL}\n"
echo -e "\n              DEFG${CLREOL}\n"
echo ${TC_RESET}
like image 66
nhed Avatar answered Oct 13 '22 09:10

nhed


Padding filter

Unfortunely, you have to pad each line with exact number of spaces for changing the color of the whole line's background.

As you're speaking about bash, my solution will use bashisms (Won't work under other shell, or older version of bash).

  • syntax printf -v VAR FORM ARGS assign to varianble VAR then result of sprintf FORM ARGS. That's bashism, under other kind of shell, you have to replace this line by TC_SPC=$(printf "%${COLUMNS}s" '')

You may try this:

... lots of printing..." 
echo -e "\n                           Welcome to Mokon's Linux!                           \n"

echo -n "${TC_GRD}"

printf -v TC_SPC "%${COLUMNS}s" ''

nodeinfo |
    sed "s/$/$TC_SPC/;s/^\\(.\\{${COLUMNS}\\}\\) */\\1/" # Just prints the info seen below...

echo ${TC_RESET}

enter image description here Maybe you have to ensure that $COLUMNS is correctly setted:

COLUMNS=$(tput cols)

As you could see, only the result of command filtered by sed is fully colored.

you may

  • use same filter many times:

    cmd1 | sed '...'
    cmd2 | sed '...'
    
  • or group your commands to use only one filter:

    ( cmd1 ; cmd 2 ) | sed '...'
    

But there is an issue in case you try to filter ouptut that contain formatting escapes:

(
    echo $'\e[33;44;1mYellow text on blue background';
    seq 1 6;
    echo $'\e[0m'
) | sed "
  s/$/$TC_SPC/;
  s/^\\(.\\{${COLUMNS}\\}\\) */\\1/"

unterminated sample

Il the lines you have to pad to contain escapes, you have to isolate thems:

(
    echo $'\e[33;44;1mYellow text on blue background';
    seq 1 6;
    echo $'\e[0m'
) | sed "
  s/\$/$TC_SPC/;
  s/^\\(\\(\\o33\\[[0-9;]*[a-zA-Z]\\)*\\)\\([^\o033]\\{${COLUMNS}\\}\\) */\\1\\3/
"

enter image description here

And finally to be able to fill terminate very long lines:

(
    echo $'\e[33;44;1mYellow text on blue background';
    seq 1 6;
    echo "This is a very very long long looooooooooong line that contain\
       more characters than the line could hold...";
    echo $'\e[0m';
) | sed "            
  s/\$/$TC_SPC/;
  s/^\\(\\(\\o33\\[[0-9;]*[a-zA-Z]\\)*\\)\\(\\([^\o033]\\{${COLUMNS}\\}\\)*\\) */\\1\\3/"

Nota: This only work if formating escapes are located at begin of line.

like image 34
F. Hauri Avatar answered Oct 13 '22 08:10

F. Hauri


Try with this:

echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0
like image 41
andrea.marangoni Avatar answered Oct 13 '22 07:10

andrea.marangoni