zsh: remove usage of PREFIX in _systemctl

The usage of PREFIX in this completion is mostly counter to the intended
usage of compsys in zsh. It is generally expected that completion code
provide the available completions and tags in that word position so that
compsys, with user configuration, can filter them to the appropriate set.

One egregious error caused by the usage of PREFIX here is the caching of
SYS_ALL_UNITS, which stored only the unit names prematurely filtered by
the completion prefix, affecting all future completions. For example,

  $ systemctl cat nonsense<TAB>

might find no matching units if nonsense* has no matches, but now

  $ systemctl cat <TAB>

will fail in all future completions even though every unit file
is a valid match, because the cached set has been erroneously filtered
by the last prefix.
This commit is contained in:
Ronan Pigott 2023-05-05 12:33:29 -07:00 committed by Luca Boccassi
parent 362235bf59
commit 8139407ec1

View file

@ -160,8 +160,8 @@ __systemctl()
if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) ||
! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
then
_sys_all_units=( ${${(f)"$(__systemctl list-units --all "$PREFIX*" )"}%% *} )
_store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
_sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
_store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_unis
fi
}
@ -174,7 +174,7 @@ __systemctl()
if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) ||
! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr;
then
all_unit_files=( ${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%% *} )
all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
_systemctl_all_units
really_all_units=($_sys_all_units $all_unit_files)
_sys_really_all_units=(${(u)really_all_units})
@ -193,22 +193,22 @@ __systemctl()
(( $+functions[_systemctl_get_non_template_names] )) ||
_systemctl_get_non_template_names() { echo -E - ${^${(R)${(f)"$(
__systemctl list-unit-files "$PREFIX*"
__systemctl list-units --all "$PREFIX*"
__systemctl list-unit-files
__systemctl list-units --all
)"}:#*@.*}%%[[:space:]]*} }
(( $+functions[_systemctl_get_template_names] )) ||
_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}##*@.[^[:space:]]##}%%@.*}\@ }
_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ }
(( $+functions[_systemctl_active_units] )) ||
_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units "$PREFIX*" )"}%% *} )}
_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )}
(( $+functions[_systemctl_startable_units] )) ||
_systemctl_startable_units(){
_sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
_filter_units_by_property CanStart yes ${${${(f)"$(
__systemctl list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$PREFIX*"
__systemctl list-units --state inactive,failed "$PREFIX*"
__systemctl list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient
__systemctl list-units --state inactive,failed
)"}:#*@.*}%%[[:space:]]*}
)) )
}
@ -216,19 +216,19 @@ __systemctl()
(( $+functions[_systemctl_restartable_units] )) ||
_systemctl_restartable_units(){
_sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
__systemctl list-unit-files --state enabled,disabled,static "$PREFIX*"
__systemctl list-units "$PREFIX*"
__systemctl list-unit-files --state enabled,disabled,static
__systemctl list-units
)"}:#*@.*}%%[[:space:]]*} ) )
}
(( $+functions[_systemctl_failed_units] )) ||
_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed "$PREFIX*" )"}%% *} ) }
_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed)"}%% *} ) }
(( $+functions[_systemctl_unit_state] )) ||
_systemctl_unit_state() {
setopt localoptions extendedglob
typeset -gA _sys_unit_state
_sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%%[[:space:]]#}% *} )
_sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files)"}%%[[:space:]]#}% *} )
}
local fun
@ -351,7 +351,7 @@ done
(( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
{
_wanted systemd-targets expl target \
compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all "$PREFIX*" )"}%% *} ||
compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
_message "no targets found"
}