From 3e7a8d4f107aef2152df27e38127aaf8b10c8faf Mon Sep 17 00:00:00 2001 From: Fabian Ising Date: Tue, 28 Jul 2026 10:23:36 +0200 Subject: [PATCH] [ZSH] Macos completions --- zsh/.mac_config.zsh_mac.example | 5 ++ zsh/completions_mac/_launchctl | 127 ++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 zsh/completions_mac/_launchctl diff --git a/zsh/.mac_config.zsh_mac.example b/zsh/.mac_config.zsh_mac.example index 82a0a8b..8327cc9 100644 --- a/zsh/.mac_config.zsh_mac.example +++ b/zsh/.mac_config.zsh_mac.example @@ -14,6 +14,11 @@ else fi export PATH="$HOMEBREW_PREFIX/opt/coreutils/libexec/gnubin:$HOME/.local/bin:$HOMEBREW_PREFIX/bin:$PATH:$HOMEBREW_PREFIX/sbin" +# macOS-specific completion functions. +fpath=("${ZDOTDIR:-$HOME}/.zsh/completions_mac" $fpath) +autoload -Uz _launchctl +compdef _launchctl launchctl + # test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh" # Research diff --git a/zsh/completions_mac/_launchctl b/zsh/completions_mac/_launchctl new file mode 100644 index 0000000..a8c6482 --- /dev/null +++ b/zsh/completions_mac/_launchctl @@ -0,0 +1,127 @@ +#compdef launchctl +# macOS launchd completion. + +_launchctl_domains() { + local uid=$EUID + local -a domains=( + 'system:system services' + "user/${uid}:services for the current user" + "gui/${uid}:services in the current graphical session" + ) + _describe -t domains 'launchd domain' domains +} + +_launchctl_service_targets() { + local uid=$EUID label + local -a targets=() + + while IFS=$'\t' read -r _ _ label; do + [[ -n $label ]] && targets+=("gui/${uid}/${label}") + done < <(launchctl list 2>/dev/null | tail -n +2) + + if (( ${#targets} )); then + _describe -t services 'launchd service target' targets + else + _message 'launchd service target' + fi +} + +_launchctl() { + local context state state_descr line + typeset -A opt_args + + local -a commands=( + 'bootstrap:register services or directories in a domain' + 'bootout:unregister services or directories from a domain' + 'enable:enable a service' + 'disable:disable a service' + 'kickstart:run or restart a service' + 'kill:send a signal to a service' + 'print:show a domain or service' + 'print-disabled:show persistent enable and disable overrides' + 'list:list loaded services' + 'start:start a legacy service by label' + 'stop:stop a legacy service by label' + 'remove:remove a legacy service by label' + 'load:load legacy plist files' + 'unload:unload legacy plist files' + 'setenv:set an environment variable' + 'unsetenv:unset an environment variable' + 'getenv:get an environment variable' + 'limit:view or change resource limits' + 'config:set persistent launchd configuration' + 'dumpstate:dump launchd state' + 'reboot:reboot a launchd domain or the system' + 'version:show launchd version' + 'help:show launchctl help' + ) + + _arguments -C \ + '1:command:->command' \ + '*::argument:->args' + + case $state in + command) + _describe -t commands 'launchctl command' commands + ;; + args) + case $words[2] in + bootstrap) + if (( CURRENT == 3 )); then + _launchctl_domains + else + _files -g '*.plist(-.)' + fi + ;; + bootout) + if (( CURRENT == 3 )); then + _launchctl_domains + _launchctl_service_targets + else + _files -g '*.plist(-.)' + fi + ;; + enable|disable|kickstart) + _launchctl_service_targets + ;; + print) + _launchctl_domains + _launchctl_service_targets + ;; + print-disabled) + _launchctl_domains + ;; + load|unload) + _files -g '*.plist(-.)' + ;; + start|stop|remove) + local -a labels + labels=(${${(f)"$(launchctl list 2>/dev/null)"}[2,-1]##*$'\t'}) + _describe -t services 'service label' labels + ;; + kill) + if (( CURRENT == 3 )); then + _signals + else + _launchctl_service_targets + fi + ;; + setenv) + if (( CURRENT == 3 )); then + _parameters + else + _message 'value' + fi + ;; + getenv|unsetenv) + _parameters + ;; + *) + _message 'argument' + ;; + esac + ;; + esac +} + +_launchctl "$@"