From 1bb402190d53c118f47a0851f5714dd4faa52809 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 9 Oct 2020 13:55:56 +0200 Subject: lib: add plugin and theme subcommands and fix `omz pr clean` - Add plugin and theme subcommand Fixes #8961 - Add confirmation prompt to `omz pr clean` command - Correct behavior on invalid answers on confirmation prompts If an invalid option is supplied, don't proceed with the potentially destructive action. Co-authored-by: Rishabh Bohra --- lib/cli.zsh | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 156 insertions(+), 18 deletions(-) (limited to 'lib/cli.zsh') diff --git a/lib/cli.zsh b/lib/cli.zsh index c1ae2bdf2..0c0f3f623 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -23,17 +23,28 @@ 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 return 0 @@ -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" -- cgit v1.2.3-70-g09d2 From fc6c9ca4b40ee1c9ba7fb4b1c8862fb54a8cb1f6 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 9 Oct 2020 15:03:03 +0200 Subject: lib: fix formatting in `omz pr clean` CLI command --- lib/cli.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/cli.zsh') diff --git a/lib/cli.zsh b/lib/cli.zsh index 0c0f3f623..5d6c31aef 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -175,7 +175,7 @@ function _omz::pr::clean { # Check if there are PR branches local fmt branches - fmt="%(align:19,right)%(color:bold blue)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)" + fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)" branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")" # Exit if there are no PR branches -- cgit v1.2.3-70-g09d2 From 982d010be5fdb1cff471978634f8ff374deba9ba Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 10 Oct 2020 11:47:09 +0200 Subject: lib: fix fmt removing ESC characters in theme and plugin list CLI commands --- lib/cli.zsh | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'lib/cli.zsh') diff --git a/lib/cli.zsh b/lib/cli.zsh index 5d6c31aef..c9ee159bc 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -134,18 +134,20 @@ function _omz::plugin::list { 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 + { + (( ${#custom_plugins} )) && { + print -Pn "%U%BCustom plugins%b%u: " + print -l ${(q-)custom_plugins} + } + + (( ${#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 | sed -E $'s/\e?(\\[[0-9]*m)/\e\\1/g' # deal with fmt removing ESC } function _omz::pr { @@ -305,18 +307,20 @@ function _omz::theme::list { 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 + { + (( ${#custom_themes} )) && { + print -Pn "%U%BCustom themes%b%u: " + print -l ${(q-)custom_themes} + } - (( ${#builtin_themes} )) && { - # add a line of separation - (( ${#custom_themes} )) && echo + (( ${#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 + print -Pn "%U%BBuilt-in themes%b%u: " + print -l ${(q-)builtin_themes} + } + } | fmt -w $COLUMNS | sed -E $'s/\e?(\\[[0-9]*m)/\e\\1/g' # deal with fmt removing ESC } function _omz::theme::use { -- cgit v1.2.3-70-g09d2 From 75ae0e905fcb928555df122ec1ec29a8a546130d Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 10 Oct 2020 12:12:22 +0200 Subject: lib: fix `omz help` and reword --- lib/cli.zsh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/cli.zsh') diff --git a/lib/cli.zsh b/lib/cli.zsh index c9ee159bc..ff865093d 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -60,8 +60,9 @@ 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 + plugin Manage plugins + pr Manage Oh My Zsh Pull Requests + theme Manage themes update Update Oh My Zsh EOF -- cgit v1.2.3-70-g09d2 From 902954d1cc4438e98149d72c878a4fbac3b3d7c5 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sun, 18 Oct 2020 21:14:05 +0200 Subject: lib: use `column` to format plugin and theme list CLI commands --- lib/cli.zsh | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) (limited to 'lib/cli.zsh') diff --git a/lib/cli.zsh b/lib/cli.zsh index ff865093d..9421fd19a 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -135,20 +135,17 @@ function _omz::plugin::list { 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} - } + if (( ${#custom_plugins} )); then + print -P "%U%BCustom plugins%b%u:" + print -l ${(q-)custom_plugins} | column + fi - (( ${#builtin_plugins} )) && { - # add a line of separation - (( ${#custom_plugins} )) && echo + if (( ${#builtin_plugins} )); then + (( ${#custom_plugins} )) && echo # add a line of separation - print -Pn "%U%BBuilt-in plugins%b%u: " - print -l ${(q-)builtin_plugins} - } - } | fmt -w $COLUMNS | sed -E $'s/\e?(\\[[0-9]*m)/\e\\1/g' # deal with fmt removing ESC + print -P "%U%BBuilt-in plugins%b%u:" + print -l ${(q-)builtin_plugins} | column + fi } function _omz::pr { @@ -308,20 +305,17 @@ function _omz::theme::list { 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} - } + if (( ${#custom_themes} )); then + print -P "%U%BCustom themes%b%u:" + print -l ${(q-)custom_themes} | column + fi - (( ${#builtin_themes} )) && { - # add a line of separation - (( ${#custom_themes} )) && echo + if (( ${#builtin_themes} )); then + (( ${#custom_themes} )) && echo # add a line of separation - print -Pn "%U%BBuilt-in themes%b%u: " - print -l ${(q-)builtin_themes} - } - } | fmt -w $COLUMNS | sed -E $'s/\e?(\\[[0-9]*m)/\e\\1/g' # deal with fmt removing ESC + print -P "%U%BBuilt-in themes%b%u:" + print -l ${(q-)builtin_themes} | column + fi } function _omz::theme::use { -- cgit v1.2.3-70-g09d2 From 7525b1d533b927bc40e3cfb29e399e5f2e0828d4 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sun, 18 Oct 2020 21:25:03 +0200 Subject: lib: allow bare output in theme and plugins list command --- lib/cli.zsh | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/cli.zsh') diff --git a/lib/cli.zsh b/lib/cli.zsh index 9421fd19a..82a2a3985 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -135,6 +135,12 @@ function _omz::plugin::list { custom_plugins=("$ZSH_CUSTOM"/plugins/*(/N:t)) builtin_plugins=("$ZSH"/plugins/*(/N:t)) + # If the command is being piped, print all found line by line + if [[ ! -t 1 ]]; then + print -l ${(q-)custom_plugins} ${(q-)builtin_plugins} + return + fi + if (( ${#custom_plugins} )); then print -P "%U%BCustom plugins%b%u:" print -l ${(q-)custom_plugins} | column @@ -305,6 +311,12 @@ function _omz::theme::list { custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::)) builtin_themes=("$ZSH"/themes/*.zsh-theme(.N:t:r)) + # If the command is being piped, print all found line by line + if [[ ! -t 1 ]]; then + print -l ${(q-)custom_themes} ${(q-)builtin_themes} + return + fi + if (( ${#custom_themes} )); then print -P "%U%BCustom themes%b%u:" print -l ${(q-)custom_themes} | column -- cgit v1.2.3-70-g09d2 From f9f45ca222a0ec6fa867ab3733f15911d1b894e6 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 20 Oct 2020 15:57:11 +0200 Subject: lib: follow symlinked plugins in `plugin list` CLI command See https://github.com/ohmyzsh/ohmyzsh/issues/9087#issuecomment-712460275 --- lib/cli.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/cli.zsh') diff --git a/lib/cli.zsh b/lib/cli.zsh index 82a2a3985..b1478a89f 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -132,8 +132,8 @@ EOF 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=("$ZSH_CUSTOM"/plugins/*(-/N:t)) + builtin_plugins=("$ZSH"/plugins/*(-/N:t)) # If the command is being piped, print all found line by line if [[ ! -t 1 ]]; then -- cgit v1.2.3-70-g09d2