Search code examples
bashsedprompt

Bash prompt with background color extending to end of line


It's bash-prompt-fiddling-time for me (had to happen one day...).

I'm trying to get a 2-line prompt:

  • 1st line with location info, and background color to the end of line
  • 2nd line with time and exit code of previous command

I'm almost there, but I can't crack the "background color to the end of line" part. Not quite.

Putting together bits of info from several sources, and most importantly from here and here, I get this result (terminal screenshot).

As you can see, something's wrong with the COLUMNS calculations:

  • it does not reach the end of line
  • it depends on the text length on the 1st line
  • it gets worse when reaching the bottom of the terminal; it does reach the end of line then; the wrong line...
  • another weird thing: the tiny [ ] which are bracketing the 2nd prompt line; and those ONLY APPEAR AFTER A COMMAND IS ENTERED

This is my bashrc code:

PROMPT_COMMAND=__prompt_command
__prompt_command()
{
    local EXIT="$?" 

    local Red='\[\033[1;38;5;9m\]'
    local Green='\[\033[1;38;5;10m\]'
    local Gray='\[\033[0;38;5;248m\]'
    local BgBlue='\[\033[48;5;253;1;38;5;12m\]'
    local BgPurple='\[\033[48;5;253;1;38;5;93m\]'
    local None='\[\e[0m\]'

    PS1="${BgPurple}\u@\h:${BgBlue}\$PWD"

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

    COLUMNS=$(tput cols)
    PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\\(.\\{${COLUMNS}\\}\\) */\\1/"`

    PS1+="\n${Gray}\D{%F %T}"

    if [ $EXIT != 0 ]; then
        PS1+=" ${Red} O_o ${None}"      # Add red if exit code non 0
    else
        PS1+="${Green} ^_^ ${None}"
    fi
}

I tried more hacking but no success.

Oh, there another more sophisticated version of the sed bit, which I also tried:

    PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\\(\\(\\o33\\[[0-9;]*[a-zA-Z]\\)*\\)\\([^\o033]\\{${COLUMNS}\\}\\) */\\1\\3/"`

Different result (terminal screenshot) but still not OK.

At this point I'm taking any help !


Solution

  • Instead of:

    printf -v TC_SPC "%${COLUMNS}s" ''
    
    COLUMNS=$(tput cols)
    PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\\(.\\{${COLUMNS}\\}\\) */\\1/"`
    

    Use:

    PS1+=$'\033[K' #erase to end of line