bashrc-linux 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ## Enable color support of ls and also add handy aliases
  2. # Some colors
  3. alias ls='ls --color=auto'
  4. alias grep='grep --color=auto'
  5. ## Some export
  6. # colored GCC warnings and errors
  7. export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
  8. # Set history size to unlimited
  9. if [[ $EUID == 0 ]] ; then
  10. export HISTSIZE=0
  11. export HISTFILESIZE=0
  12. else
  13. export HISTSIZE=-1
  14. export HISTFILESIZE=-1
  15. fi
  16. #export LANG=en_US.UTF-8
  17. # Used for vim header
  18. export VIUSER=bchanot
  19. export VIMAIL=bchanot@gmail.fr
  20. ## Activate and custom bash completion
  21. #bind 'TAB:menu-complete'
  22. #bind 'set show-all-if-ambiguous on'
  23. ## Setting prompt style
  24. # Get current branch in git repo
  25. function parse_git_branch() {
  26. BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
  27. if [ ! "${BRANCH}" == "" ]
  28. then
  29. STAT=`parse_git_dirty`
  30. echo " [${BRANCH}${STAT}]"
  31. else
  32. echo ""
  33. fi
  34. }
  35. function timer_now {
  36. date +%s%N
  37. }
  38. function timer_start {
  39. timer_start=${timer_start:-$(timer_now)}
  40. }
  41. function timer_stop {
  42. local delta_us=$((($(timer_now) - $timer_start) / 1000))
  43. local us=$((delta_us % 1000))
  44. local ms=$(((delta_us / 1000) % 1000))
  45. local s=$(((delta_us / 1000000) % 60))
  46. local m=$(((delta_us / 60000000) % 60))
  47. local h=$((delta_us / 3600000000))
  48. # Goal: always show around 3 digits of accuracy
  49. if ((h > 0)); then timer_show=${h}h${m}m
  50. elif ((m > 0)); then timer_show=${m}m${s}s
  51. elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
  52. elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
  53. elif ((ms >= 100)); then timer_show=${ms}ms
  54. elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
  55. else timer_show=${us}us
  56. fi
  57. unset timer_start
  58. }
  59. # get current status of git repo
  60. function parse_git_dirty {
  61. status=`git status 2>&1 | tee`
  62. dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
  63. untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
  64. ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
  65. newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
  66. renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
  67. deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
  68. bits=''
  69. if [ "${renamed}" == "0" ] || [ "${newfile}" == "0" ] || [ "${untracked}" == "0" ] || [ "${dirty}" == "0" ]; then
  70. bits="+${bits}"
  71. fi
  72. if [ "${ahead}" == "0" ]; then
  73. bits="*${bits}"
  74. fi
  75. if [ "${deleted}" == "0" ]; then
  76. bits="-${bits}"
  77. fi
  78. if [ ! "${bits}" == "" ]
  79. then
  80. echo " ${bits}"
  81. else
  82. echo ""
  83. fi
  84. }
  85. function set_prompt {
  86. timer_stop
  87. if [[ $EUID == 0 ]] ; then
  88. export PS1='`if [ $? = 0 ]; then echo "\[\033[01;36m\]✔"; else echo "\[\033[01;31m\]✘"; fi` ($timer_show) \[\033[01;31m\]\u [\[\033[00;0m\] \w \[\033[01;31m\]]\[\033[01;34m\]$(parse_git_branch " %s") \[\033[00;00m\]> '
  89. else
  90. export PS1='`if [ $? = 0 ]; then echo "\[\033[01;36m\]✔"; else echo "\[\033[01;31m\]✘"; fi` ($timer_show) \[\033[01;32m\]\u [\[\033[00;0m\] \w \[\033[01;32m\]]\[\033[01;34m\]$(parse_git_branch " %s") \[\033[00;00m\]> '
  91. fi
  92. }
  93. trap 'timer_start' DEBUG
  94. PROMPT_COMMAND='set_prompt'
  95. ## Lancement des commandes au demarrages