-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.prompt
199 lines (170 loc) · 5.61 KB
/
.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
# jack@host [~/dotfiles] master [+!?$]
# ⑆
# TODO: Add screenshot of prompt
# Terminal prefs: import Solarized theme and disable bright colors for bold text
# References:
# terminal color: http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim
# color output: http://tldp.org/LDP/abs/html/colorizing.html
# bash prompt escape sequences:
# https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
# http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
export TERM='gnome-256color';
elif infocmp xterm-256color >/dev/null 2>&1; then
export TERM='xterm-256color';
fi;
if [ -n "$ZSH_VERSION" ]; then
setopt PROMPT_SUBST
export RPROMPT=
fi;
is_in_git_repository() {
[ $(git rev-parse --is-inside-work-tree &>/dev/null; printf "%s" $?) = 0 ]
}
prompt_git_branch() {
local branchName=""
if is_in_git_repository ; then
# get the short symbolic ref
# if HEAD isn't a symbolic ref, get the short SHA
# otherwise, just give up
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
printf "(unknown)")"
echo $branchName
fi
}
prompt_git_status() {
local s=""
local root=""
if is_in_git_repository ; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" = "false" ]; then
# ensure index is up to date
git update-index --really-refresh -q &>/dev/null
# get root of repository
root=$(git rev-parse --show-toplevel)
# check for uncommitted changes in the index
if ! $(git diff --quiet --ignore-submodules --cached); then
s="$s+";
fi
# check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
s="$s!";
fi
# check for untracked files
if [ -n "$(git ls-files --others --exclude-standard $root)" ]; then
s="$s?";
fi
# check for stashed files
if $(git rev-parse --verify refs/stash &>/dev/null); then
s="$s$";
fi
fi
[ -n "$s" ] && s="[$s]"
echo $s
fi
}
# Define colors used in prompt
# TODO: replace associative array to be compatible with Bash versions < 4.0
declare -A colors
declare -A colors_escaped
# The following colors assume use of a terminal with a solarized color palette
# like gnome-terminal. If not using a solarized terminal palette, use the colors
# from http://git.io/solarized-colors.
if tput setaf 1 &> /dev/null; then
tput sgr0; # reset colors
colors[bold]=$(tput bold);
colors[reset]=$(tput sgr0);
colors[black]=$(tput setaf 0);
colors[gray]=$(tput setaf 12);
colors[white]=$(tput setaf 7);
colors[red]=$(tput setaf 1);
colors[orange]=$(tput setaf 9);
colors[yellow]=$(tput setaf 3);
colors[green]=$(tput setaf 2);
colors[cyan]=$(tput setaf 6);
colors[blue]=$(tput setaf 4);
colors[purple]=$(tput setaf 13);
else
colors[bold]='';
colors[reset]="\e[0m";
colors[black]="\e[30m";
colors[gray]="\e[37m";
colors[white]="\e[37m";
colors[red]="\e[31m";
colors[orange]="\e[91m";
colors[yellow]="\e[33m";
colors[green]="\e[32m";
colors[cyan]="\e[36m";
colors[blue]="\e[34m";
colors[purple]="\e[95m";
fi;
# Set variables for special prompt characters.
if [ -n "$BASH_VERSION" ]; then
echo $username;
username="\u";
hostname="\h";
workdir="\w";
escapeLeft="\[";
escapeRight="\]";
elif [ -n "$ZSH_VERSION" ]; then
echo $username;
username="%n";
hostname="%m";
workdir="%~";
escapeLeft="%{";
escapeRight="%}";
fi;
# Wrap color sequences in escaped brackets so shell doesn't count them towards
# the character length of the prompt. See a detailed explanation here:
# https://unix.stackexchange.com/a/124409.
generate_escaped_colors() {
if [ -n "$ZSH_VERSION" ]; then emulate -L ksh; fi
for i in "${!colors[@]}"; do
colors_escaped[$i]="$escapeLeft${colors[$i]}$escapeRight"
done
}
generate_escaped_colors
# Highlight the username when logged in as root.
if [[ "${USER}" == "root" ]]; then
userStyle="${colors_escaped[red]}";
else
userStyle="${colors_escaped[orange]}";
fi;
# Highlight the hostname when connected via SSH.
if [[ "${SSH_TTY}" ]]; then
hostStyle="${colors_escaped[bold]}${colors_escaped[red]}";
else
hostStyle="${colors_escaped[yellow]}";
fi;
if [ -n "$ZSH_VERSION" ]; then
setopt PROMPT_SUBST;
fi;
# Set the terminal title and prompt.
PS1="${colors_escaped[reset]}"
# PS1+="\[\e]0;\w\007\]"; # terminal title (set to working directory full path)
PS1+="${userStyle}$username${colors_escaped[reset]}"; # username
PS1+="@";
PS1+="${hostStyle}$hostname${colors_escaped[reset]}"; # host
PS1+=" ";
PS1+="${colors_escaped[green]}[$workdir]"; # working directory full path
PS1+=" ";
PS1+="${colors_escaped[cyan]}"
PS1+='$(prompt_git_branch)'; # git branch
PS1+="${colors_escaped[blue]} "
PS1+='$(prompt_git_status)'; # git status
PS1+=$'\n';
PS1+="${colors_escaped[white]}"
PS1+=$'\u2446' # prompt icon (⑆)
PS1+=" ";
PS1+="${colors_escaped[reset]}";
export PS1;
PS2="${colors_escaped[yellow]}→ ${colors_escaped[reset]}";
export PS2;
unset colors_escaped;
unset userStyle;
unset hostStyle;
unset username;
unset hostname;
unset workdir;
unset escapeRight;
unset escapeLeft;