-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkprompt.zsh
332 lines (297 loc) · 7.95 KB
/
mkprompt.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
########################################
# Author: Rui Pinheiro
#
# zsh-mkprompt main file
# Store plugin directory
typeset -g _mkprompt_root="${0:A:h}"
# Function: mkprompt_init
# Initializes the mkprompt mechanism
#
# Usage: mkprompt_init [<parameters>]
# Parameters:
# -d : default delimiter (default=" ")
function mkprompt_init {
# Prompt-related shell options required for mkprompt to work
setopt promptsubst
setopt prompt_cr
# Load dependencies
autoload -Uz add-zsh-hook
zmodload "zsh/datetime"
local f
for f in "$_mkprompt_root/lib/"*.zsh "$_mkprompt_root/modules/"*.zsh ; do
. "$f"
done
# Configuration
typeset -g _mkprompt_default_delim=" "
# Initialize
typeset -g _mkprompt_init=1
# Prepare mkprompt mechanism
typeset -g _mkprompt_rtl=1
typeset -g _mkprompt_var=""
typeset -g _mkprompt_arr=()
# Parameters
local use_default_config=0
while [[ "$#" -gt "0" ]]; do
case "$1" in
"-d"|"--delim"|"--delimiter")
_mkprompt_default_delim="$2"
shift 2
;;
"-def"|"--default")
use_default_config=1
shift 1
;;
*)
mkputils_error "[mkprompt] Invalid parameter '$1', ignored" "$0"
shift 1
;;
esac
done
# Function: mkprompt_start
# Starts a new mkprompt variable if $1 is different from the current variable
#
# Usage: mkprompt_start [<variable>] [<parameters>]
# where <variable> (default="PROMPT") is the destination for the prompt string
# Parameters:
# -ltr : Force left-to-right prompt construction (default)
# -rtl : Force right-to-left prompt construction (default for "RPROMPT" only)
# Defaults:
function mkprompt_start {
# Parse parameters
local new_rtl=""
local new_var=""
while [[ "$#" -gt "0" ]]; do
case "$1" in
"-ltr"|"--left-to-right")
new_rtl=0
shift 1
;;
"-rtl"|"--right-to-left")
new_rtl=1
shift 1
;;
"-"*)
mkputils_error "Invalid parameter '$1', ignored" "$0"
shift 1
;;
*)
new_var="$1"
shift 1
;;
esac
done
# Assume default parameters
if [[ -z "$new_var" && -z "$new_rtl" ]]; then
new_var="PROMPT"
new_rtl=0
elif [[ -z "$new_var" ]]; then
if [[ "$new_rtl" -ne "0" ]]; then
new_var="RPROMPT"
else
new_var="PROMPT"
fi
elif [[ -z "$new_rtl" ]]; then
if [[ "$new_var" == "RPROMPT" ]]; then
new_rtl=1
else
new_rtl=0
fi
fi
# Save previous prompt
if [[ "$new_var" != "$_mkprompt_var" ]]; then
mkprompt_save
fi
# Start new prompt
_mkprompt_arr=()
_mkprompt_rtl="$new_rtl"
_mkprompt_next_delim=0
if [[ "$_mkprompt_rtl" -eq "0" ]]; then
_mkprompt_next_delim_chr=""
_mkprompt_next_delim_var=""
fi
_mkprompt_var="$new_var"
}
# Function: _mkprompt_apply_delim
# Applies the current delimiter
# NOTE: Internal, should not be called by modules or the user
function _mkprompt_apply_delim {
local new_delim_chr="${1:-$_mkprompt_default_delim}" new_delim_var="$2"
local next_delim="$_mkprompt_next_delim"
if [[ "$next_delim" -ne "0" ]]; then
local delim_var
local delim_chr
if [[ "$_mkprompt_rtl" -eq "0" ]]; then
delim_var="$_mkprompt_next_delim_var"
delim_chr="$_mkprompt_next_delim_chr"
else
delim_var="$new_delim_var"
delim_chr="$new_delim_chr"
fi
if [[ ! -z "$delim_chr" ]]; then
local delim="$delim_chr"
[[ ! -z "$delim_var" ]] && delim="\${${delim_var}:+${delim_chr}}"
_mkprompt_arr+=("$delim")
fi
fi
_mkprompt_next_delim=0
if [[ "$_mkprompt_rtl" -eq "0" ]]; then
_mkprompt_next_delim_chr="$new_delim_chr"
_mkprompt_next_delim_var="$new_delim_var"
fi
}
# Function: mkprompt_raw
# Adds raw text to the prompt
# NOTE: This resets delimiters and skips various checks. Use with care
function mkprompt_add_raw {
_mkprompt_arr+=("$@")
mkprompt_set_delim ""
}
# Function: mkprompt_add
# Adds a new section to the prompt
#
# Usage: mkprompt_add [<parameters>] [-- <content> | -e <content-variable>]
# where <parameters> are any of the parameters listed below.
# and <content> is the section content
# and <content-variable> is an environment variable containing the section content that gets expanded
# every time the prompt is rendered.
# "-- <content>" and "-e <content-variable>" are exclusive.
# Parameters:
# -d : next delimiter character (equivalent to using mkprompt_set_delim after this command)
# -s : style escape code (e.g. "$fg[red]")
# NOTE: must be only non-printable characters
# -se : same as -s, but as an environment variable that gets expanded every time the prompt is rendered
function mkprompt_add {
local style=""
local content=""
local new_delim_chr="$_mkprompt_default_delim"
local new_delim_var=""
# Parse parameters
while [[ "$#" -gt "0" ]]; do
case "$1" in
"-d"|"--delim"|"--delimiter")
new_delim_chr="$2"
shift 2
;;
"-e"|"-env"|"--environment")
[[ ! -z "$content" ]] && mkputils_error "Invalid parameter '$1': already have content" "$0" && break
content="\${$2}"
new_delim_var="$2"
shift 2
;;
"-s"|"--style")
style="$2"
shift 2
;;
"-se"|"-senv"|"--style-environment")
style="\${$2}"
shift 1
;;
"-c"|"--content")
[[ ! -z "$content" ]] && mkputils_error "Invalid parameter '$1': already have content" "$0" && break
content="$2"
shift 2
;;
"--")
[[ ! -z "$content" ]] && mkputils_error "Invalid parameter '$1': already have content" "$0" && break
shift 1
content="$@"
break
;;
*)
mkputils_error "Invalid $0 parameter '$1', ignored" "$0"
shift 1
;;
esac
done
# Add desired section
if [[ ! -z "$content" ]]; then
local reset=""
if [[ ! -z "$style" ]]; then
style="%{$style%}"
reset="%{$reset_color%}"
fi
local add="${style}${content}${reset}"
_mkprompt_apply_delim "$new_delim_chr" "$new_delim_var"
_mkprompt_arr+=("$add")
_mkprompt_next_delim=1
fi
}
# Function: mkprompt_force_delim
# Immediately writes the current delimiter
# Usage: mkprompt_force_delim [<delim>]
# where <delim> is the delimiter to use (optional)
function mkprompt_force_delim {
local new_delim="$@"
[[ ! -z "$new_delim" ]] && mkprompt_set_delim "$new_delim"
_mkprompt_apply_delim
}
# Function: mkprompt_set_delim
# Sets the next delimiter character
# Usage: mkprompt_set_delim <delim>
# where <delim> is the delimiter to use
# (can be empty, in which case no delimiter will be output)
function mkprompt_set_delim {
local new_delim="$@"
_mkprompt_next_delim_chr="$new_delim"
[[ -z "$new_delim" ]] && _mkprompt_next_delim=0
}
# Function: mkprompt_save
# Renders and saves the current prompt variable
# Usage: mkprompt_save
function mkprompt_save {
[[ -z "$_mkprompt_var" ]] && return 0
local arr
if [[ "$_mkprompt_rtl" -eq "0" ]]; then
arr=(${_mkprompt_arr})
else
arr=(${(@Oa)_mkprompt_arr})
fi
typeset -g "$_mkprompt_var"="${(j::)arr}"
}
# Function: mkprompt_finish
# Finishes the mkprompt process, undefining all mkprompt variables and methods
#
# Usage: mkprompt_init [<parameters>]
# Parameters:
# -kns : Does not clear the mkprompt namespace
function mkprompt_finish {
# Parse parameters
local default=0
local kns=0
while [[ "$#" -gt "0" ]]; do
case "$1" in
"-def"|"--default")
default=1
shift 1
;;
"-kns"|"--keep-namespace")
kns=1
shift 1
;;
*)
mkputils_error "Invalid parameter '$1', ignored" "$0"
shift 1
;;
esac
done
[[ "$default" -ne "0" || -z "$_mkprompt_var" ]] && _mkprompt_default
mkprompt_save
if [[ "$kns" -eq "0" ]]; then
setopt local_options extended_glob
unset -m "mkprompt_*"
unset -m "_mkprompt_^root"
unfunction -m "mkprompt_^init"
unfunction -m "_mkprompt_*"
fi
}
# Functions: _mkprompt_default
# Sets PROMPT and RPROMPT to the default mkprompt style
# NOTE: Internal, should not be called by a module and/or user
function _mkprompt_default {
. "$_mkprompt_root/mkprompt_default.zsh"
}
# Apply default config if requested
if [[ "$use_default_config" -eq "1" ]]; then
mkprompt_finish --default
fi
}