Extending terminal colors to the end of line

梦想的初衷 提交于 2019-12-20 10:32:16

问题


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.

v.s.

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?


回答1:


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}



回答2:


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}

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/"

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/
"

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.




回答3:


Try with this:

echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0


来源:https://stackoverflow.com/questions/19915208/extending-terminal-colors-to-the-end-of-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!