diff --git a/plugins/omz-bootstrap/README.md b/plugins/omz-bootstrap/README.md new file mode 100644 index 000000000..776997ec1 --- /dev/null +++ b/plugins/omz-bootstrap/README.md @@ -0,0 +1,10 @@ +# Oh My Zsh Bootstrap + +**Maintainer:** [**@ncanceill**](https://github.com/ncanceill) + +_Oh My Zsh Bootstrap_ is a plugin for [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh). Enable it by adding `omz-bootstrap` to your plugins. + +It is inspired by [mbauhardt's project](https://github.com/mbauhardt/oh-my-zsh-bootstrap). + +It provides the `omz` function, which helps you manage your themes and plugins. + diff --git a/plugins/omz-bootstrap/_omz b/plugins/omz-bootstrap/_omz new file mode 100644 index 000000000..883332ce9 --- /dev/null +++ b/plugins/omz-bootstrap/_omz @@ -0,0 +1,147 @@ +#compdef omz + +# Completion for the omz-plugin command +# Author: github.com/ncanceill + +_omz () { + local curcontext=$curcontext state line + declare -A opt_args + _arguments -C \ + ':command:->command' \ + '*::options:->options' && ret=0 + case $state in + (command) + declare -a commands + commands=( + 'plugin:manage plugins' + 'theme:manage themes' + ) + _describe -t commands 'command' commands && ret=0 + ;; + (options) + case $line[1] in + (plugin) + _omz-plugin && ret=0 + ;; + (theme) + _omz-theme && ret=0 + ;; + esac + ;; + esac + return ret +} + +_omz-plugin () { + local curcontext=$curcontext state line + declare -A opt_args + _arguments -C \ + ':command:->command' \ + '*::options:->options' && ret=0 + case $state in + (command) + declare -a commands + commands=( + 'ls:list available plugins' + 'on:enable a plugin' + 'off:disable a plugin' + 'up:update a plugin' + 'get:download and enable a plugin' + ) + _describe -t commands 'sub-command' commands && ret=0 + ;; + (options) + case $line[1] in + (on) + _plugins_off && ret=0 + ;; + (off) + _plugins_on && ret=0 + ;; + (up) + _plugins && ret=0 + ;; + esac + ;; + esac + return ret +} + +_plugins () { + declare -a plugins + plugins=() + for plugin ($ZSH/plugins/* $ZSH_CUSTOM/plugins/*(N)); do + plugins+=$plugin:t:r + done + _describe -t plugins 'plugin' plugins && return 0 +} + +_plugins_on () { + declare -a plugins_on + plugins_on=() + for plugin ($ZSH/plugins/* $ZSH_CUSTOM/plugins/*(N)); do + local plugin_name=$plugin:t:r + _map_exists plugins $plugin_name + if [[ $? -eq 0 ]]; then + plugins_on+=$plugin_name + fi + done + _describe -t plugins_on 'plugin' plugins_on && return 0 +} + +_plugins_off () { + declare -a plugins_off + plugins_off=() + for plugin ($ZSH/plugins/* $ZSH_CUSTOM/plugins/*(N)); do + local plugin_name=$plugin:t:r + _map_exists plugins $plugin_name + if [[ $? -ne 0 ]]; then + plugins_off+=$plugin_name + fi + done + _describe -t plugins_off 'plugin' plugins_off && return 0 +} + +_omz-theme () { + local curcontext=$curcontext state line + declare -A opt_args + _arguments -C \ + ':command:->command' \ + '*::options:->options' && ret=0 + case $state in + (command) + declare -a commands + commands=( + 'ls:list available themes' + 'set:set a theme' + 'up:update a theme' + 'get:download and enable a theme' + ) + _describe -t commands 'sub-command' commands && ret=0 + ;; + (options) + case $line[1] in + (set) + _arguments \ + ':theme:_themes' && ret=0 + ;; + (up) + _arguments \ + ':theme:_themes' && ret=0 + ;; + esac + ;; + esac + return ret +} + +_themes () { + declare -a themes + themes=() + for theme ($ZSH/themes/*zsh-theme $ZSH_CUSTOM/*zsh-theme(N)); do + themes+=$theme:t:r + done + _describe -t themes 'theme' themes && return 0 +} + +_omz diff --git a/plugins/omz-bootstrap/lib/download.zsh b/plugins/omz-bootstrap/lib/download.zsh new file mode 100644 index 000000000..c463d1b9a --- /dev/null +++ b/plugins/omz-bootstrap/lib/download.zsh @@ -0,0 +1,21 @@ +_clone_git() { + local url=$1; local folder=$2 + if [[ ! -d "$folder" ]]; then + command git clone $url $folder + fi +} + +_download_plugin() { + [[ -z "$1" || -z "$2" ]] && return 1 + local url=$1; local name=$2 + _clone_git $url $ZSH_CUSTOM/plugins/$name + _map_put plugins $name enabled +} + +_download_theme() { + [[ -z "$1" || -z "$2" ]] && return 1 + local url=$1; local theme=$2 + _clone_git $url $ZSH_CUSTOM/themes/$theme + command ln -s $ZSH_CUSTOM/themes/$theme/$theme.zsh-theme $ZSH_CUSTOM/$theme.zsh-theme + _map_put themes theme $theme +} diff --git a/plugins/omz-bootstrap/lib/map.zsh b/plugins/omz-bootstrap/lib/map.zsh new file mode 100644 index 000000000..c5d295781 --- /dev/null +++ b/plugins/omz-bootstrap/lib/map.zsh @@ -0,0 +1,40 @@ +mapdir="$ZSH/cache/omz-bootstrap" + +_map_init() { + command mkdir -p "$mapdir" +} + +_map_put() { + [[ -z "$1" || -z "$2" || -z "$3" ]] && return 1 + local mapname=$1; local key=$2; local value=$3 + [[ -d "${mapdir}/${mapname}" ]] || mkdir "${mapdir}/${mapname}" + echo $value > "${mapdir}/${mapname}/${key}" +} + +_map_get() { + [[ -z "$1" || -z "$2" ]] && return 1 + local mapname=$1; local key=$2 + cat "${mapdir}/${mapname}/${key}" +} + +_map_keys() { + [[ -z "$1" ]] && return 1 + local mapname=$1 + for key ($mapdir/$mapname/*); do + basename $key + done +} + +_map_exists() { + [[ -z "$1" || -z "$2" ]] && return 1 + local mapname=$1; local key=$2 + [[ -f "${mapdir}/${mapname}/${key}" ]] && return 0 +} + +_map_remove() { + [[ -z "$1" || -z "$2" ]] && return 1 + local mapname=$1; local key=$2 + command rm "${mapdir}/${mapname}/${key}" +} + +_map_init diff --git a/plugins/omz-bootstrap/lib/repository.zsh b/plugins/omz-bootstrap/lib/repository.zsh new file mode 100644 index 000000000..1fbbcd33b --- /dev/null +++ b/plugins/omz-bootstrap/lib/repository.zsh @@ -0,0 +1,132 @@ +_init_theme() { + _map_exists themes theme + [[ $? -ne 0 ]] && _map_put themes theme $ZSH_THEME +} + +_pre_enable_plugins() { + for plugin ($plugins); do + _map_put plugins $plugin pre_enabled + done +} + +_list_plugins() { + for plugin ($ZSH/plugins/* $ZSH_CUSTOM/plugins/*(N)); do + local plugin_name=$(basename $plugin) + local enabled=disabled + _map_exists plugins $plugin_name + if [[ $? -eq 0 ]]; then + enabled=$(_map_get plugins $plugin_name) + fi + if [[ $enabled = "pre_enabled" || $enabled = "enabled" ]]; then + printf "%-30s \033[0;32m%-10s\033[0m\n" $plugin_name $enabled + else + printf "%-30s \033[0;30m%-10s\033[0m\n" $plugin_name $enabled + fi + done +} + +_list_themes() { + for theme ($ZSH/themes/*zsh-theme $ZSH_CUSTOM/*zsh-theme(N)); do + local theme_name=$(basename $theme | awk -F '.' '{print $1}') + local selected_theme=$(_map_get themes theme) + if [[ $selected_theme = $theme_name ]]; then + printf "%-30s \033[0;32m%-10s\033[0m\n" $theme_name enabled + else + printf "%-30s \033[0;30m%-10s\033[0m\n" $theme_name disabled + fi + done +} + +_list_pre_enabled_enabled_plugins() { + for plugin ($(_map_keys plugins)); do + local enabled=$(_map_get plugins $plugin) + if [[ $enabled = "enabled" ]]; then + printf "%s " $plugin + fi + done +} + +_list_enabled_plugins() { + for plugin ($(_map_keys plugins)); do + local enabled=$(_map_get plugins $plugin) + if [[ $enabled = "pre_enabled" || $enabled = "enabled" ]]; then + printf "%-30s \033[0;32m%-10s\033[0m\n" $plugin $enabled + fi + done +} + +_enable_plugin() { + [[ -z "$1" ]] && return 1 + local plugin=$1 + _map_exists plugins $plugin + if [[ $? -ne 0 ]]; then + _map_put plugins $plugin enabled + fi +} + +_enable_theme() { + [[ -z "$1" ]] && return 1 + local theme=$1 + _map_put themes theme $theme +} + +_disable_plugin() { + [[ -z "$1" ]] && return 1 + local plugin=$1 + local enabled=$(_map_get plugins $plugin) + [[ $enabled = "enabled" ]] && _map_remove plugins $plugin +} + +_populate_enabled_plugins() { + for plugin ($@); do + if [[ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]]; then + fpath=($ZSH/plugins/$plugin $fpath) + source $ZSH/plugins/$plugin/$plugin.plugin.zsh + elif [[ -f $ZSH/plugins/$plugin/_$plugin ]]; then + fpath=($ZSH/plugins/$plugin $fpath) + elif [[ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]]; then + fpath=($ZSH_CUSTOM/plugins/$plugin $fpath) + source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh + elif [[ -f $ZSH_CUSTOM/plugins/$plugin/_$plugin ]]; then + fpath=($ZSH_CUSTOM/plugins/$plugin $fpath) + fi + done +} + +_populate_enabled_theme() { + ZSH_THEME=$(_map_get themes theme) + # Reload theme (copied from oh-my-zsh.sh) + if [ -f "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" ]; then + source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme" + elif [ -f "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" ]; then + source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme" + else + source "$ZSH/themes/$ZSH_THEME.zsh-theme" + fi +} + +_update_plugin() { + [[ -z "$1" ]] && return 1 + local plugin=$1 + if [[ -d $ZSH_CUSTOM/plugins/$plugin ]]; then + command pushd $ZSH_CUSTOM/plugins/$plugin > /dev/null + command git pull + command popd > /dev/null + fi +} + +_update_theme() { + [[ -z "$1" ]] && return 1 + local theme=$1 + if [[ -d $ZSH_CUSTOM/themes/$theme ]]; then + command pushd $ZSH_CUSTOM/themes/$theme > /dev/null + command git pull + command popd > /dev/null + fi +} + +# initialize repository +_pre_enable_plugins +_populate_enabled_plugins +_init_theme +_populate_enabled_theme $(_list_pre_enabled_enabled_plugins) diff --git a/plugins/omz-bootstrap/omz-bootstrap.plugin.zsh b/plugins/omz-bootstrap/omz-bootstrap.plugin.zsh new file mode 100644 index 000000000..7b623e0e8 --- /dev/null +++ b/plugins/omz-bootstrap/omz-bootstrap.plugin.zsh @@ -0,0 +1,83 @@ +DIR="$(dirname "$0")" +source "$DIR/lib/map.zsh" +source "$DIR/lib/repository.zsh" +source "$DIR/lib/download.zsh" +unset DIR + + +usage='Usage: omz { plugin | theme } []' +usage_p='Usage: omz plugin [] +Commands: + ls list available plugins + on enable a plugin + off disable a plugin + up update a plugin + get download and enable a plugin' +usage_t='Usage: omz theme [] +Commands: + ls list available themes + set set a theme + up update a theme + get download and enable a theme' + +omz () { + case "$1" in + (plugin) + case "$2" in + (ls) + _list_plugins | less + ;; + (on) + _enable_plugin "$3" && _populate_enabled_plugins "$3" + ;; + (off) + _disable_plugin "$3" + ;; + (up) + _update_plugin "$3" + ;; + (get) + _download_plugin "$3" "$4" && _populate_enabled_plugins "$4" + ;; + (-h|--help|help) + echo $usage_p + ;; + (*) + echo $usage_p + return 10 + ;; + esac + ;; + (theme) + case "$2" in + (ls) + _list_themes | less + ;; + (set) + _enable_theme "$3" && _populate_enabled_theme + ;; + (up) + _update_theme "$3" + ;; + (get) + _download_theme "$3" "$4" && _populate_enabled_theme + ;; + (-h|--help|help) + echo $usage_t + ;; + (*) + echo $usage_t + return 20 + ;; + esac + ;; + (-h|--help|help) + echo $usage + ;; + (*) + echo $usage + return 1 + ;; + esac +} +