summaryrefslogtreecommitdiff
path: root/lib/cli.zsh
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2022-02-19 17:12:23 -0600
committerTuowen Zhao <ztuowen@gmail.com>2022-02-19 17:12:23 -0600
commitcae9a2b797649379e865e6bd73bc67e294e4ac77 (patch)
tree481419eff4bc761c3ca516704427394193473419 /lib/cli.zsh
parent49edbf438ed690c76e6b2af80368c59404cf0167 (diff)
parent3427da4057dbe302933a7b5b19b4e23bfb9d0969 (diff)
downloadzsh-cae9a2b797649379e865e6bd73bc67e294e4ac77.tar.gz
zsh-cae9a2b797649379e865e6bd73bc67e294e4ac77.tar.bz2
zsh-cae9a2b797649379e865e6bd73bc67e294e4ac77.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'lib/cli.zsh')
-rw-r--r--lib/cli.zsh94
1 files changed, 46 insertions, 48 deletions
diff --git a/lib/cli.zsh b/lib/cli.zsh
index 8cf8368e6..c2fba8556 100644
--- a/lib/cli.zsh
+++ b/lib/cli.zsh
@@ -37,7 +37,7 @@ function _omz {
elif (( CURRENT == 3 )); then
case "$words[2]" in
changelog) local -a refs
- refs=("${(@f)$(cd "$ZSH"; command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
+ refs=("${(@f)$(builtin cd -q "$ZSH"; command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
_describe 'command' refs ;;
plugin) subcmds=(
'disable:Disable plugin(s)'
@@ -61,7 +61,7 @@ function _omz {
# if command is "disable", only offer already enabled plugins
valid_plugins=($plugins)
else
- valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
+ valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
# if command is "enable", remove already enabled plugins
[[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
fi
@@ -69,11 +69,11 @@ function _omz {
_describe 'plugin' valid_plugins ;;
plugin::info)
local -aU plugins
- plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
+ plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
_describe 'plugin' plugins ;;
theme::(set|use))
local -aU themes
- themes=("$ZSH"/themes/*.zsh-theme(.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
+ themes=("$ZSH"/themes/*.zsh-theme(-.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(-.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
_describe 'theme' themes ;;
esac
elif (( CURRENT > 4 )); then
@@ -85,7 +85,7 @@ function _omz {
# if command is "disable", only offer already enabled plugins
valid_plugins=($plugins)
else
- valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
+ valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(-.N:h:t))
# if command is "enable", remove already enabled plugins
[[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
fi
@@ -105,7 +105,10 @@ function _omz {
return 0
}
-compdef _omz omz
+# If run from a script, do not set the completion function
+if (( ${+functions[compdef]} )); then
+ compdef _omz omz
+fi
## Utility functions
@@ -176,13 +179,13 @@ function _omz::changelog {
local version=${1:-HEAD} format=${3:-"--text"}
if (
- cd "$ZSH"
+ builtin cd -q "$ZSH"
! command git show-ref --verify refs/heads/$version && \
! command git show-ref --verify refs/tags/$version && \
! command git rev-parse --verify "${version}^{commit}"
) &>/dev/null; then
cat >&2 <<EOF
-Usage: omz changelog [version]
+Usage: ${(j: :)${(s.::.)0#_}} [version]
NOTE: <version> must be a valid branch, tag or commit.
EOF
@@ -193,9 +196,9 @@ EOF
}
function _omz::plugin {
- (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
+ (( $# > 0 && $+functions[$0::$1] )) || {
cat >&2 <<EOF
-Usage: omz plugin <command> [options]
+Usage: ${(j: :)${(s.::.)0#_}} <command> [options]
Available commands:
@@ -212,12 +215,12 @@ EOF
local command="$1"
shift
- _omz::plugin::$command "$@"
+ $0::$command "$@"
}
function _omz::plugin::disable {
if [[ -z "$1" ]]; then
- echo >&2 "Usage: omz plugin disable <plugin> [...]"
+ echo >&2 "Usage: ${(j: :)${(s.::.)0#_}} <plugin> [...]"
return 1
fi
@@ -289,7 +292,7 @@ multi == 1 && length(\$0) > 0 {
}
# Exit if the new .zshrc file has syntax errors
- if ! zsh -n "$zdot/.zshrc"; then
+ if ! command zsh -n "$zdot/.zshrc"; then
_omz::log error "broken syntax in '"${zdot/#$HOME/\~}/.zshrc"'. Rolling back changes..."
command mv -f "$zdot/.zshrc" "$zdot/.zshrc.new"
command mv -f "$zdot/.zshrc.bck" "$zdot/.zshrc"
@@ -299,15 +302,13 @@ multi == 1 && length(\$0) > 0 {
# Restart the zsh session if there were no errors
_omz::log info "plugins disabled: ${(j:, :)dis_plugins}."
- # Old zsh versions don't have ZSH_ARGZERO
- local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
- # Check whether to run a login shell
- [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
+ # Only reload zsh if run in an interactive session
+ [[ ! -o interactive ]] || _omz::reload
}
function _omz::plugin::enable {
if [[ -z "$1" ]]; then
- echo >&2 "Usage: omz plugin enable <plugin> [...]"
+ echo >&2 "Usage: ${(j: :)${(s.::.)0#_}} <plugin> [...]"
return 1
fi
@@ -365,7 +366,7 @@ multi == 1 && /^[^#]*\)/ {
}
# Exit if the new .zshrc file has syntax errors
- if ! zsh -n "$zdot/.zshrc"; then
+ if ! command zsh -n "$zdot/.zshrc"; then
_omz::log error "broken syntax in '"${zdot/#$HOME/\~}/.zshrc"'. Rolling back changes..."
command mv -f "$zdot/.zshrc" "$zdot/.zshrc.new"
command mv -f "$zdot/.zshrc.bck" "$zdot/.zshrc"
@@ -375,15 +376,13 @@ multi == 1 && /^[^#]*\)/ {
# Restart the zsh session if there were no errors
_omz::log info "plugins enabled: ${(j:, :)add_plugins}."
- # Old zsh versions don't have ZSH_ARGZERO
- local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
- # Check whether to run a login shell
- [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
+ # Only reload zsh if run in an interactive session
+ [[ ! -o interactive ]] || _omz::reload
}
function _omz::plugin::info {
if [[ -z "$1" ]]; then
- echo >&2 "Usage: omz plugin info <plugin>"
+ echo >&2 "Usage: ${(j: :)${(s.::.)0#_}} <plugin>"
return 1
fi
@@ -430,7 +429,7 @@ function _omz::plugin::list {
function _omz::plugin::load {
if [[ -z "$1" ]]; then
- echo >&2 "Usage: omz plugin load <plugin> [...]"
+ echo >&2 "Usage: ${(j: :)${(s.::.)0#_}} <plugin> [...]"
return 1
fi
@@ -477,9 +476,9 @@ function _omz::plugin::load {
}
function _omz::pr {
- (( $# > 0 && $+functions[_omz::pr::$1] )) || {
+ (( $# > 0 && $+functions[$0::$1] )) || {
cat >&2 <<EOF
-Usage: omz pr <command> [options]
+Usage: ${(j: :)${(s.::.)0#_}} <command> [options]
Available commands:
@@ -493,7 +492,7 @@ EOF
local command="$1"
shift
- _omz::pr::$command "$@"
+ $0::$command "$@"
}
function _omz::pr::clean {
@@ -534,7 +533,7 @@ function _omz::pr::test {
# Check the input
if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
- echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
+ echo >&2 "Usage: ${(j: :)${(s.::.)0#_}} <PR_NUMBER_or_URL>"
return 1
fi
@@ -574,7 +573,7 @@ function _omz::pr::test {
# Rebase pull request branch against the current master
_omz::log info "rebasing PR #$1..."
- command git rebase master ohmyzsh/pull-$1 || {
+ command git rebase --no-gpg-sign master ohmyzsh/pull-$1 || {
command git rebase --abort &>/dev/null
_omz::log warn "could not rebase PR #$1 on top of master."
_omz::log warn "you might not see the latest stable changes."
@@ -619,9 +618,9 @@ function _omz::reload {
}
function _omz::theme {
- (( $# > 0 && $+functions[_omz::theme::$1] )) || {
+ (( $# > 0 && $+functions[$0::$1] )) || {
cat >&2 <<EOF
-Usage: omz theme <command> [options]
+Usage: ${(j: :)${(s.::.)0#_}} <command> [options]
Available commands:
@@ -636,7 +635,7 @@ EOF
local command="$1"
shift
- _omz::theme::$command "$@"
+ $0::$command "$@"
}
function _omz::theme::list {
@@ -671,7 +670,7 @@ function _omz::theme::list {
function _omz::theme::set {
if [[ -z "$1" ]]; then
- echo >&2 "Usage: omz theme set <theme>"
+ echo >&2 "Usage: ${(j: :)${(s.::.)0#_}} <theme>"
return 1
fi
@@ -721,7 +720,7 @@ EOF
}
# Exit if the new .zshrc file has syntax errors
- if ! zsh -n "$zdot/.zshrc"; then
+ if ! command zsh -n "$zdot/.zshrc"; then
_omz::log error "broken syntax in '"${zdot/#$HOME/\~}/.zshrc"'. Rolling back changes..."
command mv -f "$zdot/.zshrc" "$zdot/.zshrc.new"
command mv -f "$zdot/.zshrc.bck" "$zdot/.zshrc"
@@ -731,15 +730,13 @@ EOF
# Restart the zsh session if there were no errors
_omz::log info "'$1' theme set correctly."
- # Old zsh versions don't have ZSH_ARGZERO
- local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
- # Check whether to run a login shell
- [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
+ # Only reload zsh if run in an interactive session
+ [[ ! -o interactive ]] || _omz::reload
}
function _omz::theme::use {
if [[ -z "$1" ]]; then
- echo >&2 "Usage: omz theme use <theme>"
+ echo >&2 "Usage: ${(j: :)${(s.::.)0#_}} <theme>"
return 1
fi
@@ -761,13 +758,13 @@ function _omz::theme::use {
}
function _omz::update {
- local last_commit=$(cd "$ZSH"; git rev-parse HEAD)
+ local last_commit=$(builtin cd -q "$ZSH"; git rev-parse HEAD)
# Run update script
if [[ "$1" != --unattended ]]; then
- ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive || return $?
+ ZSH="$ZSH" command zsh -f "$ZSH/tools/upgrade.sh" --interactive || return $?
else
- ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" || return $?
+ ZSH="$ZSH" command zsh -f "$ZSH/tools/upgrade.sh" || return $?
fi
# Update last updated file
@@ -777,7 +774,7 @@ function _omz::update {
command rm -rf "$ZSH/log/update.lock"
# Restart the zsh session if there were changes
- if [[ "$1" != --unattended && "$(cd "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then
+ if [[ "$1" != --unattended && "$(builtin cd -q "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then
# Old zsh versions don't have ZSH_ARGZERO
local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
# Check whether to run a login shell
@@ -787,16 +784,17 @@ function _omz::update {
function _omz::version {
(
- cd "$ZSH"
+ builtin cd -q "$ZSH"
# Get the version name:
# 1) try tag-like version
- # 2) try name-rev
- # 3) try branch name
+ # 2) try branch name
+ # 3) try name-rev (tag~<rev> or branch~<rev>)
local version
version=$(command git describe --tags HEAD 2>/dev/null) \
+ || version=$(command git symbolic-ref --quiet --short HEAD 2>/dev/null) \
|| version=$(command git name-rev --no-undefined --name-only --exclude="remotes/*" HEAD 2>/dev/null) \
- || version=$(command git symbolic-ref --quiet --short HEAD 2>/dev/null)
+ || version="<detached>"
# Get short hash for the current HEAD
local commit=$(command git rev-parse --short HEAD 2>/dev/null)