From 3ab4973b867951b141137c18a5925b40f810c5f2 Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 11:39:24 -0700 Subject: [PATCH 1/4] aks: only define the colors once --- git-prompt-colors.sh | 78 +++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/git-prompt-colors.sh b/git-prompt-colors.sh index aa76a49..654fb1e 100644 --- a/git-prompt-colors.sh +++ b/git-prompt-colors.sh @@ -1,44 +1,40 @@ # These are the color definitions used by gitprompt.sh -declare -g GIT_PROMPT_PREFIX -declare -g GIT_PROMPT_SUFFIX -declare -g GIT_PROMPT_SEPARATOR -declare -g GIT_PROMPT_BRANCH -declare -g GIT_PROMPT_STAGED -declare -g GIT_PROMPT_CONFLICTS -declare -g GIT_PROMPT_CHANGED -declare -g GIT_PROMPT_REMOTE -declare -g GIT_PROMPT_UNTRACKED -declare -g GIT_PROMPT_STASHED -declare -g GIT_PROMPT_CLEAN - -local Time12a="\$(date +%H:%M)" -local PathShort="\w" +define_git_prompt_colors() { + + Time12a="\$(date +%H:%M)" + PathShort="\w" + + # These are the color definitions used by gitprompt.sh + GIT_PROMPT_PREFIX="[" # start of the git info string + GIT_PROMPT_SUFFIX="]" # the end of the git info string + GIT_PROMPT_SEPARATOR="|" # separates each item + + GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory + GIT_PROMPT_STAGED="${Red}●" # the number of staged files/directories + GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict + GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files + + GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind + GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs + GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir + GIT_PROMPT_CLEAN="${BoldGreen}✔" # a colored flag indicating a "clean" repo + + GIT_PROMPT_COMMAND_OK="${Green}✔ " # indicator if the last command returned with an exit code of 0 + GIT_PROMPT_COMMAND_FAIL="${Red}✘ " # indicator if the last command returned with an exit code of other than 0 + + GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}" + GIT_PROMPT_START_ROOT="${Yellow}${PathShort}${ResetColor}" + GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ " + GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # " + + # Please do not add colors to these symbols + GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin" + GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin" + GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found +} + +if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then + define_git_prompt_colors +fi -# These are the color definitions used by gitprompt.sh -GIT_PROMPT_PREFIX="[" # start of the git info string -GIT_PROMPT_SUFFIX="]" # the end of the git info string -GIT_PROMPT_SEPARATOR="|" # separates each item - -GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory -GIT_PROMPT_STAGED="${Red}●" # the number of staged files/directories -GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict -GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files - -GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind -GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs -GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir -GIT_PROMPT_CLEAN="${BoldGreen}✔" # a colored flag indicating a "clean" repo - -GIT_PROMPT_COMMAND_OK="${Green}✔ " # indicator if the last command returned with an exit code of 0 -GIT_PROMPT_COMMAND_FAIL="${Red}✘ " # indicator if the last command returned with an exit code of other than 0 - -GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}" -GIT_PROMPT_START_ROOT="${Yellow}${PathShort}${ResetColor}" -GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ " -GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # " - -# Please do not add colors to these symbols -GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin" -GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin" -GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found From 7bf6b96b7136fe5f439a6a543b0a9a62d1d27d5e Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 11:39:42 -0700 Subject: [PATCH 2/4] aks: only define the colors once --- prompt-colors.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/prompt-colors.sh b/prompt-colors.sh index 9249a7e..cacf7bb 100644 --- a/prompt-colors.sh +++ b/prompt-colors.sh @@ -76,7 +76,11 @@ define_color_names() { _def_color ResetColor 0 0 } -define_color_names + +# do the color definitions only once +if [[ ${#ColorNames[*]} = 0 || -z "$IntenseBlack" || -z "$ResetColor" ]]; then + define_color_names +fi # end of prompt-colors.sh # vim: set ai sw=2 From a9cd7f330ccae0dd1e654b85f4934daa1bd27225 Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 11:41:12 -0700 Subject: [PATCH 3/4] aks: optimzations; don't lookup the same files over and over --- gitprompt.sh | 68 +++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 1dab935..7242fdc 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -24,6 +24,7 @@ function git_prompt_dir() # gp_set_file_var ENVAR SOMEFILE # +# If ENVAR is set, check that it's value exists as a readable file. Otherwise, # Set ENVAR to the path to SOMEFILE, based on $HOME, $__GIT_PROMPT_DIR, and the # directory of the current script. The SOMEFILE can be prefixed with '.', or # not. @@ -33,7 +34,13 @@ function git_prompt_dir() function gp_set_file_var() { local envar="$1" local file="$2" - if eval "test -z \"\$$envar\"" ; then + if eval "[[ -n \"\$$envar\" && -r \"\$$envar\" ]]" ; then # is envar set to a readable file? + local basefile + eval "basefile=\"\`basename \\\"\$$envar\\\"\`\"" # assign basefile + if [[ "$basefile" = "$file" || "$basefile" = ".$file" ]]; then + return 0 + fi + else # envar is not set, or it's set to a different file than requested eval "$envar=" # set empty envar gp_maybe_set_envar_to_path "$envar" "$HOME/.$file" "$HOME/$file" "$HOME/lib/$file" && return 0 git_prompt_dir @@ -73,24 +80,18 @@ function git_prompt_config() # git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh if gp_set_file_var __PROMPT_COLORS_SH prompt-colors.sh ; then - if [[ -n "${__PROMPT_COLORS_SH}" ]]; then - source "${__PROMPT_COLORS_SH}" # outsource the color defs - else - echo 1>&2 "Cannot find prompt-colors.sh!" - fi + source "${__PROMPT_COLORS_SH}" # outsource the color defs + else + echo 1>&2 "Cannot find prompt-colors.sh!" fi # source the user's ~/.git-prompt-colors.sh file, or the one that should be # sitting in the same directory as this script if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then - - # if the envar is defined, source the file for custom colors - if [[ -n "${__GIT_PROMPT_COLORS_FILE}" ]]; then - source "${__GIT_PROMPT_COLORS_FILE}" - else - echo 1>&2 "Cannot find git-prompt-colors.sh!" - fi + source "${__GIT_PROMPT_COLORS_FILE}" + else + echo 1>&2 "Cannot find git-prompt-colors.sh!" fi # Do this only once to define PROMPT_START and PROMPT_END @@ -110,7 +111,7 @@ function git_prompt_config() PROMPT_START="$GIT_PROMPT_START_USER" fi else - PROMPT_START="${GIT_PROMPT_START}" + PROMPT_START="$GIT_PROMPT_START" fi if [[ -z "${GIT_PROMPT_END}" ]] ; then @@ -120,27 +121,27 @@ function git_prompt_config() PROMPT_END="$GIT_PROMPT_END_USER" fi else - PROMPT_END="${GIT_PROMPT_END}" + PROMPT_END="$GIT_PROMPT_END" fi fi # set GIT_PROMPT_LEADING_SPACE to 0 if you want to have no leading space in front of the GIT prompt - if [ "x${GIT_PROMPT_LEADING_SPACE}" == "x0" ]; then + if [[ "$GIT_PROMPT_LEADING_SPACE" = 0 ]]; then PROMPT_LEADING_SPACE="" else PROMPT_LEADING_SPACE=" " fi - if [ "x${GIT_PROMPT_ONLY_IN_REPO}" == "x1" ]; then - EMPTY_PROMPT=$OLD_GITPROMPT + if [[ "$GIT_PROMPT_ONLY_IN_REPO" = 1 ]]; then + EMPTY_PROMPT="$OLD_GITPROMPT" else - if [[ -n "${VIRTUAL_ENV}" ]]; then - EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${VIRTUAL_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" + local ps="$LAST_COMMAND_INDICATOR" + if [[ -n "$VIRTUAL_ENV" ]]; then + ps="$ps(${Blue}$(basename \"${VIRTUAL_ENV}\")${ResetColor}) " elif [[ -n "${CONDA_DEFAULT_ENV}" ]]; then - EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${CONDA_DEFAULT_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" - else - EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${PROMPT_END}" + ps="$ps(${Blue}$(basename \"${CONDA_DEFAULT_ENV}\")${ResetColor}) " fi + EMPTY_PROMPT="$ps${PROMPT_START}$($prompt_callback)${PROMPT_END}" fi # fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes @@ -150,6 +151,7 @@ function git_prompt_config() if ! gp_maybe_set_envar_to_path __GIT_STATUS_CMD "$__GIT_PROMPT_DIR/gitstatus.sh" "$__GIT_PROMPT_DIR/gitstatus.py" ; then echo 1>&2 "Cannot find gitstatus.sh or gitstatus.py!" fi + # __GIT_STATUS_CMD defined fi } @@ -163,21 +165,21 @@ function setGitPrompt() { local repo=`git rev-parse --show-toplevel 2> /dev/null` if [[ ! -e "${repo}" ]]; then - PS1="${EMPTY_PROMPT}" + PS1="$EMPTY_PROMPT" return fi local FETCH_REMOTE_STATUS=1 - if [[ "x${GIT_PROMPT_FETCH_REMOTE_STATUS}" == "x0" ]]; then + if [[ "$GIT_PROMPT_FETCH_REMOTE_STATUS" = 0 ]]; then FETCH_REMOTE_STATUS=0 fi - if [[ -e "${repo}/.bash-git-rc" ]]; then - source "${repo}/.bash-git-rc" + if [[ -e "$repo/.bash-git-rc" ]]; then + source "$repo/.bash-git-rc" fi - if [ "x${FETCH_REMOTE_STATUS}" == "x1" ]; then - checkUpstream + if [ "$FETCH_REMOTE_STATUS" = 1 ]; then + checkUpstream fi updatePrompt @@ -189,7 +191,7 @@ function checkUpstream() { local FETCH_HEAD="${repo}/.git/FETCH_HEAD" # Fech repo if local is stale for more than $GIT_FETCH_TIMEOUT minutes - if [[ ! -e "${FETCH_HEAD}" || -e `find "${FETCH_HEAD}" -mmin +${GIT_PROMPT_FETCH_TIMEOUT}` ]] + if [[ ! -e "$FETCH_HEAD" || -e `find "$FETCH_HEAD" -mmin +$GIT_PROMPT_FETCH_TIMEOUT` ]] then if [[ -n $(git remote show) ]]; then ( @@ -270,15 +272,15 @@ function updatePrompt() { PS1="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" if [[ -n "${VIRTUAL_ENV}" ]]; then - PS1="(${Blue}$(basename ${VIRTUAL_ENV})${ResetColor}) ${PS1}" + PS1="(${Blue}$(basename \"$VIRTUAL_ENV\")${ResetColor}) ${PS1}" fi if [[ -n "${CONDA_DEFAULT_ENV}" ]]; then - PS1="(${Blue}$(basename ${CONDA_DEFAULT_ENV})${ResetColor}) ${PS1}" + PS1="(${Blue}$(basename \"$CONDA_DEFAULT_ENV\")${ResetColor}) ${PS1}" fi else - PS1="${EMPTY_PROMPT}" + PS1="$EMPTY_PROMPT" fi } From a3994cd962d89b90d48cc6acef94de22a857ab07 Mon Sep 17 00:00:00 2001 From: Alan Stebbens Date: Thu, 28 Aug 2014 11:51:01 -0700 Subject: [PATCH 4/4] aks: aesthetic touchups --- gitprompt.sh | 60 ++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 7242fdc..5a1dd93 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -11,14 +11,14 @@ function git_prompt_dir() { # assume the gitstatus.py is in the same directory as this script # code thanks to http://stackoverflow.com/questions/59895 - if [ -z "${__GIT_PROMPT_DIR}" ]; then + if [ -z "$__GIT_PROMPT_DIR" ]; then local SOURCE="${BASH_SOURCE[0]}" - while [ -h "${SOURCE}" ]; do - local DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" - SOURCE="$(readlink "${SOURCE}")" - [[ $SOURCE != /* ]] && SOURCE="${DIR}/${SOURCE}" + while [ -h "$SOURCE" ]; do + local DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" done - __GIT_PROMPT_DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" + __GIT_PROMPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" fi } @@ -80,7 +80,7 @@ function git_prompt_config() # git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh if gp_set_file_var __PROMPT_COLORS_SH prompt-colors.sh ; then - source "${__PROMPT_COLORS_SH}" # outsource the color defs + source "$__PROMPT_COLORS_SH" # outsource the color defs else echo 1>&2 "Cannot find prompt-colors.sh!" fi @@ -89,7 +89,7 @@ function git_prompt_config() # sitting in the same directory as this script if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then - source "${__GIT_PROMPT_COLORS_FILE}" + source "$__GIT_PROMPT_COLORS_FILE" else echo 1>&2 "Cannot find git-prompt-colors.sh!" fi @@ -104,7 +104,7 @@ function git_prompt_config() # local Time12a="(\@))" local PathShort="\w" - if [[ -z "${GIT_PROMPT_START}" ]] ; then + if [[ -z "$GIT_PROMPT_START" ]] ; then if $_isroot; then PROMPT_START="$GIT_PROMPT_START_ROOT" else @@ -114,7 +114,7 @@ function git_prompt_config() PROMPT_START="$GIT_PROMPT_START" fi - if [[ -z "${GIT_PROMPT_END}" ]] ; then + if [[ -z "$GIT_PROMPT_END" ]] ; then if $_isroot; then PROMPT_END="$GIT_PROMPT_END_ROOT" else @@ -137,11 +137,11 @@ function git_prompt_config() else local ps="$LAST_COMMAND_INDICATOR" if [[ -n "$VIRTUAL_ENV" ]]; then - ps="$ps(${Blue}$(basename \"${VIRTUAL_ENV}\")${ResetColor}) " - elif [[ -n "${CONDA_DEFAULT_ENV}" ]]; then - ps="$ps(${Blue}$(basename \"${CONDA_DEFAULT_ENV}\")${ResetColor}) " + ps="$ps($Blue$(basename \"$VIRTUAL_ENV\")$ResetColor) " + elif [[ -n "$CONDA_DEFAULT_ENV" ]]; then + ps="$ps($Blue$(basename \"$CONDA_DEFAULT_ENV\")$ResetColor) " fi - EMPTY_PROMPT="$ps${PROMPT_START}$($prompt_callback)${PROMPT_END}" + EMPTY_PROMPT="$ps$PROMPT_START$($prompt_callback)$PROMPT_END" fi # fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes @@ -164,7 +164,7 @@ function setGitPrompt() { git_prompt_config local repo=`git rev-parse --show-toplevel 2> /dev/null` - if [[ ! -e "${repo}" ]]; then + if [[ ! -e "$repo" ]]; then PS1="$EMPTY_PROMPT" return fi @@ -189,7 +189,7 @@ function checkUpstream() { local GIT_PROMPT_FETCH_TIMEOUT git_prompt_config - local FETCH_HEAD="${repo}/.git/FETCH_HEAD" + local FETCH_HEAD="$repo/.git/FETCH_HEAD" # Fech repo if local is stale for more than $GIT_FETCH_TIMEOUT minutes if [[ ! -e "$FETCH_HEAD" || -e `find "$FETCH_HEAD" -mmin +$GIT_PROMPT_FETCH_TIMEOUT` ]] then @@ -215,7 +215,7 @@ function updatePrompt() { git_prompt_config local -a GitStatus - GitStatus=($("${__GIT_STATUS_CMD}" 2>/dev/null)) + GitStatus=($("$__GIT_STATUS_CMD" 2>/dev/null)) local GIT_BRANCH=${GitStatus[0]} local GIT_REMOTE=${GitStatus[1]} @@ -229,7 +229,7 @@ function updatePrompt() { local GIT_STASHED=${GitStatus[6]} local GIT_CLEAN=${GitStatus[7]} - if [[ -n "${GitStatus}" ]]; then + if [[ -n "$GitStatus" ]]; then local STATUS="${PROMPT_LEADING_SPACE}${GIT_PROMPT_PREFIX}${GIT_PROMPT_BRANCH}${GIT_BRANCH}${ResetColor}" # __add_status KIND VALEXPR INSERT @@ -238,26 +238,26 @@ function updatePrompt() { __chk_gitvar_status() { local v if [[ "x$2" == "x-n" ]] ; then - v="$2 \"\${GIT_$1}\"" + v="$2 \"\$GIT_$1\"" else - v="\${GIT_$1} $2" + v="\$GIT_$1 $2" fi if eval "test $v" ; then if [[ $# -lt 2 || "$3" != '-' ]]; then - __add_status "\${GIT_PROMPT_$1}\${GIT_$1}\${ResetColor}" + __add_status "\$GIT_PROMPT_$1\$GIT_$1\$ResetColor" else - __add_status "\${GIT_PROMPT_$1}\${ResetColor}" + __add_status "\$GIT_PROMPT_$1\$ResetColor" fi fi } __add_gitvar_status() { - __add_status "\${GIT_PROMPT_$1}\${GIT_$1}\${ResetColor}" + __add_status "\$GIT_PROMPT_$1\$GIT_$1\$ResetColor" } # __add_status SOMETEXT __add_status() { - eval "STATUS=\"${STATUS}$1\"" + eval "STATUS=\"$STATUS$1\"" } __chk_gitvar_status 'REMOTE' '-n' @@ -268,15 +268,15 @@ function updatePrompt() { __chk_gitvar_status 'UNTRACKED' '-ne 0' __chk_gitvar_status 'STASHED' '-ne 0' __chk_gitvar_status 'CLEAN' '-eq 1' - - __add_status "${ResetColor}$GIT_PROMPT_SUFFIX" + __add_status "$ResetColor$GIT_PROMPT_SUFFIX" - PS1="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" - if [[ -n "${VIRTUAL_ENV}" ]]; then - PS1="(${Blue}$(basename \"$VIRTUAL_ENV\")${ResetColor}) ${PS1}" + PS1="$LAST_COMMAND_INDICATOR$PROMPT_START$($prompt_callback)$STATUS$PROMPT_END" + if [[ -n "$VIRTUAL_ENV" ]]; then + PS1="($Blue$(basename \"$VIRTUAL_ENV\")$ResetColor) $PS1" fi - if [[ -n "${CONDA_DEFAULT_ENV}" ]]; then - PS1="(${Blue}$(basename \"$CONDA_DEFAULT_ENV\")${ResetColor}) ${PS1}" + if [[ -n "$CONDA_DEFAULT_ENV" ]]; then + PS1="($Blue$(basename \"$CONDA_DEFAULT_ENV\")$ResetColor) $PS1" fi else