-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bash_prompt
105 lines (86 loc) · 3 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
#!/bin/bash
# Check that terminfo exists before changing TERM var to xterm-256color
# Prevents prompt flashing in Mac OS X 10.6 Terminal.app
if [ -e /usr/share/terminfo/x/xterm-256color ]; then
export TERM='xterm-256color'
fi
tput sgr 0 0
# Base styles and color palette
# Solarized colors
# https://github.com/altercation/solarized/tree/master/iterm2-colors-solarized
BOLD=$(tput bold)
RESET=$(tput sgr0)
SOLAR_BLACK=$(tput setaf 0)
SOLAR_YELLOW=$(tput setaf 136)
SOLAR_ORANGE=$(tput setaf 166)
SOLAR_RED=$(tput setaf 124)
SOLAR_MAGENTA=$(tput setaf 125)
SOLAR_VIOLET=$(tput setaf 61)
SOLAR_BLUE=$(tput setaf 33)
SOLAR_CYAN=$(tput setaf 37)
SOLAR_GREEN=$(tput setaf 64)
SOLAR_WHITE=$(tput setaf 15)
# Styles
style_at="\[${RESET}${SOLAR_VIOLET}\]"
style_path="\[${RESET}${SOLAR_GREEN}\]"
style_chars="\[${RESET}${SOLAR_WHITE}\]"
style_branch="${RESET}${SOLAR_CYAN}"
style_state="${RESET}${SOLAR_BLUE}"
# Highlight the user name when logged in as root
if [[ "$USER" == "root" ]]; then
style_user="\[${BOLD}${SOLAR_RED}\]"
else
style_user="\[${RESET}${SOLAR_ORANGE}\]"
fi;
# Highlight the hostname when connected via SSH
if [[ "$SSH_TTY" ]]; then
style_host="\[${BOLD}${SOLAR_RED}\]"
else
style_host="\[${RESET}${SOLAR_YELLOW}\]"
fi;
# Git status information
prompt_git() {
local git_branch='';
local git_state='';
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ]; then
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
git_state+='+';
fi;
# Check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
git_state+='!';
fi;
# Check for untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
git_state+='?';
fi;
# Check for stashed files
if $(git rev-parse --verify refs/stash &>/dev/null); then
git_state+='$';
fi;
fi;
git_branch="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";
[ -n "${git_state}" ] && git_state="[${git_state}]"
echo -e "${SOLAR_WHITE} on ${style_branch}${git_branch} ${style_state}${git_state}"
else
return;
fi;
}
# Set the terminal title to the current working directory
PS1="\[\033]0;\w\007\]"
# Build the prompt
#PS1+="\n" # Newline
PS1+="${style_user}\u" # Username
PS1+="${style_at}@" # @
PS1+="${style_host}\h" # Host
PS1+="${style_chars}:" # :
PS1+="${style_path} \w" # Working directory
PS1+="\$(prompt_git)" # Git details
PS1+="\n" # Newline
PS1+="${style_chars}\$ \[${RESET}\]" # $ (and reset color)