.bashrc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. # define pretty man pages colors
  3. man() {
  4. env \
  5. LESS_TERMCAP_md=$'\e[1;36m' \
  6. LESS_TERMCAP_me=$'\e[0m' \
  7. LESS_TERMCAP_se=$'\e[0m' \
  8. LESS_TERMCAP_so=$'\e[1;40;92m' \
  9. LESS_TERMCAP_ue=$'\e[0m' \
  10. LESS_TERMCAP_us=$'\e[1;32m' \
  11. man "$@"
  12. }
  13. # colors for enhanced prompt
  14. c_bold="\033[1m"
  15. c_red="\033[31m"
  16. c_green="\033[32m"
  17. c_blue="\033[34m"
  18. c_purple="\033[35m"
  19. c_cyan="\033[36m"
  20. c_sgr0="\033[0m"
  21. # promptify is an updated prompt enhancement enabled by default
  22. promptify() {
  23. local prompt_string
  24. prompt_string="${c_blue}${0} ($(date +'%R:%S.%3N'):${timer_show}s) ${c_purple}$(whoami)${c_sgr0}@${c_green}$(hostname) ${c_bold}${c_blue}$(dirs)${c_sgr0}"
  25. if git rev-parse --git-dir &> /dev/null; then
  26. git_branch=$(git branch 2> /dev/null | sed -n 's/^\*[ ]*//p')
  27. git_stats=$(git status --porcelain --untracked-files=all 2> /dev/null)
  28. if [ $(echo "${git_stats}" | sed '/^\s*$/d' | wc -l) -gt 0 ]; then
  29. prompt_string="${prompt_string} [${c_red}${git_branch}${c_sgr0}"
  30. untracked_count=$(echo "${git_stats}" | grep -ce "^??")
  31. new_file_count=$(echo "${git_stats}" | grep -ce "^A ")
  32. renamed_count=$(echo "${git_stats}" | grep -ce "^R ")
  33. modified_staged_count=$(echo "${git_stats}" | grep -ce "^M ")
  34. modified_unstaged_count=$(echo "${git_stats}" | grep -ce "^ M")
  35. deleted_staged_count=$(echo "${git_stats}" | grep -ce "^D ")
  36. deleted_unstaged_count=$(echo "${git_stats}" | grep -ce "^ D")
  37. [ $(($untracked_count + $new_file_count + $renamed_count + $modified_staged_count + $modified_unstaged_count + $deleted_staged_count + $deleted_unstaged_count)) -gt 0 ] && prompt_string="${prompt_string}:"
  38. [ $untracked_count -gt 0 ] && prompt_string="${prompt_string} ${c_red}u${untracked_count}${c_sgr0}"
  39. [ $new_file_count -gt 0 ] && prompt_string="${prompt_string} ${c_green}a${new_file_count}${c_sgr0}"
  40. [ $renamed_count -gt 0 ] && prompt_string="${prompt_string} ${c_green}r${renamed_count}${c_sgr0}"
  41. [ $modified_unstaged_count -gt 0 ] || [ $modified_staged_count -gt 0 ] && prompt_string="${prompt_string} ${c_red}m${c_sgr0}"
  42. [ $modified_unstaged_count -gt 0 ] && prompt_string="${prompt_string}${c_red}${modified_unstaged_count}${c_sgr0}" && [ $modified_staged_count -gt 0 ] && prompt_string="${prompt_string}/"
  43. [ $modified_staged_count -gt 0 ] && prompt_string="${prompt_string}${c_green}${modified_staged_count}${c_sgr0}"
  44. [ $deleted_unstaged_count -gt 0 ] || [ $deleted_staged_count -gt 0 ] && prompt_string="${prompt_string} ${c_red}d${c_sgr0}"
  45. [ $deleted_unstaged_count -gt 0 ] && prompt_string="${prompt_string}${c_red}${deleted_unstaged_count}${c_sgr0}" && [ $deleted_staged_count -gt 0 ] && prompt_string="${prompt_string}/"
  46. [ $deleted_staged_count -gt 0 ] && prompt_string="${prompt_string}${c_green}${deleted_staged_count}${c_sgr0}"
  47. prompt_string="${prompt_string}]"
  48. else
  49. prompt_string="${prompt_string} [${c_green}${git_branch}${c_sgr0}]"
  50. fi
  51. fi
  52. echo -ne "\n${prompt_string}\n "
  53. }
  54. # time every command
  55. timer_start() {
  56. timer=${timer:-$SECONDS}
  57. }
  58. timer_stop() {
  59. timer_show=$(($SECONDS - $timer))
  60. unset timer
  61. }
  62. trap 'timer_start' DEBUG
  63. # apply prompt enhancements
  64. PS2='continue-> '
  65. PS1='$(promptify)'
  66. if [ -z "$PROMPT_COMMAND" ]; then
  67. PROMPT_COMMAND="timer_stop"
  68. else
  69. PROMPT_COMMAND="$PROMPT_COMMAND; timer_stop"
  70. fi
  71. # load bash auto-completion and command-not-found
  72. . /usr/share/bash-completion/bash_completion
  73. . /usr/share/doc/pkgfile/command-not-found.bash
  74. # use shared agent to load ssh-keys; expects systemd to manage agent
  75. export SSH_AUTH_SOCK="$HOME/.ssh/socket"
  76. ssh-add -l &> /dev/null || ssh-add