diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2020-10-21 16:57:03 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2020-10-21 16:57:03 -0600 |
commit | 058885f5263f29f046c96ea2ecf55e6dca3ed321 (patch) | |
tree | 5aca868fa5f9d16f39baa0c355f6056b5c8ea4da /lib | |
parent | 1774c426de3c4845e2d606c813c37067b8cf78d7 (diff) | |
parent | 3b1699b59527ee8095397b9909a37d55689a0481 (diff) | |
download | zsh-058885f5263f29f046c96ea2ecf55e6dca3ed321.tar.gz zsh-058885f5263f29f046c96ea2ecf55e6dca3ed321.tar.bz2 zsh-058885f5263f29f046c96ea2ecf55e6dca3ed321.zip |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cli.zsh | 185 | ||||
-rw-r--r-- | lib/completion.zsh | 2 | ||||
-rw-r--r-- | lib/git.zsh | 124 | ||||
-rw-r--r-- | lib/history.zsh | 1 | ||||
-rw-r--r-- | lib/nvm.zsh | 9 | ||||
-rw-r--r-- | lib/termsupport.zsh | 2 |
6 files changed, 263 insertions, 60 deletions
diff --git a/lib/cli.zsh b/lib/cli.zsh index c1ae2bdf2..b1478a89f 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,41 @@ Usage: omz <command> [options] Available commands: help Print this help message + plugin <command> Manage plugins + pr <command> Manage Oh My Zsh Pull Requests + theme <command> Manage themes update Update Oh My Zsh - pr <command> 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 +103,57 @@ 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 <<EOF +Usage: omz plugin <command> [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)) + + # 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 + fi + + if (( ${#builtin_plugins} )); then + (( ${#custom_plugins} )) && echo # add a line of separation + + print -P "%U%BBuilt-in plugins%b%u:" + print -l ${(q-)builtin_plugins} | column + fi +} + function _omz::pr { (( $# > 0 && $+functions[_omz::pr::$1] )) || { cat <<EOF @@ -107,6 +179,24 @@ function _omz::pr::clean { set -e builtin cd -q "$ZSH" + # Check if there are PR branches + local fmt branches + 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 + if [[ -z "$branches" ]]; then + _omz::log info "there are no Pull Request branches to remove." + return + fi + + # Print found PR branches + echo "$branches\n" + # Confirm before removing the branches + _omz::confirm "do you want remove these Pull Request branches? [Y/n] " + # Only proceed if the answer is a valid yes option + [[ "$REPLY" != [yY$'\n'] ]] && return + _omz::log info "removing all Oh My Zsh Pull Request branches..." command git branch --list 'ohmyzsh/pull-*' | while read branch; do command git branch -D "$branch" @@ -181,13 +271,9 @@ function _omz::pr::test { command zsh -l # After testing, go back to the previous HEAD if the user wants - _omz::log prompt "do you want to go back to the previous branch? [Y/n] " - read -r -k 1 - - # If no newline entered, add a newline - [[ "$REPLY" != $'\n' ]] && echo - # If NO selected, do nothing else - [[ "$REPLY" = [nN] ]] && return + _omz::confirm "do you want to go back to the previous branch? [Y/n] " + # Only proceed if the answer is a valid yes option + [[ "$REPLY" != [yY$'\n'] ]] && return ( set -e @@ -200,6 +286,69 @@ function _omz::pr::test { ) } +function _omz::theme { + (( $# > 0 && $+functions[_omz::theme::$1] )) || { + cat <<EOF +Usage: omz theme <command> [options] + +Available commands: + + list List all available Oh My Zsh themes + use <theme> 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)) + + # 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 + fi + + if (( ${#builtin_themes} )); then + (( ${#custom_themes} )) && echo # add a line of separation + + print -P "%U%BBuilt-in themes%b%u:" + print -l ${(q-)builtin_themes} | column + fi +} + +function _omz::theme::use { + if [[ -z "$1" ]]; then + echo >&2 "Usage: omz theme use <theme>" + 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" diff --git a/lib/completion.zsh b/lib/completion.zsh index a3873cd08..2b62785d5 100644 --- a/lib/completion.zsh +++ b/lib/completion.zsh @@ -1,7 +1,7 @@ # fixme - the load process here seems a bit bizarre zmodload -i zsh/complist -WORDCHARS='_-' +WORDCHARS='' unsetopt menu_complete # do not autoselect the first completion entry unsetopt flowcontrol diff --git a/lib/git.zsh b/lib/git.zsh index ffc7c01a1..53d39609e 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -147,44 +147,102 @@ function git_prompt_long_sha() { SHA=$(__git_prompt_git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER" } -# Get the status of the working tree function git_prompt_status() { - emulate -L zsh + [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return - local INDEX STATUS - INDEX=$(__git_prompt_git status --porcelain -b 2> /dev/null) || return 0 - STATUS="" - if [[ "${INDEX}" =~ $'(^|\n)\\?\\? ' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)(A |M |MM) ' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)([ AM]M| T) ' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)R ' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)([A ]D|D ) ' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS" - fi - if $(__git_prompt_git rev-parse --verify refs/stash >/dev/null 2>&1); then - STATUS="$ZSH_THEME_GIT_PROMPT_STASHED$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)UU ' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*ahead' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS" + # Maps a git status prefix to an internal constant + # This cannot use the prompt constants, as they may be empty + local -A prefix_constant_map + prefix_constant_map=( + '\?\? ' 'UNTRACKED' + 'A ' 'ADDED' + 'M ' 'ADDED' + 'MM ' 'ADDED' + ' M ' 'MODIFIED' + 'AM ' 'MODIFIED' + ' T ' 'MODIFIED' + 'R ' 'RENAMED' + ' D ' 'DELETED' + 'D ' 'DELETED' + 'UU ' 'UNMERGED' + 'ahead' 'AHEAD' + 'behind' 'BEHIND' + 'diverged' 'DIVERGED' + 'stashed' 'STASHED' + ) + + # Maps the internal constant to the prompt theme + local -A constant_prompt_map + constant_prompt_map=( + 'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED" + 'ADDED' "$ZSH_THEME_GIT_PROMPT_ADDED" + 'MODIFIED' "$ZSH_THEME_GIT_PROMPT_MODIFIED" + 'RENAMED' "$ZSH_THEME_GIT_PROMPT_RENAMED" + 'DELETED' "$ZSH_THEME_GIT_PROMPT_DELETED" + 'UNMERGED' "$ZSH_THEME_GIT_PROMPT_UNMERGED" + 'AHEAD' "$ZSH_THEME_GIT_PROMPT_AHEAD" + 'BEHIND' "$ZSH_THEME_GIT_PROMPT_BEHIND" + 'DIVERGED' "$ZSH_THEME_GIT_PROMPT_DIVERGED" + 'STASHED' "$ZSH_THEME_GIT_PROMPT_STASHED" + ) + + # The order that the prompt displays should be added to the prompt + local status_constants + status_constants=( + UNTRACKED ADDED MODIFIED RENAMED DELETED + STASHED UNMERGED AHEAD BEHIND DIVERGED + ) + + local status_text="$(__git_prompt_git status --porcelain -b 2> /dev/null)" + + # Don't continue on a catastrophic failure + if [[ $? -eq 128 ]]; then + return 1 fi - if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*behind' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS" + + # A lookup table of each git status encountered + local -A statuses_seen + + if __git_prompt_git rev-parse --verify refs/stash &>/dev/null; then + statuses_seen[STASHED]=1 fi - if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*diverged' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS" + + local status_lines + status_lines=("${(@f)${status_text}}") + + # If the tracking line exists, get and parse it + if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then + local branch_statuses + branch_statuses=("${(@s/,/)match}") + for branch_status in $branch_statuses; do + if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then + continue + fi + local last_parsed_status=$prefix_constant_map[$match[1]] + statuses_seen[$last_parsed_status]=$match[2] + done fi - echo $STATUS + + # For each status prefix, do a regex comparison + for status_prefix in ${(k)prefix_constant_map}; do + local status_constant="${prefix_constant_map[$status_prefix]}" + local status_regex=$'(^|\n)'"$status_prefix" + + if [[ "$status_text" =~ $status_regex ]]; then + statuses_seen[$status_constant]=1 + fi + done + + # Display the seen statuses in the order specified + local status_prompt + for status_constant in $status_constants; do + if (( ${+statuses_seen[$status_constant]} )); then + local next_display=$constant_prompt_map[$status_constant] + status_prompt="$next_display$status_prompt" + fi + done + + echo $status_prompt } # Outputs the name of the current user diff --git a/lib/history.zsh b/lib/history.zsh index 0ee8cfe7a..8d922a30b 100644 --- a/lib/history.zsh +++ b/lib/history.zsh @@ -36,4 +36,3 @@ setopt hist_expire_dups_first # delete duplicates first when HISTFILE size excee setopt hist_ignore_dups # ignore duplicated commands history list setopt hist_ignore_space # ignore commands that start with space setopt hist_verify # show command with history expansion to user before running it -setopt share_history # share command history data diff --git a/lib/nvm.zsh b/lib/nvm.zsh index 4a8b6811e..2fe57a8f4 100644 --- a/lib/nvm.zsh +++ b/lib/nvm.zsh @@ -1,9 +1,6 @@ -# get the node.js version +# get the nvm-controlled node.js version function nvm_prompt_info() { - [[ -f "$NVM_DIR/nvm.sh" ]] || return - local nvm_prompt - nvm_prompt=$(node -v 2>/dev/null) - [[ "${nvm_prompt}x" == "x" ]] && return - nvm_prompt=${nvm_prompt:1} + which nvm &>/dev/null || return + local nvm_prompt=${$(nvm current)#v} echo "${ZSH_THEME_NVM_PROMPT_PREFIX}${nvm_prompt}${ZSH_THEME_NVM_PROMPT_SUFFIX}" } diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 8cb2389e2..778f12bca 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -42,7 +42,7 @@ function title { } ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD -ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~" +ZSH_THEME_TERM_TITLE_IDLE="%n@%m:%~" # Avoid duplication of directory in terminals with independent dir display if [[ "$TERM_PROGRAM" == Apple_Terminal ]]; then ZSH_THEME_TERM_TITLE_IDLE="%n@%m" |