-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
omp.zsh
226 lines (183 loc) · 5.49 KB
/
omp.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
export POSH_THEME=::CONFIG::
export POSH_SHELL='zsh'
export POSH_SHELL_VERSION=$ZSH_VERSION
export POSH_SESSION_ID=::SESSION_ID::
export POWERLINE_COMMAND='oh-my-posh'
export CONDA_PROMPT_MODIFIER=false
export ZLE_RPROMPT_INDENT=0
export OSTYPE=$OSTYPE
_omp_executable=::OMP::
_omp_tooltip_command=''
# switches to enable/disable features
_omp_cursor_positioning=0
_omp_ftcs_marks=0
# set secondary prompt
_omp_secondary_prompt=$($_omp_executable print secondary --shell=zsh)
function _omp_set_cursor_position() {
# not supported in Midnight Commander
# see https://github.com/JanDeDobbeleer/oh-my-posh/issues/3415
if [[ $_omp_cursor_positioning == 0 ]] || [[ -v MC_SID ]]; then
return
fi
local oldstty=$(stty -g)
stty raw -echo min 0
local pos
echo -en '\033[6n' >/dev/tty
read -r -d R pos
pos=${pos:2} # strip off the esc-[
local parts=(${(s:;:)pos})
stty $oldstty
export POSH_CURSOR_LINE=${parts[1]}
export POSH_CURSOR_COLUMN=${parts[2]}
}
# template function for context loading
function set_poshcontext() {
return
}
function _omp_preexec() {
if [[ $_omp_ftcs_marks == 1 ]]; then
printf '\033]133;C\007'
fi
_omp_start_time=$($_omp_executable get millis)
}
function _omp_precmd() {
_omp_status=$?
_omp_pipestatus=(${pipestatus[@]})
_omp_stack_count=${#dirstack[@]}
_omp_execution_time=-1
_omp_no_status=true
_omp_tooltip_command=''
if [ $_omp_start_time ]; then
local omp_now=$($_omp_executable get millis)
_omp_execution_time=$(($omp_now - $_omp_start_time))
_omp_no_status=false
fi
if [[ ${_omp_pipestatus[-1]} != "$_omp_status" ]]; then
_omp_pipestatus=("$_omp_status")
fi
set_poshcontext
_omp_set_cursor_position
# We do this to avoid unexpected expansions in a prompt string.
unsetopt PROMPT_SUBST
unsetopt PROMPT_BANG
# Ensure that escape sequences work in a prompt string.
setopt PROMPT_PERCENT
PS2=$_omp_secondary_prompt
eval "$(_omp_get_prompt primary --eval)"
unset _omp_start_time
}
# add hook functions
autoload -Uz add-zsh-hook
add-zsh-hook precmd _omp_precmd
add-zsh-hook preexec _omp_preexec
# Prevent incorrect behaviors when the initialization is executed twice in current session.
function _omp_cleanup() {
local omp_widgets=(
self-insert
zle-line-init
)
local widget
for widget in "${omp_widgets[@]}"; do
if [[ ${widgets[._omp_original::$widget]} ]]; then
# Restore the original widget.
zle -A ._omp_original::$widget $widget
elif [[ ${widgets[$widget]} = user:_omp_* ]]; then
# Delete the OMP-defined widget.
zle -D $widget
fi
done
}
_omp_cleanup
unset -f _omp_cleanup
function _omp_get_prompt() {
local type=$1
local args=("${@[2,-1]}")
$_omp_executable print $type \
--save-cache \
--shell=zsh \
--shell-version=$ZSH_VERSION \
--status=$_omp_status \
--pipestatus="${_omp_pipestatus[*]}" \
--no-status=$_omp_no_status \
--execution-time=$_omp_execution_time \
--stack-count=$_omp_stack_count \
${args[@]}
}
function _omp_render_tooltip() {
if [[ $KEYS != ' ' ]]; then
return
fi
# Get the first word of command line as tip.
local tooltip_command=${${(MS)BUFFER##[[:graph:]]*}%%[[:space:]]*}
# Ignore an empty/repeated tooltip command.
if [[ -z $tooltip_command ]] || [[ $tooltip_command = "$_omp_tooltip_command" ]]; then
return
fi
_omp_tooltip_command="$tooltip_command"
local tooltip=$(_omp_get_prompt tooltip --command="$tooltip_command")
if [[ -z $tooltip ]]; then
return
fi
RPROMPT=$tooltip
zle .reset-prompt
}
function _omp_zle-line-init() {
[[ $CONTEXT == start ]] || return 0
# Start regular line editor.
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[1]
zle .recursive-edit
local -i ret=$?
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[2]
eval "$(_omp_get_prompt transient --eval)"
zle .reset-prompt
if ((ret)); then
# TODO (fix): this is not equal to sending a SIGINT, since the status code ($?) is set to 1 instead of 130.
zle .send-break
fi
# Exit the shell if we receive EOT.
if [[ $KEYS == $'\4' ]]; then
exit
fi
zle .accept-line
return $ret
}
# Helper function for calling a widget before the specified OMP function.
function _omp_call_widget() {
# The name of the OMP function.
local omp_func=$1
# The remainder are the widget to call and potential arguments.
shift
zle "$@" && shift 2 && $omp_func "$@"
}
# Create a widget with the specified OMP function.
# An existing widget will be preserved and decorated with the function.
function _omp_create_widget() {
# The name of the widget to create/decorate.
local widget=$1
# The name of the OMP function.
local omp_func=$2
case ${widgets[$widget]:-''} in
# Already decorated: do nothing.
user:_omp_decorated_*) ;;
# Non-existent: just create it.
'')
zle -N $widget $omp_func
;;
# User-defined or builtin: backup and decorate it.
*)
# Back up the original widget. The leading dot in widget name is to work around bugs when used with zsh-syntax-highlighting in Zsh v5.8 or lower.
zle -A $widget ._omp_original::$widget
eval "_omp_decorated_${(q)widget}() { _omp_call_widget ${(q)omp_func} ._omp_original::${(q)widget} -- \"\$@\" }"
zle -N $widget _omp_decorated_$widget
;;
esac
}
function enable_poshtooltips() {
local widget=${$(bindkey ' '):2}
if [[ -z $widget ]]; then
widget=self-insert
fi
_omp_create_widget $widget _omp_render_tooltip
}
# legacy functions
function enable_poshtransientprompt() {}