Advertisement
goatbar

bash prompt and logging of bash history with path/git branch

Sep 10th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.99 KB | None | 0 0
  1. #!/bin/bash
  2. # Custom bash prompt from: http://www.linuxdoc.org/HOWTO/Bash-Prompt-HOWTO/x848.html
  3. #   termwide prompt with tty number
  4. #      by Giles - created 2 November 98, last tweaked 31 July 2001
  5. #
  6. #     This is a variant on "termwide" that incorporates the tty number.
  7. #
  8.  
  9. hostnam=$(hostname -s)
  10. usernam=$(whoami)
  11. ##temp="$(tty)"
  12. #   Chop off the first five chars of tty (ie /dev/):
  13. ##cur_tty="${temp:5}"
  14. unset temp
  15.  
  16. function prompt_command {
  17.   #   Find the width of the prompt:
  18.   TERMWIDTH=${COLUMNS}
  19.  
  20.   # Replace home dir with ~. The # prior to ${HOME} restricts the match
  21.   # to the start of the string only.
  22.   TILDEPWD="${PWD/#${HOME}/~}"
  23.  
  24.   # Git branch name
  25.   gitBRANCH=$(__git_ps1 "[%s]")
  26.   #gitBRANCH=UNKNOWN
  27.   # Fixup for |BISECTING noise that started with git 1.6.5
  28.   gitBRANCH="${gitBRANCH/|BISECTING/}"
  29.  
  30.  
  31.   #   Add all the accessories below ...
  32.   local temp="--(${TILDEPWD})---\D{%a %H:%M:%S}--${gitBRANCH}--(${usernam}@${hostnam})--"
  33.  
  34.   let fillsize=${TERMWIDTH}-${#temp}
  35.   if [ "$fillsize" -gt "0" ]; then
  36.       fill="                                                                                                                                                                                                                                                                                      "
  37.     #   It's theoretically possible someone could need more
  38.       #   dashes than above, but very unlikely!  HOWTO users,
  39.     #   the above should be ONE LINE, it may not cut and
  40.       #   paste properly
  41.     fill="${fill:0:${fillsize}}"
  42.     newPWD="${TILDEPWD}"
  43.   fi
  44.  
  45.   if [ "$fillsize" -lt "0" ]; then
  46.       fill=""
  47.     let cut=3-${fillsize}
  48.       newPWD="...${TILDEPWD:${cut}}"
  49.   fi
  50.  
  51.   if [ "$(id -u)" -ne 0 ]; then echo "[$(__git_ps1 "%s")]" `date` `pwd` `history 1` >> ~/.shell.log; fi
  52.  
  53.   twtty
  54. }
  55.  
  56. PROMPT_COMMAND=prompt_command
  57.  
  58. function twtty {
  59.   local NO_COLOR="\[\e[0m\]"
  60.  
  61.   local BG_BLACK="40;"
  62.   local BG_RED="41;"
  63.   local BG_GREEN="42;"
  64.   local BG_YELLOW="43;"
  65.   local BG_BLUE="44;"
  66.   local BG_MAGENTA="45;"
  67.   local BG_CYAN="46;"
  68.   local BG_WHITE="47;"
  69.  
  70.   local FG_BLACK="1;30m"
  71.   local FG_RED="1;31m"
  72.   local FG_GREEN="1;32m"
  73.   local FG_YELLOW="1;33m"
  74.   local FG_BLUE="1;34m"
  75.   local FG_MAGENTA="1;35m"
  76.   local FG_CYAN="1;36m"
  77.   local FG_WHITE="1;37m"
  78.  
  79.   local COLOR1="\[\e[${BG_GREEN}${FG_BLACK}\]"
  80.   local COLOR2="\[\e[${BG_GREEN}${FG_WHITE}\]"
  81.  
  82.   if [ -z $gitBRANCH ]; then
  83.     COLOR1="\[\e[${BG_WHITE}${FG_BLACK}\]"
  84.     COLOR2="\[\e[${BG_WHITE}${FG_BLACK}\]"
  85.   fi
  86.  
  87.   case $TERM in
  88.     xterm*|rxvt*)
  89.         if [ -z $gitBRANCH ]; then
  90.           TITLEBAR='\[\e]0;\u@\h: (\w)\007\]'
  91.         else
  92.           TITLEBAR='\[\e]0;\u@\h: (\w) - ${gitBRANCH}\007\]'
  93.         fi
  94.         ;;
  95.     *)
  96.         TITLEBAR=""
  97.         ;;
  98.   esac
  99.  
  100. # Bugger... TODO: put  a # in as the prompt
  101. export PS1="$TITLEBAR\
  102. ${COLOR2}  (\${newPWD})\
  103. ${COLOR1}   \D{%a %H:%M:%S}   ${COLOR2}\${gitBRANCH}${COLOR1}   \${fill}\
  104. (\$usernam@\$hostnam)  ${NO_COLOR}\nLaptop $ "
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement