forked from spaceship-prompt/spaceship-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceship.zsh
217 lines (185 loc) · 7.62 KB
/
spaceship.zsh
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
#
# Spaceship ZSH
#
# Author: Denys Dovhan, denysdovhan.com
# License: MIT
# https://github.com/denysdovhan/spaceship-prompt
# Current version of Spaceship
# Useful for issue reporting
export SPACESHIP_VERSION='3.11.1'
# Common-used variable for new line separator
NEWLINE='
'
# Determination of Spaceship working directory
# https://git.io/vdBH7
if [[ -z "$SPACESHIP_ROOT" ]]; then
if [[ "${(%):-%N}" == '(eval)' ]]; then
if [[ "$0" == '-antigen-load' ]] && [[ -r "${PWD}/spaceship.zsh" ]]; then
# Antigen uses eval to load things so it can change the plugin (!!)
# https://github.com/zsh-users/antigen/issues/581
export SPACESHIP_ROOT=$PWD
else
print -P "%F{red}You must set SPACESHIP_ROOT to work from within an (eval).%f"
return 1
fi
else
# Get the path to file this code is executing in; then
# get the absolute path and strip the filename.
# See https://stackoverflow.com/a/28336473/108857
export SPACESHIP_ROOT=${${(%):-%x}:A:h}
fi
fi
# ------------------------------------------------------------------------------
# CONFIGURATION
# The default configuration that can be overridden in .zshrc
# ------------------------------------------------------------------------------
if [ -z "$SPACESHIP_PROMPT_ORDER" ]; then
SPACESHIP_PROMPT_ORDER=(
time # Time stampts section
user # Username section
dir # Current directory section
host # Hostname section
git # Git section (git_branch + git_status)
hg # Mercurial section (hg_branch + hg_status)
package # Package version
node # Node.js section
ruby # Ruby section
elm # Elm section
elixir # Elixir section
xcode # Xcode section
swift # Swift section
golang # Go section
php # PHP section
rust # Rust section
haskell # Haskell Stack section
julia # Julia section
docker # Docker section
aws # Amazon Web Services section
venv # virtualenv section
conda # conda virtualenv section
pyenv # Pyenv section
dotnet # .NET section
ember # Ember.js section
kubecontext # Kubectl context section
terraform # Terraform workspace section
exec_time # Execution time
line_sep # Line break
battery # Battery level and status
vi_mode # Vi-mode indicator
jobs # Background jobs indicator
exit_code # Exit code section
char # Prompt character
)
fi
if [ -z "$SPACESHIP_RPROMPT_ORDER" ]; then
SPACESHIP_RPROMPT_ORDER=(
# empty by default
)
fi
# PROMPT
SPACESHIP_PROMPT_ADD_NEWLINE="${SPACESHIP_PROMPT_ADD_NEWLINE=true}"
SPACESHIP_PROMPT_SEPARATE_LINE="${SPACESHIP_PROMPT_SEPARATE_LINE=true}"
SPACESHIP_PROMPT_FIRST_PREFIX_SHOW="${SPACESHIP_PROMPT_FIRST_PREFIX_SHOW=false}"
SPACESHIP_PROMPT_PREFIXES_SHOW="${SPACESHIP_PROMPT_PREFIXES_SHOW=true}"
SPACESHIP_PROMPT_SUFFIXES_SHOW="${SPACESHIP_PROMPT_SUFFIXES_SHOW=true}"
SPACESHIP_PROMPT_DEFAULT_PREFIX="${SPACESHIP_PROMPT_DEFAULT_PREFIX="via "}"
SPACESHIP_PROMPT_DEFAULT_SUFFIX="${SPACESHIP_PROMPT_DEFAULT_SUFFIX=" "}"
# ------------------------------------------------------------------------------
# LIBS
# Spaceship utils/hooks/etc
# ------------------------------------------------------------------------------
# Load utils
source "$SPACESHIP_ROOT/lib/utils.zsh"
# load hooks
source "$SPACESHIP_ROOT/lib/hooks.zsh"
# load section utils
source "$SPACESHIP_ROOT/lib/section.zsh"
# ------------------------------------------------------------------------------
# SECTIONS
# Sourcing sections the prompt consists of
# ------------------------------------------------------------------------------
for section in $(spaceship::union $SPACESHIP_PROMPT_ORDER $SPACESHIP_RPROMPT_ORDER); do
if [[ -f "$SPACESHIP_ROOT/sections/$section.zsh" ]]; then
source "$SPACESHIP_ROOT/sections/$section.zsh"
elif spaceship::defined "spaceship_$section"; then
# Custom section is declared, nothing else to do
continue
else
echo "Section '$section' have not been loaded."
fi
done
# ------------------------------------------------------------------------------
# BACKWARD COMPATIBILITY WARNINGS
# Show deprecation messages for options that are set, but not supported
# ------------------------------------------------------------------------------
spaceship::deprecated SPACESHIP_PROMPT_SYMBOL "Use %BSPACESHIP_CHAR_SYMBOL%b instead."
spaceship::deprecated SPACESHIP_BATTERY_ALWAYS_SHOW "Use %BSPACESHIP_BATTERY_SHOW='always'%b instead."
spaceship::deprecated SPACESHIP_BATTERY_CHARGING_SYMBOL "Use %BSPACESHIP_BATTERY_SYMBOL_CHARGING%b instead."
spaceship::deprecated SPACESHIP_BATTERY_DISCHARGING_SYMBOL "Use %BSPACESHIP_BATTERY_SYMBOL_DISCHARGING%b instead."
spaceship::deprecated SPACESHIP_BATTERY_FULL_SYMBOL "Use %BSPACESHIP_BATTERY_SYMBOL_FULL%b instead."
# ------------------------------------------------------------------------------
# PROMPTS
# An entry point of prompt
# ------------------------------------------------------------------------------
# PROMPT
# Primary (left) prompt
spaceship_prompt() {
# Retrieve exit code of last command to use in exit_code
# Must be captured before any other command in prompt is executed
# Must be the very first line in all entry prompt functions, or the value
# will be overridden by a different command execution - do not move this line!
RETVAL=$?
# Should it add a new line before the prompt?
[[ $SPACESHIP_PROMPT_ADD_NEWLINE == true ]] && echo -n "$NEWLINE"
spaceship::compose_prompt $SPACESHIP_PROMPT_ORDER
}
# $RPROMPT
# Optional (right) prompt
spaceship_rprompt() {
# Retrieve exit code of last command to use in exit_code
RETVAL=$?
spaceship::compose_prompt $SPACESHIP_RPROMPT_ORDER
}
# PS2
# Continuation interactive prompt
spaceship_ps2() {
# Retrieve exit code of last command to use in exit_code
RETVAL=$?
local char="${SPACESHIP_CHAR_SYMBOL_SECONDARY="$SPACESHIP_CHAR_SYMBOL"}"
spaceship::section "$SPACESHIP_CHAR_COLOR_SECONDARY" "$char"
}
# ------------------------------------------------------------------------------
# SETUP
# Setup requirements for prompt
# ------------------------------------------------------------------------------
# Runs once when user opens a terminal
# All preparation before drawing prompt should be done here
prompt_spaceship_setup() {
autoload -Uz vcs_info
autoload -Uz add-zsh-hook
# This variable is a magic variable used when loading themes with zsh's prompt
# function. It will ensure the proper prompt options are set.
prompt_opts=(cr percent sp subst)
# Borrowed from promptinit, sets the prompt options in case the prompt was not
# initialized via promptinit.
setopt noprompt{bang,cr,percent,subst} "prompt${^prompt_opts[@]}"
# Add exec_time hooks
add-zsh-hook preexec spaceship_exec_time_preexec_hook
add-zsh-hook precmd spaceship_exec_time_precmd_hook
# Disable python virtualenv environment prompt prefix
VIRTUAL_ENV_DISABLE_PROMPT=true
# Configure vcs_info helper for potential use in the future
add-zsh-hook precmd spaceship_exec_vcs_info_precmd_hook
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git*' formats '%b'
# Expose Spaceship to environment variables
PROMPT='$(spaceship_prompt)'
PS2='$(spaceship_ps2)'
RPS1='$(spaceship_rprompt)'
}
# ------------------------------------------------------------------------------
# ENTRY POINT
# An entry point of prompt
# ------------------------------------------------------------------------------
# Pass all arguments to the spaceship_setup function
prompt_spaceship_setup "$@"