The very personal dotfiles of Levi Olson.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2 KiB

6 years ago
  1. #!/bin/bash
  2. # Shell prompt based on the amazing work of Jess Frazelle.
  3. # Screenshot: http://i.imgur.com/EkEtphC.png
  4. if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
  5. export TERM='gnome-256color';
  6. elif infocmp xterm-256color >/dev/null 2>&1; then
  7. export TERM='xterm-256color';
  8. fi;
  9. prompt_git() {
  10. local s='';
  11. local branchName='';
  12. # Check if the current directory is in a Git repository.
  13. if [ "$(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}")" == '0' ]; then
  14. # check if the current directory is in .git before running git checks
  15. if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
  16. if [[ -O "$(git rev-parse --show-toplevel)/.git/index" ]]; then
  17. git update-index --really-refresh -q &> /dev/null;
  18. fi;
  19. # Check for uncommitted changes in the index.
  20. if ! git diff --quiet --ignore-submodules --cached; then
  21. s+='+';
  22. fi;
  23. # Check for unstaged changes.
  24. if ! git diff-files --quiet --ignore-submodules --; then
  25. s+='!';
  26. fi;
  27. # Check for untracked files.
  28. if [ -n "$(git ls-files --others --exclude-standard)" ]; then
  29. s+='?';
  30. fi;
  31. # Check for stashed files.
  32. if git rev-parse --verify refs/stash &>/dev/null; then
  33. s+='$';
  34. fi;
  35. fi;
  36. # Get the short symbolic ref.
  37. # If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
  38. # Otherwise, just give up.
  39. branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
  40. git rev-parse --short HEAD 2> /dev/null || \
  41. echo '(unknown)')";
  42. [ -n "${s}" ] && s=" [${s}]";
  43. echo -e "${1}${branchName}${blue}${s}";
  44. else
  45. return;
  46. fi;
  47. }
  48. cloud=""
  49. if grep -q "^flags.* hypervisor" /proc/cpuinfo && [[ ! -d "/mnt/c/Windows/" ]]; then
  50. cloud="☁️ "
  51. fi
  52. if tput setaf 1 &> /dev/null; then
  53. tput sgr0; # reset colors
  54. bold=$(tput bold);
  55. reset=$(tput sgr0);
  56. # Solarized colors, taken from http://git.io/solarized-colors.
  57. black=$(tput setaf 0);
  58. blue=$(tput setaf 33);
  59. cyan=$(tput setaf 37);
  60. green=$(tput setaf 64);
  61. orange=$(tput setaf 166);
  62. purple=$(tput setaf 125);
  63. red=$(tput setaf 124);
  64. violet=$(tput setaf 61);
  65. white=$(tput setaf 15);
  66. yellow=$(tput setaf 136);
  67. else
  68. bold='';
  69. reset="\\e[0m";
  70. # shellcheck disable=SC2034
  71. black="\\e[1;30m";
  72. blue="\\e[1;34m";
  73. cyan="\\e[1;36m";
  74. green="\\e[1;32m";
  75. # shellcheck disable=SC2034
  76. orange="\\e[1;33m";
  77. # shellcheck disable=SC2034
  78. purple="\\e[1;35m";
  79. red="\\e[1;31m";
  80. violet="\\e[1;35m";
  81. white="\\e[1;37m";
  82. yellow="\\e[1;33m";
  83. fi;
  84. # Highlight the user name when logged in as root.
  85. if [[ "${USER}" == "root" ]]; then
  86. userStyle="${red}";
  87. else
  88. userStyle="${blue}";
  89. fi;
  90. # Highlight the hostname when connected via SSH.
  91. if [[ "${SSH_TTY}" ]]; then
  92. hostStyle="${bold}${cyan}";
  93. else
  94. hostStyle="${cyan}";
  95. fi;
  96. # Set the terminal title to the current working directory.
  97. PS1="\\[\\033]0;\\w\\007\\]";
  98. PS1+="\\[${bold}\\]\\n"; # newline
  99. PS1+="\\[${userStyle}\\]\\u"; # username
  100. PS1+="\\[${white}\\] at ";
  101. PS1+="\\[${hostStyle}\\]${cloud}\\h"; # host
  102. PS1+="\\[${white}\\] in ";
  103. PS1+="\\[${green}\\]\\w"; # working directory
  104. PS1+="\$(prompt_git \"${white} on ${violet}\")"; # Git repository details
  105. PS1+="\\n";
  106. PS1+="\\[${white}\\]\$ \\[${reset}\\]"; # `$` (and reset color)
  107. export PS1;
  108. PS2="\\[${yellow}\\]→ \\[${reset}\\]";
  109. export PS2;