diff --git a/plugins/virtualbox/README.md b/plugins/virtualbox/README.md new file mode 100644 index 000000000..2d4a1e3fc --- /dev/null +++ b/plugins/virtualbox/README.md @@ -0,0 +1,81 @@ +# VirtualBox plugin + +The `virtualbox` plugin provides many useful aliases for VirtualBox. + +To use it, add `virtualbox` to the plugins array of your `.zshrc` file: + +```zsh +plugins=(... virtualbox) +``` + +## Aliases + +| Alias | Command | Description | +|:-----------------------|:--------------------------------------|:-------------------------------------------------------------------| +| `vbox-start` | `VBoxManage startvm` | Start a virtual machine | +| `vbox-start-headless` | `VBoxManage startvm --type=headless` | Start a virtual machine (headless mode) | +| `vbox-clone` | `VBoxManage clonevm --register` | Clone and register a virtual machine | +| `vbox-create` | `VBoxManage createvm --register` | Create and register a new virtual machine | +| `vbox-create-medium` | `VBoxManage createmedium` | Create a new medium | +| `vbox-delete` | `VBoxManage unregistervm --delete` | Unregister and delete a virtual machine | +| `vbox-control` | `VBoxManage controlvm` | Control a virtual machine | +| `vbox-info` | `VBoxManage showvminfo` | Show information about a virtual machine | +| `vbox-list` | `VBoxManage list` | List system information and configuration | +| `vbox-poweroff` | `VBoxManage controlvm "$1" poweroff` | Forcibly power off a virtual machine | +| `vbox-shutdown` | `VBoxManage controlvm "$1" shutdown` | Gracefully shutdown a virtual machine | +| `vbox-stop` | `aliased to vbox-shutdown` | - | +| `vbox-pause` | `VBoxManage controlvm "$1" pause` | Pause execution of a virtual machine | +| `vbox-resume` | `VBoxManage controlvm "$1" resume` | Resume execution of a virtual machine | +| `vbox-save` | `VBoxManage controlvm "$1" savestate` | Save current state of a virtual machine | +| `vbox-discard` | `VBoxManage discardstate` | Discard saved state of a virtual machine | +| `vbox-reboot` | `VBoxManage controlvm "$1" reboot` | Reboot a virtual machine | +| `vbox-reset` | `VBoxManage controlvm "$1" reset` | Reset a virtual machine | + +The prefix for the aliases can be customised by setting `ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX` (default: `vbox`). + +## Status prompt + +Add `$(virtualbox_prompt_info [vm_name] ...)` to your prompt to show the status +of all specified virtual machines. + +The plugin will add the following to your prompt for each `$vm_name`. + +```text +: +``` + +You can control these parts with the following variables: + +- ``: Set `$ZSH_THEME_VIRTUALBOX_PROMPT_PREFIX`. + +- ``: Set `$ZSH_THEME_VIRTUALBOX_PROMPT_SUFFIX`. + +- ``: name passed as parameter to the function. If you want it to be in ALL CAPS, + you can set the variable `$ZSH_THEME_VIRTUALBOX_PROMPT_CAPS` to a non-empty string. + +- ``: shown if the virtual machine is running. + Set `$ZSH_THEME_VIRTUALBOX_PROMPT_RUNNING`. + +- ``: shown if the virtual machine is *not* running. + Set `$ZSH_THEME_VIRTUALBOX_PROMPT_NOTRUNNING`. + +You can set `ZSH_THEME_VIRTUALBOX_PROMPT_COUNT` to a non-empty string to show a +count of running virtual machines / total. + +For example, if your prompt contains `PROMPT='$(virtualbox_prompt_info "Arch Linux" "Debian")'` +and you set the following variables: + +```sh +ZSH_THEME_VIRTUALBOX_PROMPT_COUNT="true" +ZSH_THEME_VIRTUALBOX_PROMPT_PREFIX="[" +ZSH_THEME_VIRTUALBOX_PROMPT_SUFFIX="]" +ZSH_THEME_VIRTUALBOX_PROMPT_RUNNING="+" +ZSH_THEME_VIRTUALBOX_PROMPT_NOTRUNNING="X" +ZSH_THEME_VIRTUALBOX_PROMPT_CAPS="true" +``` + +If `Arch Linux` is running, and `Debian` is not, then your prompt will look like this: + +```text +[1 / 2][ARCH LINUX: +][DEBIAN: X] +``` diff --git a/plugins/virtualbox/virtualbox.plugin.zsh b/plugins/virtualbox/virtualbox.plugin.zsh new file mode 100644 index 000000000..4dec6ec42 --- /dev/null +++ b/plugins/virtualbox/virtualbox.plugin.zsh @@ -0,0 +1,85 @@ +# VirtualBox aliases + +ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX="${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX:-vbox}" + +# VBoxManage +declare -A manage_commands=( + ["start"]="startvm" + ["start-headless"]="startvm --type=headless" + ["clone"]="clonevm --register" + ["create"]="createvm --register" + ["create-medium"]="createmedium" + ["discard"]="discardstate" + ["delete"]="unregistervm --delete" + ["control"]="controlvm" + ["info"]="showvminfo" + ["list"]="list" +) + +for c in "${(k)manage_commands[@]}"; do + alias "${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-${c}"="VBoxManage ${manage_commands[${c}]}" +done + +unset c manage_commands + +# Functions +function ${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-poweroff() { + VBoxManage controlvm "$1" poweroff +} + +function ${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-shutdown() { + VBoxManage controlvm "$1" shutdown +} + +function ${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-pause() { + VBoxManage controlvm "$1" pause +} + +function ${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-resume() { + VBoxManage controlvm "$1" resume +} + +function ${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-save() { + VBoxManage controlvm "$1" savestate +} + +function ${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-reboot() { + VBoxManage controlvm "$1" reboot +} + +function ${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-reset() { + VBoxManage controlvm "$1" reset +} + +alias "${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-stop"="${ZSH_PLUGIN_VIRTUALBOX_ALIAS_PREFIX}-shutdown" + +# VirtualBox prompt +function virtualbox_prompt_info() { + if [[ -n "${ZSH_THEME_VIRTUALBOX_PROMPT_COUNT}" ]]; then + local vm_count="$(VBoxManage list runningvms | wc -l)" + local vm_total="$(VBoxManage list vms | wc -l)" + + echo -n "${ZSH_THEME_VIRTUALBOX_PROMPT_PREFIX}" + echo -n "${vm_count} / ${vm_total}" + echo -n "${ZSH_THEME_VIRTUALBOX_PROMPT_SUFFIX}" + fi + + local vm_name + for vm_name in "$@"; do + echo -n "${ZSH_THEME_VIRTUALBOX_PROMPT_PREFIX}" + + if [[ -n "${ZSH_THEME_VIRTUALBOX_PROMPT_CAPS}" ]]; then + echo -n "${(U)vm_name:gs/%/%%}:" + else + echo -n "${vm_name:gs/%/%%}:" + fi + + if VBoxManage list runningvms | grep -w "\"${vm_name}\"" &>/dev/null; then + echo -n "${ZSH_THEME_VIRTUALBOX_PROMPT_RUNNING}" + else + echo -n "${ZSH_THEME_VIRTUALBOX_PROMPT_NOTRUNNING}" + fi + + echo -n "${ZSH_THEME_VIRTUALBOX_PROMPT_SUFFIX}" + done +}