/
.prompt_bash
199 lines (157 loc) · 7.99 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
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
#!/bin/bash
#$HOME/.promptrc
# Author: Casey DeLorme
# Created On: 12-30-2013
# Updated On: 01-25-2014
# Description: Entirely rewritten bash PS1 enhancer targeting git repositories, fully extendable
# License: GPLv3+
### Instructions!
# Toggle with Booleans:
# Turn enhanced prompt off with `ENHANCED_PROMPT`
# Turn git prompt on/off with `GIT_PROMPT_ON`
# Only display branch name with `GIT_PROMPT_BRANCH_ONLY`
# Turn detailed git prompt output on/off with `GIT_PROMPT_DETAILED`
#
# These variables can be defined in your bashrc for semi-permanent functionality.
#
# Extend with Array (called at tail of prompt output):
# ENHANCERS+=('newFunctionName')
#
# Define color functions for enhanced terminal aesthetics
c_bold=`tput bold`
c_red=`tput setaf 1`
c_green=`tput setaf 2`
c_blue=`tput setaf 4`
c_purple=`tput setaf 5`
c_cyan=`tput setaf 6`
c_sgr0=`tput sgr0`
# Define enhancers for easy future-external customization
export ENHANCERS=('gitify')
# Set Defaults to unset properties
[[ -z "$ENHANCED_PROMPT" ]] && ENHANCED_PROMPT=false
[[ -z "$GIT_PROMPT_ON" ]] && GIT_PROMPT_ON=true
[[ -z "$GIT_PROMPT_BRANCH_ONLY" ]] && GIT_PROMPT_BRANCH_ONLY=false
[[ -z "$GIT_PROMPT_DETAILED" ]] && GIT_PROMPT_DETAILED=true
# Add git information if applicable
gitify ()
{
# Check GIT_PROMPT_ON before running git status check
if $GIT_PROMPT_ON
then
# Verify we are in a git repo
git_works=$(git branch 2>&1)
# If no error occurred assume we are inside a repo
if [ $? -eq 0 ]
then
# Acquire the branch name
git_branch=$(echo "${git_works}" | sed -n 's/^\*[ ]*//p')
# if branch name is (no branch) get commit hash instead
if [ "${git_branch}" == "(no branch)" ]
then
git_branch=$(git rev-parse --verify --short HEAD)
fi
if $GIT_PROMPT_BRANCH_ONLY
then
# Append branch only
prompt_string="${prompt_string} [${git_branch}]"
else
# Acquire git status
git_stats=$(git status --porcelain --untracked-files=all)
# Get count of changes from git_stats
git_changes=$(echo "${git_stats}" | sed '/^\s*$/d' | wc -l)
# If no changes print branch only
if [ $git_changes -gt 0 ]
then
# Append branch with red color to prompt string
prompt_string="${prompt_string} [${c_red}${git_branch}${c_sgr0}"
# Do we want to print detailed?
if $GIT_PROMPT_DETAILED
then
# Currently this script only attends to:
# untracked files
# new files
# renamed files
# modified files
# deleted filesd
# it will not count anything it cannot identify
# it will not show branch conflicts (eg both*)
# it **will** show unstaged and staged independently (if both exist)
# Format: u# a# r# m#/# d#/#
# Parse known types for detailed output
untracked_count=$(echo "${git_stats}" | grep -ce "^??")
new_file_count=$(echo "${git_stats}" | grep -ce "^A ")
renamed_count=$(echo "${git_stats}" | grep -ce "^R ")
modified_staged_count=$(echo "${git_stats}" | grep -ce "^M ")
modified_unstaged_count=$(echo "${git_stats}" | grep -ce "^ M")
deleted_staged_count=$(echo "${git_stats}" | grep -ce "^D ")
deleted_unstaged_count=$(echo "${git_stats}" | grep -ce "^ D")
# Appending the contents below
# This process is exceedingly tedius
# if a better approach is found it
# should be implemented
# If our total counts are gt 0, append colon prefix
[[ $(expr $untracked_count + $new_file_count + $renamed_count + $modified_staged_count + $modified_unstaged_count + $deleted_staged_count + $deleted_unstaged_count) -gt 0 ]] && prompt_string="${prompt_string}:"
# If we have untracked items, append
[[ $untracked_count -gt 0 ]] && prompt_string="${prompt_string} ${c_red}u${untracked_count}${c_sgr0}"
# If we have staged added files, append (unstaged == untracked)
[[ $new_file_count -gt 0 ]] && prompt_string="${prompt_string} ${c_green}a${new_file_count}${c_sgr0}"
# If we have staged renamed files, append (unstaged is a combination of untracked & deleted)
[[ $renamed_count -gt 0 ]] && prompt_string="${prompt_string} ${c_green}r${renamed_count}${c_sgr0}"
# If we have modifications, append header
[[ $modified_unstaged_count -gt 0 ]] || [[ $modified_staged_count -gt 0 ]] && prompt_string="${prompt_string} ${c_red}m${c_sgr0}"
# If we have modified unstaged, append red count
[[ $modified_unstaged_count -gt 0 ]] && prompt_string="${prompt_string}${c_red}${modified_unstaged_count}${c_sgr0}" && [[ $modified_staged_count -gt 0 ]] && prompt_string="${prompt_string}/"
# If we have modified staged, append green count
[[ $modified_staged_count -gt 0 ]] && prompt_string="${prompt_string}${c_green}${modified_staged_count}${c_sgr0}"
# If we have deletions, append header
[[ $deleted_unstaged_count -gt 0 ]] || [[ $deleted_staged_count -gt 0 ]] && prompt_string="${prompt_string} ${c_red}d${c_sgr0}"
# If we have deleted unstaged, append red count
[[ $deleted_unstaged_count -gt 0 ]] && prompt_string="${prompt_string}${c_red}${deleted_unstaged_count}${c_sgr0}" && [[ $deleted_staged_count -gt 0 ]] && prompt_string="${prompt_string}/"
# If we have deleted staged, append green count
[[ $deleted_staged_count -gt 0 ]] && prompt_string="${prompt_string}${c_green}${deleted_staged_count}${c_sgr0}"
# Close prompt
prompt_string="${prompt_string}]"
else
# Append count & close branch
prompt_string="${prompt_string}: ${c_red}${git_changes}${c_sgr0}]"
fi
else
prompt_string="${prompt_string} [${c_green}${git_branch}${c_sgr0}]"
fi
fi
fi
fi
}
# Create a colorful detailed prompt
colorify ()
{
# Confirm enhanced prompt
if $ENHANCED_PROMPT
then
# If Linux display milliseconds (OSX does not support %N `data`)
if [ $(uname) = "Darwin" ]
then
prompt_string="${c_blue}${0} ($(date +'%R:%S')) ${c_purple}$(whoami)${c_sgr0}@${c_green}$(hostname) ${c_bold}${c_blue}$(dirs)${c_sgr0}"
else
prompt_string="${c_blue}${0} ($(date +'%R:%S.%3N')) ${c_purple}$(whoami)${c_sgr0}@${c_green}$(hostname) ${c_bold}${c_blue}$(dirs)${c_sgr0}"
fi
# Run through Enhancers
for ENHANCEMENT in ${ENHANCERS[*]}
do
[[ $(type -t $ENHANCEMENT) = 'function' ]] && $ENHANCEMENT
done
# Output custom prompt
echo -ne "\n${prompt_string}"
# Start prompt on new line
echo -ne "\n$ "
else
# Reset prompt to the original if it was ever modified
[[ -n "${PSORIG}" ]] && PS1=${PSORIG}
fi
}
# Grab the original
PSORIG=${PS1}
# By wrapping in single quotes we allow the command execution to be parsed only when requested
PS1='$(colorify)'
# When entering multi-line commands our prompt will be more sensible
PS2='continue-> '