-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_prompt
214 lines (149 loc) · 6.04 KB
/
bash_prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enable_color_support() {
if [[ $COLORTERM == gnome-* && $TERM == xterm ]] \
&& infocmp gnome-256color &> /dev/null; then
export TERM="gnome-256color"
elif infocmp xterm-256color &> /dev/null; then
export TERM="xterm-256color"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Load OS specific color support.
. "colors/$OS/bash_colors"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# [!] Don't break this function into smaller, more specialized ones
# as you will only pollute the global space even more. This function
# cannot be unset because it's called every time the prompt string
# is shown.
get_git_repository_details() {
local branchName=""
local tmp=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if the current directory is in a Git repository.
! git rev-parse &>/dev/null \
&& return
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if in `.git/` directory (some of the following
# checks don't make sense/won't work in the `.git` directory).
[ "$(git rev-parse --is-inside-git-dir)" == "true" ] \
&& return
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for uncommitted changes in the index.
if ! git diff --quiet --ignore-submodules --cached; then
tmp="$tmp+";
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for unstaged changes.
if ! git diff-files --quiet --ignore-submodules --; then
tmp="$tmp!";
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
tmp="$tmp?";
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for stashed files.
if git rev-parse --verify refs/stash &>/dev/null; then
tmp="$tmp$";
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ -n "$tmp" ] \
&& tmp=" [$tmp]"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
branchName="$( printf "%s" "$( git rev-parse --abbrev-ref HEAD 2> /dev/null \
|| git rev-parse --short HEAD 2> /dev/null \
|| printf " (unknown)" )" | tr -d "\n" )"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
printf "%s" "$1$branchName$tmp"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set_prompts() {
local bold=$(tput bold 2> /dev/null)
local reset=$(tput sgr0 2> /dev/null)
local cyan=""
local green=""
local orange=""
local white=""
local yellow=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ "$(tput colors 2> /dev/null || printf "0")" -ge 256 ]; then
# Solarized colors.
# https://github.com/altercation/solarized/tree/master/iterm2-colors-solarized#the-values
cyan=$(tput setaf 37)
green=$(tput setaf 64)
orange=$(tput setaf 166)
white=$(tput setaf 15)
yellow=$(tput setaf 136)
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Prompt Statement variables.
# http://ss64.com/bash/syntax-prompt.html
# ------------------------------------------------------------------
# | PS1 - Default interactive prompt |
# ------------------------------------------------------------------
PS1="\[\033]0;\W\007\]" # Terminal title (set to the
# current working directory)
PS1+="$reset$bold"
PS1+="$orange\u" # Username
PS1+="$white@"
PS1+="$yellow\h" # Host
PS1+="$white: "
PS1+="$green\w" # Working directory
PS1+="\$(get_git_repository_details \"$white on $cyan\")"
PS1+="\n"
PS1+="\[$reset\]\[$white\]$ \[$reset\]"
export PS1
# ------------------------------------------------------------------
# | PS2 - Continuation interactive prompt |
# ------------------------------------------------------------------
PS2="⚡ "
export PS2
# ------------------------------------------------------------------
# | PS4 - Debug prompt |
# ------------------------------------------------------------------
# e.g:
#
# The GNU `date` command has the `%N` interpreted sequence while
# other implementations don't (on macOS `gdate` can be used instead
# of the native `date` if the `coreutils` package has been installed).
#
# local dateCmd=""
#
# if [ "$(date +%N)" != "N" ] || \
# [ ! -x "$(command -v "gdate")" ]; then
# dateCmd="date +%s.%N"
# else
# dateCmd="gdate +%s.%N"
# fi
#
# PS4="+$( tput cr && tput cuf 6 &&
# printf "$yellow %s $green%6s $reset" "$($dateCmd)" "[$LINENO]" )"
#
# PS4 output:
#
# ++ 1357074705.875970000 [123] '[' 1 == 0 ']'
# └──┬─┘└────┬───┘ └───┬───┘ └──┬─┘ └──────┬─────┘
# │ │ │ │ │
# │ │ │ │ └─ command
# │ │ │ └─ line number
# │ │ └─ nanoseconds
# │ └─ seconds since 1970-01-01 00:00:00 UTC
# └─ depth-level of the subshell
PS4="+$( tput cr 2> /dev/null;
tput cuf 6 2> /dev/null;
printf "%s" "$reset" )"
export PS4
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main() {
enable_color_support
set_prompts
}
main
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Cleanup.
unset -f enable_color_support
unset -f main
unset -f set_prompts