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