bash_prompt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Prompt ideas freely used and stolen from:
  2. # ---
  3. # * http://blog.bigdinosaur.org/easy-ps1-colors/
  4. # * https://github.com/eprev/dotfiles/blob/master/includes/prompt.bash
  5. # * http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
  6. # * https://github.com/mathiasbynens/dotfiles/blob/master/.bash_prompt
  7. # * https://github.com/sindresorhus/pure
  8. # * http://ebeab.com/2014/04/17/the-bash-prompt/
  9. #
  10. color_prompt=0
  11. # set a fancy prompt (non-color, unless we know we "want" color)
  12. case "$TERM" in
  13. *color*) color_prompt=1;;
  14. rxvt-unicode) color_prompt=1;;
  15. esac
  16. RED=''
  17. CYAN=''
  18. GREEN=''
  19. MAGENTA=''
  20. ORANGE=''
  21. PURPLE=''
  22. WHITE=''
  23. LIME_YELLOW=''
  24. POWDER_BLUE=''
  25. RESET=''
  26. if (( color_prompt > 0 )); then
  27. RED=$(tput setaf 1)
  28. CYAN=$(tput setaf 6)
  29. GREEN=$(tput setaf 2)
  30. MAGENTA=$(tput setaf 9)
  31. ORANGE=$(tput setaf 172)
  32. PURPLE=$(tput setaf 141)
  33. WHITE=$(tput setaf 254)
  34. LIME_YELLOW=$(tput setaf 190)
  35. POWDER_BLUE=$(tput setaf 153)
  36. RESET=$(tput sgr0)
  37. fi
  38. _rprompt() {
  39. local _time=$1
  40. (( $_time < 5 )) && return
  41. local _out
  42. local days=$(( $_time / 60 / 60 / 24 ))
  43. local hours=$(( $_time / 60 / 60 % 24 ))
  44. local minutes=$(( $_time / 60 % 60 ))
  45. local seconds=$(( $_time % 60 ))
  46. (( $days > 0 )) && _out="${days}d"
  47. (( $hours > 0 )) && _out="$_out ${hours}h"
  48. (( $minutes > 0 )) && _out="$_out ${minutes}m"
  49. _out="$_out ${seconds}s"
  50. printf "${RED}$_out${RESET}"
  51. }
  52. exit_code_emote() {
  53. case $1 in
  54. # 0) prompt="(•◡•)❥";; # success
  55. 0) prompt="(/^ヮ^)/*:・゚✧";; # success
  56. 127) prompt="(•ิ_•ิ)❥" ;; # command not found
  57. 130) prompt="(☉_☉)❥" ;; # ^C
  58. # 148) prompt="ᕕ( ᐛ )ᕗ" ;;
  59. 148) prompt="(°▽°)❥" ;; # ^z
  60. *) prompt="(•́‸•̀)❥" ;; # Other errors
  61. # *) prompt="(•̀д•́)❥" ;;
  62. esac
  63. }
  64. prompt() {
  65. local _exitcode=$?
  66. local _end_time=$(date +%s)
  67. local _total_time=$(( _end_time - _start_time ));
  68. if ((_end_time == _start_time || _start_time == 0)); then
  69. _total_time=0
  70. fi
  71. unset _start_time
  72. exit_code_emote $_exitcode
  73. if [ $_exitcode -eq 0 ]; then
  74. color=${GREEN}
  75. else
  76. color=${RED}
  77. fi
  78. venv=''
  79. if [ -n "$VIRTUAL_ENV" ]; then
  80. venv="(\[${BRIGHT}${MAGENTA}\]$(basename $VIRTUAL_ENV)\[${RESET}\])"
  81. fi
  82. GIT_PS1_SHOWCOLORHINTS=1
  83. GIT_PS1_SHOWUNTRACKEDFILES=1
  84. GIT_PS1_SHOWDIRTYSTATE=1
  85. GIT_PS1_SHOWUPSTREAM="auto verbose"
  86. . "$HOME"/bin/git-prompt.sh
  87. # __git_ps1 here is provided by ~/bin/git-prompt.sh
  88. _top_row="\[${CYAN}\]\W\[${RESET}\]$(__git_ps1 ' (%s)')\[${RESET}\]"
  89. [[ "$SSH_CONNECTION" != '' ]] && \
  90. _top_row="\[${BRIGHT}${MAGENTA}\]\u\[${RESET}\]@\[${ORANGE}\]\h\[${RESET}\]:${_top_row}"
  91. # _bottom_row="${venv}\[${color}\]❯\[${RESET}\] "
  92. _bottom_row="${venv}\[${color}\]${prompt}\[${RESET}\] "
  93. PS1="${_top_row}\$(_rprompt $_total_time)\n${_bottom_row}"
  94. }
  95. _log_start() {
  96. local hist
  97. [ -n "$COMP_LINE" ] && return # do nothing if completing
  98. [[ "$BASH_COMMAND" == "$PROMPT_COMMAND" ]] && return # don't cause a preexec for $PROMPT_COMMAND
  99. hist=$(HISTTIMEFORMAT='%s ' history 1 | { read -r _ this_command; echo "$this_command"; })
  100. [[ -n "$hist" ]] && {
  101. # https://www.debian-administration.org/article/543/Bash_eternal_history
  102. echo "$$ $PWD $USER $hist $BASH_COMMAND" >> "$HOME/.muh_history"
  103. }
  104. _start_time=$(date +%s)
  105. }
  106. trap '_log_start' DEBUG
  107. PROMPT_COMMAND=prompt
  108. PS2="\[$ORANGE\]→ \[$RESET\]"