fa64758aeaeb17abdefca5d193c1002230048977

Author
Marc Cornellà <marc@mcornella.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix(vagrant-prompt): make `vagrant_prompt_info` generic for any state (#12782)

Diff

 1diff --git a/plugins/vagrant-prompt/README.md b/plugins/vagrant-prompt/README.md
 2index 0720f4bdea66b708d0daf2b42f2721ba533e21e9..f7bfce4f361617c7a3a71cf60582753235e454fc 100644
 3--- a/plugins/vagrant-prompt/README.md
 4+++ b/plugins/vagrant-prompt/README.md
 5@@ -17,11 +17,15 @@ To display Vagrant info on your prompt add the `vagrant_prompt_info` to the
 6 `$PROMPT` or `$RPROMPT` variable in your theme. Example:
 7 
 8 ```zsh
 9-PROMPT='%{$fg[$NCOLOR]%}%B%n%b%{$reset_color%}:%{$fg[blue]%}%B%c/%b%{$reset_color%} $(vagrant_prompt_info)$(svn_prompt_info)$(git_prompt_info)%(!.#.$) '
10+PROMPT="$PROMPT"' $(vagrant_prompt_info)'
11+# or
12+RPROMPT='$(vagrant_prompt_info)'
13 ```
14 
15-`vagrant_prompt_info` makes use of some custom variables. This is an example
16-definition:
17+### Customization
18+
19+`vagrant_prompt_info` makes use of the following custom variables, which can be set in your
20+`.zshrc` file:
21 
22 ```zsh
23 ZSH_THEME_VAGRANT_PROMPT_PREFIX="%{$fg_bold[blue]%}["
24@@ -31,3 +35,18 @@ ZSH_THEME_VAGRANT_PROMPT_POWEROFF="%{$fg_no_bold[red]%}●"
25 ZSH_THEME_VAGRANT_PROMPT_SUSPENDED="%{$fg_no_bold[yellow]%}●"
26 ZSH_THEME_VAGRANT_PROMPT_NOT_CREATED="%{$fg_no_bold[white]%}○"
27 ```
28+
29+### State to variable mapping
30+
31+The plugin uses the output reported by `vagrant status` to print whichever symbol matches,
32+according to the following table:
33+
34+| State       | Symbol                                 |
35+| ----------- | -------------------------------------- |
36+| running     | `ZSH_THEME_VAGRANT_PROMPT_RUNNING`     |
37+| not running | `ZSH_THEME_VAGRANT_PROMPT_POWEROFF`    |
38+| poweroff    | `ZSH_THEME_VAGRANT_PROMPT_POWEROFF`    |
39+| paused      | `ZSH_THEME_VAGRANT_PROMPT_SUSPENDED`   |
40+| saved       | `ZSH_THEME_VAGRANT_PROMPT_SUSPENDED`   |
41+| suspended   | `ZSH_THEME_VAGRANT_PROMPT_SUSPENDED`   |
42+| not created | `ZSH_THEME_VAGRANT_PROMPT_NOT_CREATED` |
43diff --git a/plugins/vagrant-prompt/vagrant-prompt.plugin.zsh b/plugins/vagrant-prompt/vagrant-prompt.plugin.zsh
44index 29f4038c5169bb129856bd26f6d6f01a688bb217..2d8455a534fe03be54dbb37932e2567540abe2f1 100644
45--- a/plugins/vagrant-prompt/vagrant-prompt.plugin.zsh
46+++ b/plugins/vagrant-prompt/vagrant-prompt.plugin.zsh
47@@ -1,16 +1,18 @@
48 function vagrant_prompt_info() {
49-  local vm_states vm_state
50-  if [[ -d .vagrant && -f Vagrantfile ]]; then
51-    vm_states=(${(f)"$(vagrant status 2> /dev/null | sed -nE 's/^.*(saved|poweroff|running|not created) \([[:alnum:]_]+\)$/\1/p')"})
52-    printf '%s' $ZSH_THEME_VAGRANT_PROMPT_PREFIX
53-    for vm_state in $vm_states; do
54-      case "$vm_state" in
55-        saved) printf '%s' $ZSH_THEME_VAGRANT_PROMPT_SUSPENDED ;;
56-        running) printf '%s' $ZSH_THEME_VAGRANT_PROMPT_RUNNING ;;
57-        poweroff) printf '%s' $ZSH_THEME_VAGRANT_PROMPT_POWEROFF ;;
58-        "not created") printf '%s' $ZSH_THEME_VAGRANT_PROMPT_NOT_CREATED ;;
59-      esac
60-    done
61-    printf '%s' $ZSH_THEME_VAGRANT_PROMPT_SUFFIX
62+  if [[ ! -d .vagrant || ! -f Vagrantfile ]]; then
63+    return
64   fi
65+
66+  local vm_states vm_state
67+  vm_states=(${(f)"$(vagrant status 2> /dev/null | sed -nE 's/^[^ ]* *([[:alnum:] ]*) \([[:alnum:]_]+\)$/\1/p')"})
68+  printf '%s' $ZSH_THEME_VAGRANT_PROMPT_PREFIX
69+  for vm_state in $vm_states; do
70+    case "$vm_state" in
71+      running) printf '%s' $ZSH_THEME_VAGRANT_PROMPT_RUNNING ;;
72+      "not running"|poweroff) printf '%s' $ZSH_THEME_VAGRANT_PROMPT_POWEROFF ;;
73+      paused|saved|suspended) printf '%s' $ZSH_THEME_VAGRANT_PROMPT_SUSPENDED ;;
74+      "not created") printf '%s' $ZSH_THEME_VAGRANT_PROMPT_NOT_CREATED ;;
75+    esac
76+  done
77+  printf '%s' $ZSH_THEME_VAGRANT_PROMPT_SUFFIX
78 }