Skip to content

Commit 7ca3cae

Browse files
rpigottbluca
authored andcommitted
zsh: reintroduce pattern argument to uncached verbs
The systemctl completion previously made use of PREFIX as a pattern argument to list-unit-files and list-units. This had the problem of erroneously filtering the results that were stored in the cache, and erroneously filtering results that might have been requested according to the users configuration (e.g. _correct completer, certain matcher-lists or tag-orders, etc.). Unfortunately, the runtime of list-unit-files increases when no pattern argument is provided, and systemctl show, used to filter those units, can become unacceptably slow when provided with too many units to describe. Let's re-introduce the pattern argument to list-unit-files and list-units where necessary in order to alleviate these bottlenecks without poisining the cache. A 'use-pattern' style is introduced that may be used to disable this behavior if it is undesired. We can still expect that certain completions, like `systemctl start <TAB>` will be slow, like before. To fix this we will need systemd to learn a more efficient way of filtering the units than parsing systemctl show. (cherry picked from commit 2cbda74) (cherry picked from commit dfc0445)
1 parent f664081 commit 7ca3cae

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

shell-completion/zsh/_systemctl.in

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,37 @@ __systemctl()
202202
_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ }
203203

204204
(( $+functions[_systemctl_active_units] )) ||
205-
_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )}
205+
_systemctl_active_units() {
206+
local pattern
207+
if zstyle -T ":completion:$curcontext" use-pattern; then
208+
pattern="$PREFIX*$SUFFIX"
209+
fi
210+
_sys_active_units=( ${${(f)"$(__systemctl list-units $pattern)"}%% *} )
211+
}
206212

207213
(( $+functions[_systemctl_startable_units] )) ||
208214
_systemctl_startable_units(){
215+
local pattern
216+
if zstyle -T ":completion:$curcontext" use-pattern; then
217+
pattern="$PREFIX*$SUFFIX"
218+
fi
209219
_sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
210220
_filter_units_by_property CanStart yes ${${${(f)"$(
211-
__systemctl list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient
212-
__systemctl list-units --state inactive,failed
221+
__systemctl list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient $pattern
222+
__systemctl list-units --state inactive,failed $pattern
213223
)"}:#*@.*}%%[[:space:]]*}
214224
)) )
215225
}
216226

217227
(( $+functions[_systemctl_restartable_units] )) ||
218228
_systemctl_restartable_units(){
229+
local pattern
230+
if zstyle -T ":completion:$curcontext" use-pattern; then
231+
pattern="$PREFIX*$SUFFIX"
232+
fi
219233
_sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
220-
__systemctl list-unit-files --state enabled,disabled,static
221-
__systemctl list-units
234+
__systemctl list-unit-files --state enabled,disabled,static $pattern
235+
__systemctl list-units $pattern
222236
)"}:#*@.*}%%[[:space:]]*} ) )
223237
}
224238

@@ -228,8 +242,12 @@ __systemctl()
228242
(( $+functions[_systemctl_unit_state] )) ||
229243
_systemctl_unit_state() {
230244
setopt localoptions extendedglob
245+
local pattern
246+
if zstyle -T ":completion:$curcontext" use-pattern; then
247+
pattern="$PREFIX*$SUFFIX"
248+
fi
231249
typeset -gA _sys_unit_state
232-
_sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files)"}%%[[:space:]]#}% *} )
250+
_sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files $pattern)"}%%[[:space:]]#}% *} )
233251
}
234252

235253
local fun

0 commit comments

Comments
 (0)