-
Notifications
You must be signed in to change notification settings - Fork 7
/
prompt.bash
142 lines (122 loc) · 4.86 KB
/
prompt.bash
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
#! /bin/bash
# This value is used to hold the return value of the prompt sub-functions.
__prompt_retval=""
# Create some color constants to make the prompt a little more readable.
__prompt_color_prefix="\[\e["
__prompt_256_prefix="38;5;"
__prompt_color_suffix="m\]"
__prompt_no_color="\[\e[0m\]"
# Set the path color. Defaults to green. It can be overwritten by
# `PROMPT_PWD_COLOR`.
__prompt_pwd_color="${__prompt_color_prefix}${__prompt_256_prefix}43${__prompt_color_suffix}"
if ! [[ -z $PROMPT_PWD_COLOR ]]; then
__prompt_pwd_color="${__prompt_color_prefix}${PROMPT_PWD_COLOR}${__prompt_color_suffix}"
fi
# Set the git color. Defaults to purple. It can be overwritten by
# `PROMPT_GIT_COLOR`.
__prompt_git_color="${__prompt_color_prefix}${__prompt_256_prefix}105${__prompt_color_suffix}"
if ! [[ -z $PROMPT_GIT_COLOR ]]; then
__prompt_git_color="${__prompt_color_prefix}${PROMPT_GIT_COLOR}${__prompt_color_suffix}"
fi
# Set the user-host color. Defaults to blue. It can be overwritten by
# `PROMPT_USERHOST_COLOR`.
__prompt_userhost_color="${__prompt_color_prefix}${__prompt_256_prefix}39${__prompt_color_suffix}"
if ! [[ -z $PROMPT_USERHOST_COLOR ]]; then
__prompt_userhost_color="${__prompt_color_prefix}${PROMPT_USERHOST_COLOR}${__prompt_color_suffix}"
fi
# Set the error color. Defaults to red. It can be overwritten by
# `PROMPT_ERROR_COLOR`.
__prompt_error_color="${__prompt_color_prefix}${__prompt_256_prefix}204${__prompt_color_suffix}"
if ! [[ -z $PROMPT_ERROR_COLOR ]]; then
__prompt_error_color="${__prompt_color_prefix}${PROMPT_ERROR_COLOR}${__prompt_color_suffix}"
fi
# Set the dollar color. Defaults to white. It can be overwritten by
# `PROMPT_DOLLAR_COLOR`.
__prompt_dollar_color="${__prompt_color_prefix}${__prompt_256_prefix}255${__prompt_color_suffix}"
if ! [[ -z $PROMPT_DOLLAR_COLOR ]]; then
__prompt_dollar_color="${__prompt_color_prefix}${PROMPT_DOLLAR_COLOR}${__prompt_color_suffix}"
fi
# Gets the current working directory path, but shortens the directories in the
# middle of long paths to just their respective first letters.
function __prompt_get_short_pwd {
# Break down the local variables.
local dir=`dirs +0`
local dir_parts=(${dir//// })
local number_of_parts=${#dir_parts[@]}
# If there are less than 6 path parts, then do no shortening.
if [[ "$number_of_parts" -gt "5" ]]; then
# Leave the last 2 part parts alone.
local last_index="$(( $number_of_parts - 3 ))"
local short_pwd=""
# Check for a leading slash.
if [[ "${dir:0:1}" == "/" ]]; then
# If there is a leading slash, add one to `short_pwd`.
short_pwd+='/'
fi
for i in "${!dir_parts[@]}"; do
# Append a '/' before we do anything (provided this isn't the first part).
if [[ "$i" -gt "0" ]]; then
short_pwd+='/'
fi
# Don't shorten the first/last few arguments - leave them as-is.
if [[ "$i" -lt "2" || "$i" -gt "$last_index" ]]; then
short_pwd+="${dir_parts[i]}"
else
# This means that this path part is in the middle of the path. Our logic
# dictates that we shorten parts in the middle like this.
short_pwd+="${dir_parts[i]:0:1}"
fi
done
# Return the resulting short pwd.
__prompt_retval="$short_pwd"
else
# We didn't change anything, so return the original pwd.
__prompt_retval="$dir"
fi
}
# Returns the user@host. The host is overriden by `PROMPT_HOST_NAME`.
function __prompt_get_host() {
# Check to see if we already have the host name cached.
if [[ -z $PROMPT_HOST_NAME ]]; then
# We do not have the friendly host name cached, so use use '\h'.
__prompt_retval='\u@\h'
else
# We have the host name cached! Dope! Let's use it.
__prompt_retval="\u@$PROMPT_HOST_NAME"
fi
}
# Returns the git branch (if there is one), or returns empty.
function __prompt_get_git_stuff() {
local branch
if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
if [[ "$branch" == "HEAD" ]]; then
branch='detached*'
fi
# Return the resulting git branch/ref.
__prompt_retval=":$branch"
else
# Return empty if there is no git stuff.
__prompt_retval=''
fi
}
# This function creates prompt.
function __prompt_command() {
# Make the dollar red if the last command exited with error.
local last_command_retval="$?"
local dollar_color="$__prompt_dollar_color"
if [ $last_command_retval -ne 0 ]; then
dollar_color="$__prompt_error_color"
fi
# Calculate prompt parts.
__prompt_get_short_pwd
local short_pwd="${__prompt_pwd_color}${__prompt_retval}"
__prompt_get_host
local host="${__prompt_userhost_color}${__prompt_retval}"
__prompt_get_git_stuff
local git_stuff="${__prompt_git_color}${__prompt_retval}"
local dollar="${dollar_color}$"
# Set the PS1 to the new prompt.
PS1="${short_pwd}${git_stuff} ${host} ${dollar}${__prompt_no_color} "
}
# Tell bash about the function above.
export PROMPT_COMMAND=__prompt_command