TYPO free

home

fighting for TYPO free code

Happy colorful prompt

I live on the command line quite a lot. Sometimes I wonder how the machine I am working on is holding up. A nice command to see what is going on is 'top'. Although it's quite resource hungry. A lighter alternative is 'uptime'. I don't like typing that command very often however.

There are some very nice BASH prompt tutorials in the pipes and tubes out there and there are some ready made prompts to get you started. I adapted one of these snippets to my own taste. So I came up with this:

 

It shows the [email protected] followed by the 1, 5 and 15 minutes load average followed by the current directory. And it has nice colors to keep me happy.

Here's the how for a Linux machine:

  1. #!/bin/bash
  2.  
  3. #   termwide prompt
  4. #   by Giles - created 2 November 98
  5. #   adapted by Michiel Roos somewhere in 2007
  6. #
  7. #   The idea here is to have the upper line of this two line prompt
  8. #   always be the width of your term.  Do this by calculating the
  9. #   width of the text elements, and putting in fill as appropriate
  10. #   or left-truncating $PWD.
  11. #
  12.  
  13. function prompt_command {
  14.         # Calculate the width of the prompt:
  15.         hostnam=$(echo -n $HOSTNAME | sed -e "s/[\.].*//")
  16.         # "whoami" and "pwd" include a trailing newline
  17.         usernam=$(whoami)
  18.         let usersize=$(echo -n $usernam | wc -c | tr -d " ")
  19.         newPWD="${PWD}"
  20.         let pwdsize=$(echo -n ${newPWD} | wc -c | tr -d " ")
  21.  
  22.         # load monitor
  23.         load1=$(uptime | sed -e "s/.*average: \([^ ]*\), .*$/\1/")
  24.         load5=$(uptime | sed -e "s/.*average: [^ ]* \([^ ]*\), .*$/\1/")
  25.         load15=$(uptime | sed -e "s/.*average: [^ ]* [^ ]* \([^ ]*\)$/\1/")
  26.  
  27.         # Add all the accessories below ...
  28.         let promptsize=$(echo -n ".-(${usernam}@${hostnam}:${load1} ${load5} ${load15})---(${PWD}]" | wc -c | tr -d " ")
  29.         let fillsize=${COLUMNS}-${promptsize}
  30.         fill=""
  31.         while [ "$fillsize" -gt "0" ]
  32.         do
  33.                  fill="${fill}-"
  34.                  let fillsize=${fillsize}-1
  35.         done
  36.  
  37.         if [ "$fillsize" -lt "0" ]
  38.         then
  39.                  let cut=3-${fillsize}
  40.                  newPWD="...$(echo -n $PWD | sed -e "s/\(^.\{$cut\}\)\(.*\)/\2/")"
  41.         fi
  42. }
  43.  
  44. PROMPT_COMMAND=prompt_command
  45.  
  46. function termwide {
  47.         case $TERM in
  48.                 xterm*)
  49.                         TITLEBAR='\[\033]0;\u@\h:\w\007\]'
  50.                         ;;
  51.                 *)
  52.                         TITLEBAR=""
  53.                         ;;
  54.         esac
  55.  
  56. local       RED="\[\033[0;31m\]"
  57. local     GREEN="\[\033[0;32m\]"
  58. local    YELLOW="\[\033[0;33m\]"
  59. local      BLUE="\[\033[0;34m\]"
  60. local   MAGENTA="\[\033[0;35m\]"
  61. local      CYAN="\[\033[0;36m\]"
  62. local     WHITE="\[\033[0;37m\]"
  63. local NO_COLOUR="\[\033[0m\]"
  64.  
  65. local      FILL=$WHITE
  66. local  BRACKETS=$RED
  67. local      USER=$CYAN
  68. local        AT=$WHITE
  69. local      HOST=$GREEN
  70. local     PWDIR=$CYAN
  71. local     LOAD1=$RED
  72. local     LOAD5=$YELLOW
  73. local    LOAD15=$GREEN
  74.  
  75.         PS1="$TITLEBAR\
  76. $FILL.$BRACKETS-(\
  77. $USER\${usernam}[email protected]$HOST\${hostnam}\
  78. $AT:$LOAD1\${load1} $LOAD5\${load5} $LOAD15\${load15}$BRACKETS)-$FILL-\${fill}$BRACKETS-[\
  79. $PWDIR\${newPWD}\
  80. $BRACKETS]\
  81. \n\
  82. $FILL\$(cat ~/.bash_backtick)-\
  83. $BRACKETS_$FILL\$$NO_COLOUR "
  84.  
  85.         PS2="$BRACKETS-$FILL-$FILL-$NO_COLOUR "
  86. }

For OSX you will have to adapt it slightly because the uptime command gives slightly different output (maybe on Linux too, depending on your locale settings).

It all went fairly smoothly, until I had to put in the backtick character in the bottom left of the curve. Bash likes to eat backticks. So a workaround was needed. Near the end you see the following line that fixes that:

$FILL\$(cat ~/.bash_backtick)-\

The contents fo the .bash_backtick are surprise surprise . . . :

`

That's it. A nice prompt for your bash shell. There is still room for optimization sincethe uptime is called three times to get the load values. This could be reduced to one time.

Happy hacking!

Jos 21 Sep 2008, 13:43
I don't snap any reed of this.. ;)
Good Job Michiel! :)
Just say it . . . *



*
*
* required