]> git.phdru.name Git - dotfiles.git/blob - .bash_prompt
Add .bash_prompt
[dotfiles.git] / .bash_prompt
1 #!/bin/bash
2
3 # bash_prompt; adapted from
4 # https://github.com/necolas/dotfiles/blob/master/shell/bash_prompt
5
6 # Example:
7 # nicolas@host: ~/.dotfiles on master [+!?$]
8 # $
9
10 # Screenshot: http://i.imgur.com/DSJ1G.png
11 # iTerm2 prefs: import Solarized theme (disable bright colors for bold text)
12 # Color ref: http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim
13 # More tips: http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html
14
15 prompt_git() {
16     local s=""
17     local branchName=""
18
19     # check if the current directory is in a git repository
20     if [ $(git rev-parse --is-inside-work-tree &>/dev/null; printf "%s" $?) == 0 ]; then
21
22         # check if the current directory is in .git before running git checks
23         if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == "false" ]; then
24
25             # ensure index is up to date
26             git update-index --really-refresh  -q &>/dev/null
27
28             # check for uncommitted changes in the index
29             if ! $(git diff --quiet --ignore-submodules --cached); then
30                 s="$s+";
31             fi
32
33             # check for unstaged changes
34             if ! $(git diff-files --quiet --ignore-submodules --); then
35                 s="$s!";
36             fi
37
38             # check for untracked files
39             if [ -n "$(git ls-files --others --exclude-standard)" ]; then
40                 s="$s?";
41             fi
42
43             # check for stashed files
44             if $(git rev-parse --verify refs/stash &>/dev/null); then
45                 s="$s$";
46             fi
47
48         fi
49
50         # get the short symbolic ref
51         # if HEAD isn't a symbolic ref, get the short SHA
52         # otherwise, just give up
53         branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
54                       git rev-parse --short HEAD 2> /dev/null || \
55                       printf "(unknown)")"
56
57         [ -n "$s" ] && s=" [$s]"
58
59         printf "%s" "$1$branchName$s"
60     else
61         return
62     fi
63 }
64
65 set_prompts() {
66     local black=""
67     local blue=""
68     local bold=""
69     local cyan=""
70     local green=""
71     local orange=""
72     local purple=""
73     local red=""
74     local reset=""
75     local white=""
76     local yellow=""
77
78     local hostStyle=""
79     local userStyle=""
80
81     if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
82         tput sgr0 # reset colors
83
84         bold=$(tput bold)
85         reset=$(tput sgr0)
86
87         # Solarized colors
88         # (https://github.com/altercation/solarized/tree/master/iterm2-colors-solarized#the-values)
89         black=$(tput setaf 0)
90         blue=$(tput setaf 33)
91         cyan=$(tput setaf 37)
92         green=$(tput setaf 64)
93         orange=$(tput setaf 166)
94         purple=$(tput setaf 125)
95         red=$(tput setaf 124)
96         white=$(tput setaf 15)
97         yellow=$(tput setaf 136)
98     else
99         bold=""
100         reset="\e[0m"
101
102         black="\e[1;30m"
103         blue="\e[1;34m"
104         cyan="\e[1;36m"
105         green="\e[1;32m"
106         orange="\e[1;33m"
107         purple="\e[1;35m"
108         red="\e[1;31m"
109         white="\e[1;37m"
110         yellow="\e[1;33m"
111     fi
112
113     # build the prompt
114
115     # logged in as root
116     if [[ "$USER" == "root" ]]; then
117         userStyle="\[$bold$red\]"
118     else
119         userStyle="\[$orange\]"
120     fi
121
122     # connected via ssh
123     if [[ "$SSH_TTY" ]]; then
124         hostStyle="\[$bold$red\]"
125     else
126         hostStyle="\[$yellow\]"
127     fi
128
129     # set the terminal title to the current working directory
130     PS1="\[\033]0;\w\007\]"
131
132     PS1+="\n" # newline
133     PS1+="\[$userStyle\]\u" # username
134     PS1+="\[$reset$white\]@"
135     PS1+="\[$hostStyle\]\h" # host
136     PS1+="\[$reset$white\]: "
137     PS1+="\[$green\]\w" # working directory
138     PS1+="\$(prompt_git \"$white on $cyan\")" # git repository details
139     PS1+="\n"
140     PS1+="\[$reset$white\]\$ \[$reset\]" # $ (and reset color)
141
142     export PS1
143 }
144
145 set_prompts
146 unset set_prompts