diff --git a/lib/cli.zsh b/lib/cli.zsh index c1ae2bdf2..0c0f3f623 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -23,16 +23,27 @@ function _omz { local -a cmds subcmds cmds=( 'help:Usage information' + 'plugin:Commands for Oh My Zsh plugins management' + 'pr:Commands for Oh My Zsh Pull Requests management' + 'theme:Commands for Oh My Zsh themes management' 'update:Update Oh My Zsh' - 'pr:Commands for Oh My Zsh Pull Requests' ) if (( CURRENT == 2 )); then _describe 'command' cmds elif (( CURRENT == 3 )); then case "$words[2]" in - pr) subcmds=( 'test:Test a Pull Request' 'clean:Delete all Pull Request branches' ) + plugin) subcmds=('list:List plugins') _describe 'command' subcmds ;; + pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches') + _describe 'command' subcmds ;; + theme) subcmds=('use:Load a theme' 'list:List themes') + _describe 'command' subcmds ;; + esac + elif (( CURRENT == 4 )); then + case "$words[2]::$words[3]" in + theme::use) compadd "$ZSH"/themes/*.zsh-theme(.N:t:r) \ + "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::) ;; esac fi @@ -49,23 +60,40 @@ Usage: omz [options] Available commands: help Print this help message + pr Commands for Oh My Zsh Pull Requests management + theme Commands for Oh My Zsh themes management update Update Oh My Zsh - pr Commands for Oh My Zsh Pull Requests EOF } +function _omz::confirm { + # If question supplied, ask it before reading the answer + # NOTE: uses the logname of the caller function + if [[ -n "$1" ]]; then + _omz::log prompt "$1" "${${functrace[1]#_}%:*}" + fi + + # Read one character + read -r -k 1 + + # If no newline entered, add a newline + if [[ "$REPLY" != $'\n' ]]; then + echo + fi +} + function _omz::log { # if promptsubst is set, a message with `` or $() # will be run even if quoted due to `print -P` setopt localoptions nopromptsubst # $1 = info|warn|error|debug - # $@ = text + # $2 = text + # $3 = (optional) name of the logger local logtype=$1 - local logname=${${functrace[1]#_}%:*} - shift + local logname=${3:-${${functrace[1]#_}%:*}} # Don't print anything if debug is not active if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then @@ -74,14 +102,52 @@ function _omz::log { # Choose coloring based on log type case "$logtype" in - prompt) print -Pn "%S%F{blue}$logname%f%s: $@" ;; - debug) print -P "%F{white}$logname%f: $@" ;; - info) print -P "%F{green}$logname%f: $@" ;; - warn) print -P "%S%F{yellow}$logname%f%s: $@" ;; - error) print -P "%S%F{red}$logname%f%s: $@" ;; + prompt) print -Pn "%S%F{blue}$logname%f%s: $2" ;; + debug) print -P "%F{white}$logname%f: $2" ;; + info) print -P "%F{green}$logname%f: $2" ;; + warn) print -P "%S%F{yellow}$logname%f%s: $2" ;; + error) print -P "%S%F{red}$logname%f%s: $2" ;; esac >&2 } +function _omz::plugin { + (( $# > 0 && $+functions[_omz::plugin::$1] )) || { + cat < [options] + +Available commands: + + list List all available Oh My Zsh plugins + +EOF + return 1 + } + + local command="$1" + shift + + _omz::plugin::$command "$@" +} + +function _omz::plugin::list { + local -a custom_plugins builtin_plugins + custom_plugins=("$ZSH_CUSTOM"/plugins/*(/N:t)) + builtin_plugins=("$ZSH"/plugins/*(/N:t)) + + (( ${#custom_plugins} )) && { + print -Pn "%U%BCustom plugins%b%u: " + print -l ${(q-)custom_plugins} + } | fmt -w $COLUMNS + + (( ${#builtin_plugins} )) && { + # add a line of separation + (( ${#custom_plugins} )) && echo + + print -Pn "%U%BBuilt-in plugins%b%u: " + print -l ${(q-)builtin_plugins} + } | fmt -w $COLUMNS +} + function _omz::pr { (( $# > 0 && $+functions[_omz::pr::$1] )) || { cat < 0 && $+functions[_omz::theme::$1] )) || { + cat < [options] + +Available commands: + + list List all available Oh My Zsh themes + use Load an Oh My Zsh theme + +EOF + return 1 + } + + local command="$1" + shift + + _omz::theme::$command "$@" +} + +function _omz::theme::list { + local -a custom_themes builtin_themes + custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::)) + builtin_themes=("$ZSH"/themes/*.zsh-theme(.N:t:r)) + + (( ${#custom_themes} )) && { + print -Pn "%U%BCustom themes%b%u: " + print -l ${(q-)custom_themes} + } | fmt -w $COLUMNS + + (( ${#builtin_themes} )) && { + # add a line of separation + (( ${#custom_themes} )) && echo + + print -Pn "%U%BBuilt-in themes%b%u: " + print -l ${(q-)builtin_themes} + } | fmt -w $COLUMNS +} + +function _omz::theme::use { + if [[ -z "$1" ]]; then + echo >&2 "Usage: omz theme use " + return 1 + fi + + # Respect compatibility with old lookup order + if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then + source "$ZSH_CUSTOM/$1.zsh-theme" + elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then + source "$ZSH_CUSTOM/themes/$1.zsh-theme" + elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then + source "$ZSH/themes/$1.zsh-theme" + else + _omz::log error "theme '$1' not found" + return 1 + fi +} + function _omz::update { # Run update script env ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"