29a2394c8f88efe04a61460a9a171228c39b1de0

Author
Alberto Re <alberto.re@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Adds `vagrant_prompt_info` function to your shell (#4081)

Diff

 1diff --git a/plugins/vagrant-prompt/README.md b/plugins/vagrant-prompt/README.md
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..c5bc55d17741ab8c663ca9f95a269fda6a7f46fd
 4--- /dev/null
 5+++ b/plugins/vagrant-prompt/README.md
 6@@ -0,0 +1,6 @@
 7+This plugin prompts the status of the Vagrant VMs. It supports single-host and
 8+multi-host configurations as well.
 9+
10+Look inside the source for documentation about custom variables. 
11+
12+Alberto Re <alberto.re@gmail.com>
13diff --git a/plugins/vagrant-prompt/vagrant-prompt.plugin.zsh b/plugins/vagrant-prompt/vagrant-prompt.plugin.zsh
14new file mode 100644
15index 0000000000000000000000000000000000000000..28bf31f9143ca0a26bfbdfe152fc0390da37cde5
16--- /dev/null
17+++ b/plugins/vagrant-prompt/vagrant-prompt.plugin.zsh
18@@ -0,0 +1,38 @@
19+# vim:ft=zsh ts=2 sw=2 sts=2
20+#
21+# To display Vagrant infos on your prompt add the vagrant_prompt_info to the
22+# $PROMPT variable in your theme. Example:
23+#
24+# PROMPT='%{$fg[$NCOLOR]%}%B%n%b%{$reset_color%}:%{$fg[blue]%}%B%c/%b%{$reset_color%} $(vagrant_prompt_info)$(svn_prompt_info)$(git_prompt_info)%(!.#.$) '
25+#
26+# `vagrant_prompt_info` makes use of some custom variables. This is an example
27+# definition:
28+#
29+# ZSH_THEME_VAGRANT_PROMPT_PREFIX="%{$fg_bold[blue]%}["
30+# ZSH_THEME_VAGRANT_PROMPT_SUFFIX="%{$fg_bold[blue]%}]%{$reset_color%} "
31+# ZSH_THEME_VAGRANT_PROMPT_RUNNING="%{$fg_no_bold[green]%}●"
32+# ZSH_THEME_VAGRANT_PROMPT_POWEROFF="%{$fg_no_bold[red]%}●"
33+# ZSH_THEME_VAGRANT_PROMPT_SUSPENDED="%{$fg_no_bold[yellow]%}●"
34+# ZSH_THEME_VAGRANT_PROMPT_NOT_CREATED="%{$fg_no_bold[white]%}○"
35+
36+function vagrant_prompt_info() {
37+  test -d .vagrant && test -f Vagrantfile
38+  if [[ "$?" == "0" ]]; then
39+    statuses=$(vagrant status 2> /dev/null | grep -P "\w+\s+[\w\s]+\s\(\w+\)")
40+    statuses=("${(f)statuses}")
41+    printf '%s' $ZSH_THEME_VAGRANT_PROMPT_PREFIX
42+    for vm_details in $statuses; do
43+      vm_state=$(echo $vm_details | grep -o -E "saved|poweroff|not created|running")
44+      if [[ "$vm_state" == "running" ]]; then
45+        printf '%s' $ZSH_THEME_VAGRANT_PROMPT_RUNNING
46+      elif [[ "$vm_state" == "saved" ]]; then
47+        printf '%s' $ZSH_THEME_VAGRANT_PROMPT_SUSPENDED
48+      elif [[ "$vm_state" == "not created" ]]; then
49+        printf '%s' $ZSH_THEME_VAGRANT_PROMPT_NOT_CREATED
50+      elif [[ "$vm_state" == "poweroff" ]]; then
51+        printf '%s' $ZSH_THEME_VAGRANT_PROMPT_POWEROFF
52+      fi
53+    done
54+    printf '%s' $ZSH_THEME_VAGRANT_PROMPT_SUFFIX
55+  fi
56+}