90c2c25e23c2622324ac36e3be317d5c50dd238c

Author
Michael Aquilina <michaelaquilina@gmail.com>
Committer
Michael Aquilina <michaelaquilina@gmail.com>
Date

Message

Detect whether pwgen is installed

Diff

 1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 2index 071348cfc456d06b1fb989ae976fe78b92db9932..a76bd56e048187012bceb5c448c518a6698bd0a5 100644
 3--- a/autoswitch_virtualenv.plugin.zsh
 4+++ b/autoswitch_virtualenv.plugin.zsh
 5@@ -7,7 +7,6 @@ PURPLE="\e[35m"
 6 BOLD="\e[1m"
 7 NORMAL="\e[0m"
 8 
 9-
10 function _validated_source() {
11     local target_path="$1"
12 
13@@ -384,7 +383,6 @@ function install_requirements() {
14 
15 
16 function enable_autoswitch_virtualenv() {
17-    autoload -Uz add-zsh-hook
18     disable_autoswitch_virtualenv
19     add-zsh-hook chpwd check_venv
20 }
21@@ -397,12 +395,18 @@ function disable_autoswitch_virtualenv() {
22 # This function is only used to startup zsh-autoswitch-virtualenv
23 # the first time a terminal is started up
24 # it waits for the terminal to be ready using precmd and then
25-# imediately removes itself from the zsh-hook
26+# immediately removes itself from the zsh-hook.
27+# This seems important for "instant prompt" zsh themes like powerlevel10k
28 function _autoswitch_startup() {
29     add-zsh-hook -D precmd _startup
30-    check_venv
31-}
32 
33+    if ! type pwgen 1>/dev/null; then
34+        printf "${PURPLE}pwgen is required for zsh-autoswitch-virtualenv to run${NONE}\n"
35+    else
36+        enable_autoswitch_virtualenv
37+        check_venv
38+    fi
39+}
40 
41-enable_autoswitch_virtualenv
42+autoload -Uz add-zsh-hook
43 add-zsh-hook precmd _autoswitch_startup
44diff --git a/tests/test_plugin.zunit b/tests/test_plugin.zunit
45index eeb2c5fe46f9111caa61d9762f296afcad97aead..8a3ebd89b16fe383d37faa8a4049cdaec0ca4f1b 100644
46--- a/tests/test_plugin.zunit
47+++ b/tests/test_plugin.zunit
48@@ -39,6 +39,9 @@
49 @test 'plugin - switches env when .venv found' {
50     unset AUTOSWITCH_DEFAULTENV
51     load ../autoswitch_virtualenv.plugin.zsh
52+    # zsh-hook precmd does not seem to work correctly in tests
53+    # so we need to manually enable the plugin
54+    enable_autoswitch_virtualenv
55 
56     TEMP_DIR="$(mktemp -d)"
57     echo "foobar" > "$TEMP_DIR/.venv"