-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.setupPrompt
233 lines (204 loc) · 9.49 KB
/
.setupPrompt
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
##############################################################
#-------------------------------------------------------------
# Setup bash prompt
#-------------------------------------------------------------
##############################################################
# Try to load virtualenv wrapper for prompt
### From my observations, this seems to add about a second of overhead time.
### Additionally, there might be reason to use the wrapper corresponding to a particular Python installation.
### With those points in mind, it might be better to have this be part of envrc files instead.
#-------------------------------------------------------------
# if [ -v PY38 ]; then
# source "$PY38/Scripts/virtualenvwrapper.sh"
# elif [ -v PY37 ]; then
# source "$PY37/Scripts/virtualenvwrapper.sh"
# elif [ -v PY36 ]; then
# source "$PY36/Scripts/virtualenvwrapper.sh"
# fi
isInGitBranch(){
git symbolic-ref --short HEAD >/dev/null 2>&1
}
# If passed an argument, prepend the output with it, followed by 3 spaces
parse_git_branch(){
#branch="$(git branch 2>/dev/null)"
#if [ -n "$branch" ]; then
# # The final sed replacement shouldn't lead to any strangeness, but if it does, I can just do something like ${1:-SOMETHING} instead of just $1; I'll have to wait and see if I missed any loopholes
# echo "${branch}" | sed -e '/^[^*]/d' -e 's/\* \(.*\)/(\1)/' -e "s/^/$1 /"
#fi
# Functionally equivalent to:
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
[ "$branch" != '' ] && printf "$1 (%s)\n" "$branch"
unset branch
}
# TODO BEFORE THIS IS USEFUL: Think of a good way to remove the prompt prefix from virtualenv only AFTER determining name
function __getVenvName(){
# This function is intended for eventually replacing the default python venv prompt
[ ! -v VIRTUAL_ENV ] && return
# If displaying the default '(venv)' string at the start of the line to indicate an active virtualenv, but the normal prompt change is disabled, just return the unicode snake emoji 🐍
if [ ! -v VIRTUAL_ENV_DISABLE_PROMPT ]; then
# If displaying the default '(venv)' string at the start of the line to indicate an active virtualenv, remove it from the prompt and return the name of the active virtualenv
local norm="${PS1#(*)}" #"${PS1/\(venv\)/}"
local namelen=$(( "${#PS1}-${#norm}" ))
local name="${PS1:1:$((namelen-2))}"
PS1="$norm"
echo " ($name)"
else
echo " 🐍"
fi
}
__smartishTitle(){
# If this is an xterm, set the title to pwd, replacing the value of $HOME with the ~ character
#case "$TERM" in
# xterm*|rxvt*)
# printf "\033]0;${PWD/$HOME/\~}\007";;
#esac
printf "\033]0;${PWD/$HOME/\~}\007";
}
function prunePromptFuncs(){
# Undefine the prompt function(s) that wont be utilized based on the selection. Also undefines itself afterwards.
declare -a opts=(subtlePrompt colorfulPrompt)
opts="${opts/$(caller 0|cut -d ' ' -f 2)/}"
unset -f "$opts" prunePromptFuncs
}
function subtlePrompt(){
# 'Subtle' PS1 details
#-------------------------------------------------------------
### This prompt is more subtle (Read: less attention grabbing)
### It also includes a timestamp (though its the time the line itself was originally displayed, rather than continually running an update command in the background until the user 'submits' the command)
### This prompt only separates the command from the static information with a "$ "
### Completely ignorant of VCS repositories
prunePromptFuncs 2>/dev/null
local RED='\[\033[0;31m\]'
local LRED='\[\033[1;31m\]'
local GREEN='\[\033[0;32m\]'
local LGREEN='\[\033[1;32m\]'
local BOLD='\[\033[1m\]'
local UNDERLINE='\[\033[4m\]'
local RST='\[\033[0m\]' # Reset formatting
# Display the hostname underlined and in bold when connected via SSH.
local hostStyle="${SSH_TTY:+${BOLD}${UNDERLINE}}"
if [ $EUID -eq 0 ]; then
local usr="${RED}\u"
local sep="${LRED}@"
local host="${RED}${hostStyle}\H"
else
local usr="${GREEN}\u"
local sep="${LGREEN}@"
local host="${GREEN}${hostStyle}\H"
fi
local clock="\[\e[1;30;40m\]⟨\T ⟩${RST}"
PS1='$(getexitcode)'"${clock}[${usr}${sep}${host}${RST}] \w\\$ "
}
function colorfulPrompt(){
# Colorful PS1 details
#-------------------------------------------------------------
### This prompt is rather colorful and eye catching, though not blindingly so.
### It lacks a timestamp or any kind.
### This prompt puts the command on a second line, leaving it clearly separated from the rest of the prompt
### If the current user is root, the second line starts with an uppercase omega (Ω), and with a lowercase lambda (λ) otherwise
### If the current directory is part of a git repository, the current branch name will be appended to the first line; still does not include status information
prunePromptFuncs 2>/dev/null
local RED='\[\033[0;31m\]'
local GREEN='\[\033[0;32m\]'
local YELLOW='\[\033[0;33m\]'
local LPURPLE='\[\033[1;35m\]'
local CYAN='\[\033[0;36m\]'
local RST='\[\033[0m\]' # Reset formatting
local BOLD='\[\033[1m\]'
local UNDERLINE='\[\033[4m\]'
# 24-bit true color support is assumed. If your terminal emulator doesn't support it, get a better terminal emulator!
local branchColor='\[\033[38;2;29;194;164m\]' # Currently hardcoded to an almost-teal blue-green color (29;194;164)
if [ $EUID -eq 0 ]; then
local usr="${RED}\u"
local delimColor="${RED}"
local cmdMarker="${RED}Ω${RST} "
else
local usr="${CYAN}\u"
local delimColor="${LPURPLE}"
local cmdMarker="${LPURPLE}λ${RST} "
fi
# Display the current branch when in a directory that is part of a git repository
local gitBranch="\$(parse_git_branch '${branchColor}')${RST}"
local delim="${delimColor}\$>"
# Display the username and hostname underlined and in bold when connected via SSH.
local host="${GREEN}@${SSH_TTY:+${BOLD}${UNDERLINE}}\H${RST}"
# shellcheck disable=SC2016
local exval="${RST}\[\033[7;31m\]"'$(getexitcode)'"${RST}"
local here="${YELLOW}\w"'$(__smartishTitle)'
# local tmpVenvSeg='$(__getVenvName)'
# Scroll markers for the first and last line of the multi-line prompt; may only work with mintty...
# Semantic prompt zone sequences that work in other terminals just seem much more, err, messy and cumbersome. Future work, perhaps?
local firstLineMarker='\[\033[?7711l\]'
local lastLineMarker='\[\033[?7711h\]'
PS1="${firstLineMarker}${exval}${usr}${host} ${here}${RST}${tmpVenvSeg} ${delim}${gitBranch}"$'\n'"${delimColor}${RST}${cmdMarker}${lastLineMarker}" # FIXME: delimColor looks unused here
}
# Main prompt setup
# Note that an update to a relevant shell variable can be forced into affect by calling setupPrompt after this script has been sourced once
#-------------------------------------------------------------
function setupPrompt(){
### The value of this variable is used as the prompt for the select command.
### If this variable is not set, the select command prompts with `#? '
# export PS3=''
### When running scripts in debug mode (-x), give the name of the file,
### followed by the current line number within it
### NOTE:
### The first character of the expanded value is replicated multiple times,
### as necessary, to indicate multiple levels of indirection. The default is `+ '
export PS4='$0 line: $LINENO '
# TODO: validate intended behavior of PS4
### The value of this parameter is expanded like PS1 and displayed by interactive
### shells after reading a command and before the command is executed.
### ex: PS0="$(printf '\r'; fullWidthLineUnicode ─; echo)"
# export PS0=''
# Common PS1 details
#-------------------------------------------------------------
### Both options for PS1 display:
### The current user's name
### The machine hostname (underlined and in bold if logged in over SSH)
### The return status of the last command, if it was non-zero
### The current directory
### Whether the current user is root, indicated by the user's name (and sometimes hostname or command separator) turning red.
###
### Neither option for PS1 inherently includes anything indicating virtual environments, vcs status, or job information. This may change at some point.
# Behavior of promptMode variable
#-------------------------------------------------------------
# promptMode unset : Use colorful 2-line PS1
# promptMode == 'colorful' : Use colorful 2-line PS1
# promptMode == 'subtle' : Use 'subtle' PS1
# promptMode == 'default' : Use default value for PS1 specified in the bash man page
# promptMode == anything else : Don't modify PS1; keep any existing value
case "${promptMode:-colorful}" in
subtle)
subtlePrompt
;;
colorful)
colorfulPrompt
;;
default)
PS1="\s-\v\$ "
;;
*)
# echo "Unknown prompt mode '${promptMode}' specified"
;;
esac
# Apparently you can prepend PS1 with something to set the terminal window title in xterm-based terminal emulators.
# see: https://stackoverflow.com/questions/1687642/set-screen-title-from-shellscript
#PS1="${PS1}"
export PS1
}
setupPrompt
### If this is an xterm, set the title to pwd, replacing the value of $HOME with the ~ character
# case "$TERM" in
# xterm*|rxvt*)
# PROMPT_COMMAND='printf "\033]0;${PWD/$HOME/\~}\007"'
# ;;
# *)
# ;;
# esac
## Whenever displaying the prompt, write the previous line to disk
## Always occurs BEFORE $PS1 is applied
## Normally there is no reason to export this variable, as it is only (normally) used by interactive bash sessions,
## and doing so can apparently cause issues with some programs, like Gentoo's `emerge`
## PROMPT_COMMAND="history -a"
# Set the number of trailing directory components to show when expanding \w or \W in the prompt string
# export PROMPT_DIRTRIM=3