From 58ff4e1d2e6a81ea97a05b142c28a931a9924b70 Mon Sep 17 00:00:00 2001 From: Michele Bologna Date: Thu, 8 Oct 2020 19:20:47 +0200 Subject: lib: follow standards in window title (#9320) In Ubuntu and Debian, in scp, and in rsync the prompt is by default specified as in user@hostname:/path/to/directory while the previous title in ohmyzsh was user@hostname: /path/to/directory --- lib/termsupport.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') 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" -- cgit v1.2.3-70-g09d2 From 297238b739fd51f03ab9b3ffcc6c644daa4a1401 Mon Sep 17 00:00:00 2001 From: Michał Dębski Date: Fri, 11 Mar 2016 16:59:40 +0100 Subject: lib: hide git_prompt_status when hide-status is set Closes #4912 Closes #5137 Closes #8071 --- lib/git.zsh | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/git.zsh b/lib/git.zsh index ffc7c01a1..b3b25f802 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -151,6 +151,8 @@ function git_prompt_long_sha() { 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="" -- cgit v1.2.3-70-g09d2 From 45a954cb0f8b67e35529efbcdf60f0621d95f4a1 Mon Sep 17 00:00:00 2001 From: Billy Conn Date: Thu, 13 Oct 2016 22:31:44 -0500 Subject: git_prompt_status now uses hash lookups instead of multiple greps --- lib/git.zsh | 124 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 33 deletions(-) (limited to 'lib') diff --git a/lib/git.zsh b/lib/git.zsh index b3b25f802..578f319e4 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -147,46 +147,104 @@ 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" + local status_prompt="" + + # A lookup table of each git status encountered + local -A statuses_seen + + # 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=( + '?? ' '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=( + '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=(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 $(__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" + statuses_seen['STASHED']=1 fi - if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*ahead' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*behind' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS" - fi - if [[ "${INDEX}" =~ $'(^|\n)## [^ ]\+ .*diverged' ]]; then - STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS" + + local status_lines=("${(@f)${status_text}}"); + + # If the tracking line exists, get and parse it + if [[ $status_lines[1] =~ "^## [^ ]+ \[(.*)\]" ]]; then + local 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 + shift status_lines fi - echo $STATUS + + # This not only gives us a status lookup, but the count of each type + for status_line in ${status_lines}; do + local status_prefix=${status_line[1, 3]} + local status_constant=${(v)prefix_constant_map[$status_prefix]} + + if [[ -z $status_constant ]]; then + continue + fi + + (( statuses_seen[$status_constant]++ )) + done + + # At this point, the statuses_seen hash contains: + # - Tracking => The difference between tracked and current + # - Modifications => The count of that type of modification + # - Stash => Whether or not a stash exists + # Might be useful for someone? + + for status_constant in $status_constants; do + if [[ ${+statuses_seen[$status_constant]} -eq 1 ]]; 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 -- cgit v1.2.3-70-g09d2 From 865f6572d586e7ba1231333cadf808002688603e Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 14 Apr 2018 23:37:45 +0200 Subject: Fix performance drop of iterating over lines and other stuff - Fix 'STASHED' key and unsupported syntax in 5.0.2 - Optimise `if` statement to make it more idiomatic. --- lib/git.zsh | 62 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 32 deletions(-) (limited to 'lib') diff --git a/lib/git.zsh b/lib/git.zsh index 578f319e4..f9b27949e 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -150,15 +150,11 @@ function git_prompt_long_sha() { function git_prompt_status() { [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return - local status_prompt="" - - # A lookup table of each git status encountered - local -A statuses_seen - # 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=( - '?? ' 'UNTRACKED' + local -A prefix_constant_map + prefix_constant_map=( + '\?\? ' 'UNTRACKED' 'A ' 'ADDED' 'M ' 'ADDED' 'MM ' 'ADDED' @@ -176,7 +172,8 @@ function git_prompt_status() { ) # Maps the internal constant to the prompt theme - local -A constant_prompt_map=( + 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" @@ -190,25 +187,33 @@ function git_prompt_status() { ) # The order that the prompt displays should be added to the prompt - local status_constants=(UNTRACKED ADDED MODIFIED RENAMED DELETED STASHED - UNMERGED AHEAD BEHIND DIVERGED) + 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) + 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 $(__git_prompt_git rev-parse --verify refs/stash >/dev/null 2>&1); then - statuses_seen['STASHED']=1 + # 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 - local status_lines=("${(@f)${status_text}}"); + 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=("${(@s/,/)match}") + 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 @@ -216,29 +221,22 @@ function git_prompt_status() { local last_parsed_status=$prefix_constant_map[$match[1]] statuses_seen[$last_parsed_status]=$match[2] done - shift status_lines fi - # This not only gives us a status lookup, but the count of each type - for status_line in ${status_lines}; do - local status_prefix=${status_line[1, 3]} - local status_constant=${(v)prefix_constant_map[$status_prefix]} + # 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 [[ -z $status_constant ]]; then - continue + if [[ "$status_text" =~ $status_regex ]]; then + statuses_seen[$status_constant]=1 fi - - (( statuses_seen[$status_constant]++ )) done - # At this point, the statuses_seen hash contains: - # - Tracking => The difference between tracked and current - # - Modifications => The count of that type of modification - # - Stash => Whether or not a stash exists - # Might be useful for someone? - + # Display the seen statuses in the order specified + local status_prompt for status_constant in $status_constants; do - if [[ ${+statuses_seen[$status_constant]} -eq 1 ]]; then + if (( ${+statuses_seen[$status_constant]} )); then local next_display=$constant_prompt_map[$status_constant] status_prompt="$next_display$status_prompt" fi -- cgit v1.2.3-70-g09d2 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') 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') 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 ef44416df2e2ae819b13764dbf6ca87ce099ec36 Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Thu, 10 Sep 2015 04:10:18 -0400 Subject: nvm: use `nvm current` in nvm_prompt_info and look in alternate install locations This makes it work regardless of where nvm is loaded from. And it uses nvm's version strings, which distinguish the "system" and "none" NVM environments, instead of reporting the specific version of the system node.js or erroring, respectively. Fixes #4336 Closes #4338 --- lib/nvm.zsh | 9 +++---- plugins/nvm/nvm.plugin.zsh | 62 +++++++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 25 deletions(-) (limited to 'lib') diff --git a/lib/nvm.zsh b/lib/nvm.zsh index 4a8b6811e..c4f70c849 100644 --- a/lib/nvm.zsh +++ b/lib/nvm.zsh @@ -1,9 +1,8 @@ -# 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 + nvm_prompt=$(nvm current) + nvm_prompt=${nvm_prompt#v} echo "${ZSH_THEME_NVM_PROMPT_PREFIX}${nvm_prompt}${ZSH_THEME_NVM_PROMPT_SUFFIX}" } diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh index 2264a2420..ee8d2324b 100644 --- a/plugins/nvm/nvm.plugin.zsh +++ b/plugins/nvm/nvm.plugin.zsh @@ -1,23 +1,45 @@ -# Set NVM_DIR if it isn't already defined -[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm" +# nvm +# +# This plugin locates and loads nvm, looking for it in well-known locations. -# Don't try to load nvm if command already available -type "nvm" &> /dev/null && return +() { + emulate -L zsh + local nvm_install_dir="" dir install_locations + if [[ -n $NVM_INSTALL_DIR ]]; then + # User-specified path + nvm_install_dir=$NVM_INSTALL_DIR + else + # Well-known common installation locations for NVM + install_locations=( ~/.nvm ) + [[ -n $NVM_DIR ]] && install_locations=($NVM_DIR $install_locations) + # Mac Homebrew sticks + which brew &>/dev/null && install_locations+=$(brew --prefix nvm) + for dir ($install_locations); do + if [[ -s $dir/nvm.sh ]]; then + nvm_install_dir=$dir + break + fi + done + fi -# Load nvm if it exists in $NVM_DIR -if [[ -f "$NVM_DIR/nvm.sh" ]]; then - source "$NVM_DIR/nvm.sh" - return -fi + if [[ -n $nvm_install_dir ]]; then + source $nvm_install_dir/nvm.sh + else + # No NVM installation found + return 0 + fi -# Otherwise try to load nvm installed via Homebrew - -# User can set this if they have an unusual Homebrew setup -NVM_HOMEBREW="${NVM_HOMEBREW:-/usr/local/opt/nvm}" -# Load nvm from Homebrew location if it exists -[[ -f "$NVM_HOMEBREW/nvm.sh" ]] && source "$NVM_HOMEBREW/nvm.sh" -# Load nvm bash completion from Homebrew if it exists -if [[ -f "$NVM_HOMEBREW/etc/bash_completion.d/nvm" ]]; then - autoload -U +X bashcompinit && bashcompinit - source "$NVM_HOMEBREW/etc/bash_completion.d/nvm" -fi + # Locate and use the completion file shipped with NVM, instead of this + # plugin's completion + # (Their bash completion file has zsh portability support) + if [[ $ZSH_NVM_BUNDLED_COMPLETION == true ]]; then + local bash_comp_file + # Homebrew relocates the bash completion file, so look multiple places + for bash_comp_file ( bash_completion etc/bash_completion.d/nvm ); do + if [[ -s $nvm_install_dir/$bash_comp_file ]]; then + source $nvm_install_dir/$bash_comp_file + break; + fi + done + fi +} -- cgit v1.2.3-70-g09d2 From d8cb67023540c1e2e7e4e211e2f7c9fc2d4e0c3c Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 9 Oct 2020 16:12:03 +0200 Subject: nvm: simplify nvm.sh and bash completion loading --- lib/nvm.zsh | 4 +-- plugins/nvm/nvm.plugin.zsh | 67 ++++++++++++++++++---------------------------- 2 files changed, 27 insertions(+), 44 deletions(-) (limited to 'lib') diff --git a/lib/nvm.zsh b/lib/nvm.zsh index c4f70c849..2fe57a8f4 100644 --- a/lib/nvm.zsh +++ b/lib/nvm.zsh @@ -1,8 +1,6 @@ # get the nvm-controlled node.js version function nvm_prompt_info() { - local nvm_prompt which nvm &>/dev/null || return - nvm_prompt=$(nvm current) - nvm_prompt=${nvm_prompt#v} + local nvm_prompt=${$(nvm current)#v} echo "${ZSH_THEME_NVM_PROMPT_PREFIX}${nvm_prompt}${ZSH_THEME_NVM_PROMPT_SUFFIX}" } diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh index ee8d2324b..2c137894b 100644 --- a/plugins/nvm/nvm.plugin.zsh +++ b/plugins/nvm/nvm.plugin.zsh @@ -1,45 +1,30 @@ -# nvm -# -# This plugin locates and loads nvm, looking for it in well-known locations. +# Set NVM_DIR if it isn't already defined +[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm" -() { - emulate -L zsh - local nvm_install_dir="" dir install_locations - if [[ -n $NVM_INSTALL_DIR ]]; then - # User-specified path - nvm_install_dir=$NVM_INSTALL_DIR - else - # Well-known common installation locations for NVM - install_locations=( ~/.nvm ) - [[ -n $NVM_DIR ]] && install_locations=($NVM_DIR $install_locations) - # Mac Homebrew sticks - which brew &>/dev/null && install_locations+=$(brew --prefix nvm) - for dir ($install_locations); do - if [[ -s $dir/nvm.sh ]]; then - nvm_install_dir=$dir - break - fi - done - fi +# Don't try to load nvm if command already available +which nvm &> /dev/null && return - if [[ -n $nvm_install_dir ]]; then - source $nvm_install_dir/nvm.sh - else - # No NVM installation found - return 0 - fi +if [[ -f "$NVM_DIR/nvm.sh" ]]; then + # Load nvm if it exists in $NVM_DIR + source "$NVM_DIR/nvm.sh" +else + # Otherwise try to load nvm installed via Homebrew + # User can set this if they have an unusual Homebrew setup + NVM_HOMEBREW="${NVM_HOMEBREW:-/usr/local/opt/nvm}" + # Load nvm from Homebrew location if it exists + [[ -f "$NVM_HOMEBREW/nvm.sh" ]] && source "$NVM_HOMEBREW/nvm.sh" +fi - # Locate and use the completion file shipped with NVM, instead of this - # plugin's completion - # (Their bash completion file has zsh portability support) - if [[ $ZSH_NVM_BUNDLED_COMPLETION == true ]]; then - local bash_comp_file - # Homebrew relocates the bash completion file, so look multiple places - for bash_comp_file ( bash_completion etc/bash_completion.d/nvm ); do - if [[ -s $nvm_install_dir/$bash_comp_file ]]; then - source $nvm_install_dir/$bash_comp_file - break; - fi - done +# Load nvm bash completion +for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do + if [[ -f "$nvm_completion" ]]; then + # Load bashcompinit + autoload -U +X bashcompinit && bashcompinit + # Bypass compinit call in nvm bash completion script. See: + # https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93 + ZSH_VERSION= source "$nvm_completion" + break fi -} +done + +unset NVM_HOMEBREW nvm_completion -- 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') 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') 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 23760228908d14a4644718869d5ebfb7b0dde6a7 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 10 Oct 2020 12:38:17 +0200 Subject: lib: remove share_history Related: #2537, #9324 --- lib/history.zsh | 1 - 1 file changed, 1 deletion(-) (limited to 'lib') 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 -- cgit v1.2.3-70-g09d2 From 6e7b861675c4568931e91255abc62a7c45660e28 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 10 Oct 2020 19:14:30 +0200 Subject: lib: fix regex bug in git_prompt_status Fixes #9326 --- lib/git.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/git.zsh b/lib/git.zsh index f9b27949e..53d39609e 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -226,7 +226,7 @@ function git_prompt_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" + local status_regex=$'(^|\n)'"$status_prefix" if [[ "$status_text" =~ $status_regex ]]; then statuses_seen[$status_constant]=1 -- 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') 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') 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') 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 From 3f42700c0d65cf26470d1e165b8ed80dfff9efca Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Wed, 21 Oct 2020 16:01:36 +0200 Subject: Revert "lib: treat _ and - as part of a word" This reverts commit 50dc4ab3574f4e265dff816d8d9a0195cd260152. The community has spoken: - https://github.com/ohmyzsh/ohmyzsh/issues/9367 - https://github.com/ohmyzsh/ohmyzsh/issues/8743 Fixes #9367 --- lib/completion.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') 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 -- cgit v1.2.3-70-g09d2