title: Bash prompt with background color extending to end of line
body: <p>It's bash-prompt-fiddling-time for me (had to happen one day...).</p>
  
  <p>I'm trying to get a 2-line prompt:</p>
  
  <ul>
  <li>1st line with location info, and background color to the end of line</li>
  <li>2nd line with time and exit code of previous command</li>
  </ul>
  
  <p>I'm almost there, but I can't crack the "background color to the end of line" part. Not quite.</p>
  
  <p>Putting together bits of info from several sources, and most importantly from <a href="https://stackoverflow.com/questions/16715103/bash-prompt-with-last-exit-code">here</a> and <a href="https://stackoverflow.com/questions/19915208/extending-terminal-colors-to-the-end-of-line">here</a>, I get <a href="https://i.stack.imgur.com/wnNwu.png" rel="nofollow noreferrer">this result (terminal screenshot)</a>.</p>
  
  <p>As you can see, something's wrong with the COLUMNS calculations:</p>
  
  <ul>
  <li>it does not reach the end of line</li>
  <li>it depends on the text length on the 1st line</li>
  <li>it gets worse when reaching the bottom of the terminal; it does reach the end of line then; the wrong line...</li>
  <li>another weird thing: the tiny [ ] which are bracketing the 2nd prompt line; and those ONLY APPEAR AFTER A COMMAND IS ENTERED</li>
  </ul>
  
  <p>This is my bashrc code:</p>
  
  <pre><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
  }
  </code></pre>
  
  <p>I tried more hacking but no success.</p>
  
  <p>Oh, there another more sophisticated version of the sed bit, which I also tried:</p>
  
  <pre><code>    PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\\(\\(\\o33\\[[0-9;]*[a-zA-Z]\\)*\\)\\([^\o033]\\{${COLUMNS}\\}\\) */\\1\\3/"`
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FFlag.png" rel="nofollow noreferrer">Different result (terminal screenshot)</a> but still not OK.</p>
  
  <p>At this point I'm taking any help !</p>