prompt 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. ##################################################
  3. # Fancy PWD display function
  4. ##################################################
  5. # The home directory (HOME) is replaced with a ~
  6. # The last pwdmaxlen characters of the PWD are displayed
  7. # Leading partial directory names are striped off
  8. # /home/me/stuff -> ~/stuff if username=me
  9. # /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
  10. ##################################################
  11. rewrite_pwd()
  12. {
  13. # how many characters of the $PWD should be kept
  14. local pwdmaxlen=25
  15. # indicate that there has been dir truncation
  16. local trunc_symbol=".."
  17. local dir=${PWD##*/}
  18. pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
  19. NEW_PWD=${PWD/$HOME/~}
  20. local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
  21. if [ ${pwdoffset} -gt "0" ]
  22. then
  23. NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
  24. NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
  25. fi
  26. }
  27. # rewrites the PS1 bash prompt var
  28. rewrite_prompt()
  29. {
  30. local NONE='\[\033[0m\]' # unsets color to term's fg color
  31. # regular colors
  32. local K='\[\033[0;30m\]' # black
  33. local R='\[\033[0;31m\]' # red
  34. local G='\[\033[0;32m\]' # green
  35. local Y='\[\033[0;33m\]' # yellow
  36. local B='\[\033[0;34m\]' # blue
  37. local M='\[\033[0;35m\]' # magenta
  38. local C='\[\033[0;36m\]' # cyan
  39. local W='\[\033[0;37m\]' # white
  40. # empahsized (bolded) colors
  41. local EMK='\[\033[1;30m\]'
  42. local EMR='\[\033[1;31m\]'
  43. local EMG='\[\033[1;32m\]'
  44. local EMY='\[\033[1;33m\]'
  45. local EMB='\[\033[1;34m\]'
  46. local EMM='\[\033[1;35m\]'
  47. local EMC='\[\033[1;36m\]'
  48. local EMW='\[\033[1;37m\]'
  49. # background colors
  50. local BGK='\[\033[40m\]'
  51. local BGR='\[\033[41m\]'
  52. local BGG='\[\033[42m\]'
  53. local BGY='\[\033[43m\]'
  54. local BGB='\[\033[44m\]'
  55. local BGM='\[\033[45m\]'
  56. local BGC='\[\033[46m\]'
  57. local BGW='\[\033[47m\]'
  58. local UC=$C # username's color
  59. [ $UID -eq "0" ] && UC=$R # root's color
  60. # rewrite prompt
  61. PS1="${G}\u@\h> \${NEW_PWD} ${C}\$(git_branch)\${NONE}${NONE}\n$ "
  62. }
  63. # adds ~/.ssh/config to the ssh autocomplete
  64. ssh_load_autocomplete()
  65. {
  66. complete -W "$(awk '/^\s*Host\s*/ { sub(/^\s*Host /, ""); print; }' ~/.ssh/config)" ssh
  67. }
  68. # adds Git autocompletion
  69. if [ -f ~/.bin/git-completion.bash ]; then
  70. . ~/.bin/git-completion.bash
  71. fi
  72. PROMPT_COMMAND=rewrite_pwd
  73. rewrite_prompt