fix(cli): allow `omz` commands to be used in a script (#10645)
The commands `omz plugin {enable,disable}` and `omz theme set`
automatically reload the zsh session on success. With this
change, the CLI checks whether the commands are run in an
interactive session before reloading the zsh session.
This change also conditionally sets the completion function
for `omz` so that it's not done in a non-interactive session.
Diff
1diff --git a/lib/cli.zsh b/lib/cli.zsh
2index edc10e722d780055b2437f39cf73614df0407665..e7f99701305d72103a78f90ad907c340fdaf7b4e 100644
3--- a/lib/cli.zsh
4+++ b/lib/cli.zsh
5@@ -105,7 +105,10 @@ function _omz {
6 return 0
7 }
8 9-compdef _omz omz
10+# If run from a script, do not set the completion function
11+if (( ${+functions[compdef]} )); then
12+ compdef _omz omz
13+fi
1415 ## Utility functions
1617@@ -299,10 +302,8 @@ multi == 1 && length(\$0) > 0 {
18 # Restart the zsh session if there were no errors
19 _omz::log info "plugins disabled: ${(j:, :)dis_plugins}."
2021- # Old zsh versions don't have ZSH_ARGZERO
22- local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
23- # Check whether to run a login shell
24- [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
25+ # Only reload zsh if run in an interactive session
26+ [[ ! -o interactive ]] || _omz::reload
27 }
2829 function _omz::plugin::enable {
30@@ -375,10 +376,8 @@ multi == 1 && /^[^#]*\)/ {
31 # Restart the zsh session if there were no errors
32 _omz::log info "plugins enabled: ${(j:, :)add_plugins}."
3334- # Old zsh versions don't have ZSH_ARGZERO
35- local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
36- # Check whether to run a login shell
37- [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
38+ # Only reload zsh if run in an interactive session
39+ [[ ! -o interactive ]] || _omz::reload
40 }
4142 function _omz::plugin::info {
43@@ -731,10 +730,8 @@ EOF
44 # Restart the zsh session if there were no errors
45 _omz::log info "'$1' theme set correctly."
4647- # Old zsh versions don't have ZSH_ARGZERO
48- local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
49- # Check whether to run a login shell
50- [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
51+ # Only reload zsh if run in an interactive session
52+ [[ ! -o interactive ]] || _omz::reload
53 }
5455 function _omz::theme::use {