]> git.phdru.name Git - dotfiles.git/blob - .bash_prompt
.bash_prompt: rename purple to magenta
[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 dim=""
71     local green=""
72     local orange=""
73     local magenta=""
74     local red=""
75     local reset=""
76     local white=""
77     local yellow=""
78
79     local dim_black=""
80     local dim_blue=""
81     local dim_bold=""
82     local dim_cyan=""
83     local dim_green=""
84     local dim_orange=""
85     local dim_magenta=""
86     local dim_red=""
87     local dim_white=""
88     local dim_yellow=""
89
90     local hostStyle=""
91     local userStyle=""
92
93     if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
94         tput sgr0 # reset colors
95
96         bold=$(tput bold)
97         dim=$(tput dim)
98         reset=$(tput sgr0)
99
100         # Solarized colors
101         # (https://github.com/altercation/solarized/tree/master/iterm2-colors-solarized#the-values)
102         black=$(tput setaf 0)
103         blue=$(tput setaf 33)
104         cyan=$(tput setaf 37)
105         green=$(tput setaf 64)
106         orange=$(tput setaf 166)
107         red=$(tput setaf 124)
108         white=$(tput setaf 15)
109         yellow=$(tput setaf 136)
110         magenta=$(tput setaf 5)
111
112         dim_black=$dim$black
113         dim_blue=$dim$blue
114         dim_cyan=$dim$cyan
115         dim_green=$dim$green
116         dim_orange=$dim$orange
117         dim_magenta=$dim$magenta
118         dim_red=$dim$red
119         dim_white=$dim$white
120         dim_yellow=$dim$yellow
121     else
122         bold=""
123         dim=""
124         reset="\e[0m"
125
126         black="\e[1;30m"
127         blue="\e[1;34m"
128         cyan="\e[1;36m"
129         green="\e[1;32m"
130         orange="\e[1;33m"
131         red="\e[1;31m"
132         white="\e[1;37m"
133         yellow="\e[1;33m"
134         magenta="\e[0;35m"
135
136         dim_black="\e[1;30m"
137         dim_blue="\e[1;34m"
138         dim_cyan="\e[1;36m"
139         dim_green="\e[1;32m"
140         dim_orange="\e[1;33m"
141         dim_magenta="\e[1;35m"
142         dim_red="\e[1;31m"
143         dim_white="\e[1;37m"
144         dim_yellow="\e[1;33m"
145     fi
146
147     # build the prompt
148
149     # logged in as root
150     if [[ "$USER" == "root" ]]; then
151         userStyle="\[$bold$red\]"
152     else
153         userStyle="\[$orange\]"
154     fi
155
156     # connected via ssh
157     if [[ "$SSH_TTY" ]]; then
158         hostStyle="\[$bold$red\]"
159     else
160         hostStyle="\[$yellow\]"
161     fi
162
163     # set the terminal title to the current working directory
164     PS1="\[\033]0;\w\007\]"
165
166     PS1+="\n" # newline
167     PS1+="\[$userStyle\]\u" # username
168     PS1+="\[$reset$white\]@"
169     PS1+="\[$hostStyle\]\h" # host
170     PS1+="\[$reset$white\]: "
171     PS1+="\[$green\]\w" # working directory
172     PS1+="\$(prompt_git \"$white on $cyan\")" # git repository details
173     PS1+="\n"
174     PS1+="\[$reset$white\]\$ \[$reset\]" # $ (and reset color)
175
176     export PS1
177 }
178
179 set_prompts
180 unset set_prompts