mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 04:43:10 +00:00
Refactor service
completions to drop eval
usage under FreeBSD
This commit is contained in:
parent
8f166cbb2a
commit
31745d29ab
1 changed files with 17 additions and 6 deletions
|
@ -1,11 +1,6 @@
|
|||
# Fist argument is the names of the service, i.e. a file in /etc/init.d
|
||||
complete -c service -n "__fish_is_first_token" -xa "(__fish_print_service_names)" -d "Service name"
|
||||
|
||||
#The second argument is what action to take with the service
|
||||
function __fish_complete_static_service_actions
|
||||
complete -c service -n "not __fish_is_first_token" -xa "(eval echo -- $service_commands)"
|
||||
end
|
||||
|
||||
set -l service_commands
|
||||
|
||||
# as found in __fish_print_service_names.fish
|
||||
|
@ -21,5 +16,21 @@ else if test -d /etc/init.d # SysV on Debian and other linuxen
|
|||
else # FreeBSD
|
||||
# Use the output of `service -v foo` to retrieve the list of service-specific verbs
|
||||
# We can safely use `sed` here because this is platform-specific
|
||||
complete -c service -n "not __fish_is_first_token" -xa "(set -l service_name (commandline --tokenize --cut-at-cursor)[-1]; eval printf '%s\n' (service \$service_name -v 2>| sed -rn 's/Usage.*\[/\{,/;s/\]|\)/\}/g;s/\|/,/g;s/\(/{/gp'))"
|
||||
complete -c service -n "not __fish_is_first_token" -xa "(__fish_complete_freebsd_service_actions)"
|
||||
end
|
||||
|
||||
function __fish_complete_static_service_actions
|
||||
#The second argument is what action to take with the service
|
||||
complete -c service -n "not __fish_is_first_token" -xa "$service_commands"
|
||||
end
|
||||
|
||||
function __fish_complete_freebsd_service_actions
|
||||
# Use the output of `service -v foo` to retrieve the list of service-specific verbs
|
||||
# Output takes the form "[prefix1 prefix2 ..](cmd1 cmd2 cmd3)" where any combination
|
||||
# of zero or one prefixe(s) and any one command is a valid verb.
|
||||
set -l service_name (commandline --tokenize --cut-at-cursor)[-1]
|
||||
set -l results (service $service_name -v 2>| string match -r '\\[(.*)\\]\\((.*)\\)')
|
||||
set -l prefixes "" (string split '|' -- $results[2])
|
||||
set -l commands (string split '|' -- $results[3])
|
||||
printf '%s\n' $prefixes$commands
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue