]> git.phdru.name Git - dotfiles.git/blob - .bash_prompt
.vim/plugin/tmpwatch.vim: echo -> echoerr
[dotfiles.git] / .bash_prompt
1 #!/bin/bash
2
3 # bash_prompt; adapted from
4 # https://github.com/necolas/dotfiles/blob/master/shell/bash_prompt and
5 # http://vitus-wagner.livejournal.com/1176589.html?thread=40146189#t40146189
6
7 prompt_git() {
8     local s=""
9     local branchName=""
10
11     # check if the current directory is in a git repository
12     if [ $(git rev-parse --is-inside-work-tree &>/dev/null; printf "%s" $?) == 0 ]; then
13
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
17             # ensure index is up to date
18             #git update-index --really-refresh  -q &>/dev/null
19
20             # check for uncommitted changes in the index
21             if ! $(git diff --quiet --ignore-submodules --cached); then
22                 s="$s+";
23             fi
24
25             # check for unstaged changes
26             if ! $(git diff-files --quiet --ignore-submodules --); then
27                 s="$s!";
28             fi
29
30             # check for untracked files
31             if [ -n "$(git ls-files --others --exclude-standard)" ]; then
32                 s="$s?";
33             fi
34
35             # check for stashed files
36             if $(git rev-parse --verify refs/stash &>/dev/null); then
37                 s="$s$";
38             fi
39
40         fi
41
42         # get the short symbolic ref
43         # if HEAD isn't a symbolic ref, get the short SHA
44         # otherwise, just give up
45         branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
46                       git rev-parse --short HEAD 2> /dev/null || \
47                       printf "(unknown)")"
48
49         [ -n "$s" ] && s=" [$s]"
50
51         printf "%s" "$1$branchName$s"
52     else
53         return
54     fi
55 }
56
57 set_prompts() {
58     local black=""
59     local blue=""
60     local bold=""
61     local cyan=""
62     local dim=""
63     local green=""
64     local orange=""
65     local magenta=""
66     local red=""
67     local reset=""
68     local white=""
69     local yellow=""
70
71     local dim_black=""
72     local dim_blue=""
73     local dim_bold=""
74     local dim_cyan=""
75     local dim_green=""
76     local dim_orange=""
77     local dim_magenta=""
78     local dim_red=""
79     local dim_white=""
80     local dim_yellow=""
81
82     local charStyle=""
83     local gitStyle=""
84     local hostStyle=""
85     local userStyle=""
86     local wdirStyle=""
87
88     if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
89         tput sgr0 # reset colors
90
91         bold=$(tput bold)
92         dim=$(tput dim)
93         reset=$(tput sgr0)
94
95         black=$(tput setaf 0)
96         blue=$(tput setaf 4)
97         cyan=$(tput setaf 6)
98         green=$(tput setaf 2)
99         orange=$(tput setaf 3)
100         magenta=$(tput setaf 5)
101         red=$(tput setaf 1)
102         white=$bold$(tput setaf 7)
103         yellow=$(tput setaf 3)
104
105         dim_black=$dim$black
106         dim_blue=$dim$blue
107         dim_cyan=$dim$cyan
108         dim_green=$dim$green
109         dim_orange=$dim$orange
110         dim_magenta=$dim$magenta
111         dim_red=$dim$red
112         dim_white=$dim$white
113         dim_yellow=$dim$yellow
114     else
115         bold=""
116         dim=""
117         reset="\e[0m"
118
119         black="\e[0;30m"
120         blue="\e[0;34m"
121         cyan="\e[0;36m"
122         green="\e[0;32m"
123         orange="\e[0;33m"
124         magenta="\e[0;35m"
125         red="\e[0;31m"
126         white="\e[0;37m"
127         yellow="\e[0;33m"
128
129         dim_black="\e[1;30m"
130         dim_blue="\e[1;34m"
131         dim_cyan="\e[1;36m"
132         dim_green="\e[1;32m"
133         dim_orange="\e[1;33m"
134         dim_magenta="\e[1;35m"
135         dim_red="\e[1;31m"
136         dim_white="\e[1;37m"
137         dim_yellow="\e[1;33m"
138     fi
139
140     charStyle="$reset$bold$black"
141     gitStyle="\[$white\]on \[$magenta\]"
142     case "$BACKGROUND" in
143        DARK|dark)
144           hostStyle="$reset$cyan"
145        ;;
146        LIGHT|light)
147           hostStyle="$reset$blue"
148        ;;
149     esac
150     wdirStyle="$reset$green"
151
152
153     # logged in as root
154     if [[ "$USER" == "root" ]]; then
155         userStyle="$bold$red"
156     else
157         userStyle="$orange"
158     fi
159
160     # build the prompt
161     # display the user, host and current working directory in the terminal title
162     case "$TERM" in
163        *rxvt*|screen*|*term*|vt100)
164           PS1="\[\033]0;\u@\h:\w\007\]"
165           case "$TERM" in
166              screen*)
167                 PS1+"\[\033k\u@\h:\w\033\\\]" # Set screen/tmux caption
168              ;;
169           esac
170        ;;
171
172        *)
173           PS1=""
174        ;;
175     esac
176
177     #PS1+="\n" # newline
178     PS1+="\[$userStyle\]\u" # username
179     PS1+="\[$charStyle\]@"
180     PS1+="\[$hostStyle\]\h " # host
181     #PS1+="\[$charStyle\]: "
182     PS1+="\[$wdirStyle\]\W " # working directory
183     PS1+='`erco=$?; [ $erco == 0 ] && echo -n ":)" || echo -n '"\"\[$red\]:(\[$reset\] $\""'?=$erco` '
184     PS1+="\$(prompt_git \"$gitStyle\")" # git repository details
185     #PS1+="\n"
186     PS1+="\[$charStyle\]\$ \[$reset\]" # $ (and reset color)
187
188     export PS1
189 }
190
191 set_prompts
192 unset set_prompts