Extending terminal colors to the end of line

后端 未结 3 1023
温柔的废话
温柔的废话 2021-02-02 11:49

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. Othertime

相关标签:
3条回答
  • 2021-02-02 12:28

    Try with this:

    echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0
    
    0 讨论(0)
  • 2021-02-02 12:40

    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}
    
    0 讨论(0)
  • 2021-02-02 12:48

    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.

    0 讨论(0)
提交回复
热议问题