-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_prompt
136 lines (121 loc) · 4.06 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
#!/bin/sh
# use colours in terminal
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 tput setaf 1 &> /dev/null; then
tput sgr0
export bold=$(tput bold)
export reset=$(tput sgr0)
export black=$(tput setaf 0)
export blue=$(tput setaf 33)
export cyan=$(tput setaf 37)
export green=$(tput setaf 64)
export orange=$(tput setaf 166)
export pink=$(tput setaf 9)
export purple=$(tput setaf 125)
export red=$(tput setaf 124)
export violet=$(tput setaf 61)
export white=$(tput setaf 15)
export yellow=$(tput setaf 136)
else
export bold=""
export reset="\e[0m"
export black="\e[1;30m"
export blue="\e[1;34m"
export cyan="\e[1;36m"
export green="\e[1;32m"
export orange="\e[1;33m"
export pink="\e[1;31m"
export purple="\e[1;35m"
export red="\e[1;31m"
export violet="\e[1;35m"
export white="\e[1;37m"
export yellow="\e[1;33m"
fi
# highlight the user and host name when logged in as root
if [[ $(id -un) == "root" ]] || [[ "$USER" == "root" ]]; then
userStyle="${pink}"
hostStyle="${pink}"
else
userStyle="${orange}"
hostStyle="${yellow}"
fi
function __prompt_git_branch {
if [[ ! "$BASH_PROMPT_SHOW_GIT" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
return
fi
local branch=""
# check if the current directory is in a git repository
if [ $(git rev-parse --is-inside-work-tree &> /dev/null; echo "${?}") -eq 0 ]; then
branch="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')"
echo "${branch}"
else
return
fi
}
function __prompt_git_status {
if [[ ! "$BASH_PROMPT_SHOW_GIT" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
return
fi
local status=""
# check if the current directory is in a git repository
if [ $(git rev-parse --is-inside-work-tree &> /dev/null; echo "${?}") -eq 0 ]; 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 the index is up-to-date
git update-index --really-refresh -q &> /dev/null
# check for uncommitted changes in the index
if ! $(git diff --quiet --ignore-submodules --cached); then
status+='+'
fi
# check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
status+='!'
fi
# check for untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
status+='?'
fi
# check for stashed files
if $(git rev-parse --verify refs/stash &> /dev/null); then
status+='$'
fi
fi
[ -n "${status}" ] && status="[${status}]"
echo "${status}"
else
return
fi
}
function prompt_command {
# code
local code=$?
if [ $code -ne 0 ]; then
code=" \[${reset}${pink}\](${code})"
else
code=""
fi
# git
local git=""
local branch=$(__prompt_git_branch)
[ -n "${branch}" ] && git=" \[${reset}${white}\]on \[${bold}${violet}\]${branch}"
local status=$(__prompt_git_status)
[ -n "${status}" ] && git+=" \[${reset}${blue}\]${status}"
PS1="\[\033]0;\w\007\]"
PS1+="\[${bold}${userStyle}\]\u " # {user}
PS1+="\[${reset}${white}\]at " # at
PS1+="\[${bold}${hostStyle}\]\h " # {host}
PS1+="\[${reset}${white}\]in " # in
PS1+="\[${bold}${green}\]\w" # {dir}
PS1+="${git}" # on {branch} {info}
PS1+="${code}" # {code}
PS1+="\[${reset}${white}\] \$ \[${reset}\]" # $
export PS1
PS2="\[${white}\]→ \[${reset}\]"
export PS2
}