From 554f14bf235ab7ce10392ffd4ec3e1513143037d Mon Sep 17 00:00:00 2001 From: oGre Date: Fri, 11 Sep 2015 14:00:09 +0200 Subject: [PATCH 1/5] Re-indented functions. Added unset of prompt_callback when calling reset prompt. Call reset prompt when loading new theme. Added quicker check for function type. --- gitprompt.sh | 97 +++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 98c7696..ed60902 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -1,14 +1,13 @@ #!/bin/sh -function async_run() +function async_run() { { eval "$@" &> /dev/null }& } -function git_prompt_dir() -{ +function git_prompt_dir() { # assume the gitstatus.sh is in the same directory as this script # code thanks to http://stackoverflow.com/questions/59895 if [ -z "$__GIT_PROMPT_DIR" ]; then @@ -23,11 +22,10 @@ function git_prompt_dir() } function echoc() { - echo -e "${1}$2${ResetColor}" | sed 's/\\\]//g' | sed 's/\\\[//g' + echo -e "${1}$2${ResetColor}" | sed 's/\\\]//g' | sed 's/\\\[//g' } -function get_theme() -{ +function get_theme() { local CUSTOM_THEME_FILE="${HOME}/.git-prompt-colors.sh" local DEFAULT_THEME_FILE="${__GIT_PROMPT_DIR}/themes/Default.bgptheme" @@ -67,16 +65,15 @@ function get_theme() fi } -function git_prompt_load_theme() -{ +function git_prompt_load_theme() { + git_prompt_reset get_theme local DEFAULT_THEME_FILE="${__GIT_PROMPT_DIR}/themes/Default.bgptheme" source "${DEFAULT_THEME_FILE}" source "${__GIT_PROMPT_THEME_FILE}" } -function git_prompt_list_themes() -{ +function git_prompt_list_themes() { local oldTheme local oldThemeFile @@ -160,7 +157,7 @@ function gp_set_file_var() { # return 0 (true) if any FILEPATH is readable, set ENVAR to it # return 1 (false) if not -function gp_maybe_set_envar_to_path(){ +function gp_maybe_set_envar_to_path() { local envar="$1" shift local file @@ -183,6 +180,7 @@ git_prompt_reset() { for var in GIT_PROMPT_DIR __GIT_PROMPT_COLORS_FILE __PROMPT_COLORS_FILE __GIT_STATUS_CMD GIT_PROMPT_THEME_NAME; do unset $var done + unset -f prompt_callback } # gp_format_exit_status RETVAL @@ -203,8 +201,7 @@ gp_format_exit_status() { fi } -function git_prompt_config() -{ +function git_prompt_config() { #Checking if root to change output _isroot=false [[ $UID -eq 0 ]] && _isroot=true @@ -225,10 +222,10 @@ function git_prompt_config() git_prompt_load_theme - if [ "`type -t prompt_callback`" = 'function' ]; then - prompt_callback="prompt_callback" + if is_function prompt_callback; then + prompt_callback="prompt_callback" else - prompt_callback="prompt_callback_default" + prompt_callback="prompt_callback_default" fi if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ]; then @@ -369,42 +366,42 @@ function setGitPrompt() { _have_find_mmin=1 function olderThanMinutes() { - local matches - local find_exit_code - - if [[ -z "$_find_command" ]]; then - if command -v gfind > /dev/null; then - _find_command=gfind - else - _find_command=find - fi - fi + local matches + local find_exit_code - if [[ "$_have_find_mmin" = 1 ]]; then - matches=`"$_find_command" "$1" -mmin +"$2" 2> /dev/null` - find_exit_code="$?" - if [[ -n "$matches" ]]; then - return 0 - else - if [[ "$find_exit_code" != 0 ]]; then - _have_find_mmin=0 - else - return 1 - fi - fi + if [[ -z "$_find_command" ]]; then + if command -v gfind > /dev/null; then + _find_command=gfind + else + _find_command=find fi + fi - # try perl, solaris ships with perl - if command -v perl > /dev/null; then - perl -e '((time - (stat("'"$1"'"))[9]) / 60) > '"$2"' && exit(0) || exit(1)' - return "$?" + if [[ "$_have_find_mmin" = 1 ]]; then + matches=`"$_find_command" "$1" -mmin +"$2" 2> /dev/null` + find_exit_code="$?" + if [[ -n "$matches" ]]; then + return 0 else - echo >&2 - echo "WARNING: neither a find that supports -mmin (such as GNU find) or perl is available, disabling remote status checking. Install GNU find as gfind or perl to enable this feature, or set GIT_PROMPT_FETCH_REMOTE_STATUS=0 to disable this warning." >&2 - echo >&2 - GIT_PROMPT_FETCH_REMOTE_STATUS=0 + if [[ "$find_exit_code" != 0 ]]; then + _have_find_mmin=0 + else return 1 + fi fi + fi + + # try perl, solaris ships with perl + if command -v perl > /dev/null; then + perl -e '((time - (stat("'"$1"'"))[9]) / 60) > '"$2"' && exit(0) || exit(1)' + return "$?" + else + echo >&2 + echo "WARNING: neither a find that supports -mmin (such as GNU find) or perl is available, disabling remote status checking. Install GNU find as gfind or perl to enable this feature, or set GIT_PROMPT_FETCH_REMOTE_STATUS=0 to disable this warning." >&2 + echo >&2 + GIT_PROMPT_FETCH_REMOTE_STATUS=0 + return 1 + fi } function checkUpstream() { @@ -524,8 +521,14 @@ function updatePrompt() { PS1="${NEW_PROMPT//_LAST_COMMAND_INDICATOR_/${LAST_COMMAND_INDICATOR}}" } +# Use exit status from declare command to determine whether input argument is a +# bash function +function is_function { + declare -Ff "$1" >/dev/null; +} + function prompt_callback_default { - return + return } function gp_install_prompt { From ffe831d659c0c61ba8a797640e99b3fb8bb00bd1 Mon Sep 17 00:00:00 2001 From: oGre Date: Sat, 12 Sep 2015 17:48:43 +0200 Subject: [PATCH 2/5] Missed two functions --- gitprompt.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index ed60902..e02fb68 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -1,7 +1,6 @@ #!/bin/sh -function async_run() -{ +function async_run() { { eval "$@" &> /dev/null }& @@ -421,8 +420,7 @@ function checkUpstream() { fi } -function replaceSymbols() -{ +function replaceSymbols() { if [[ -z ${GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING} ]]; then GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING=L fi From df40c38f265388f1be0913d22fd744a51e648551 Mon Sep 17 00:00:00 2001 From: oGre Date: Sat, 12 Sep 2015 21:08:15 +0200 Subject: [PATCH 3/5] re-indented replaceSymbols function containing tab stops --- gitprompt.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index e02fb68..97b12d0 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -425,11 +425,11 @@ function replaceSymbols() { GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING=L fi - local VALUE=${1//_AHEAD_/${GIT_PROMPT_SYMBOLS_AHEAD}} - local VALUE1=${VALUE//_BEHIND_/${GIT_PROMPT_SYMBOLS_BEHIND}} + local VALUE=${1//_AHEAD_/${GIT_PROMPT_SYMBOLS_AHEAD}} + local VALUE1=${VALUE//_BEHIND_/${GIT_PROMPT_SYMBOLS_BEHIND}} local VALUE2=${VALUE1//_NO_REMOTE_TRACKING_/${GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING}} - echo ${VALUE2//_PREHASH_/${GIT_PROMPT_SYMBOLS_PREHASH}} + echo ${VALUE2//_PREHASH_/${GIT_PROMPT_SYMBOLS_PREHASH}} } function updatePrompt() { From d83795adcbb37aed26a3bb3484fb63f9ae4869b9 Mon Sep 17 00:00:00 2001 From: oGre Date: Tue, 22 Sep 2015 07:10:32 +0200 Subject: [PATCH 4/5] Revert unset prompt_callback and extra call to git_prompt_reset --- gitprompt.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 97b12d0..da53548 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -65,7 +65,6 @@ function get_theme() { } function git_prompt_load_theme() { - git_prompt_reset get_theme local DEFAULT_THEME_FILE="${__GIT_PROMPT_DIR}/themes/Default.bgptheme" source "${DEFAULT_THEME_FILE}" @@ -179,7 +178,6 @@ git_prompt_reset() { for var in GIT_PROMPT_DIR __GIT_PROMPT_COLORS_FILE __PROMPT_COLORS_FILE __GIT_STATUS_CMD GIT_PROMPT_THEME_NAME; do unset $var done - unset -f prompt_callback } # gp_format_exit_status RETVAL From 249c24e279d6394c152c7aa3b2f0a29caa6f896b Mon Sep 17 00:00:00 2001 From: oGre Date: Mon, 28 Sep 2015 07:05:59 +0200 Subject: [PATCH 5/5] Two functions missed. --- gitprompt.sh | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index ed60902..1913b5b 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -189,16 +189,16 @@ git_prompt_reset() { # signalled, otherwise echos the original value of RETVAL gp_format_exit_status() { - local RETVAL="$1" - local SIGNAL - # Suppress STDERR in case RETVAL is not an integer (in such cases, RETVAL - # is echoed verbatim) - if [ "${RETVAL}" -gt 128 ] 2>/dev/null; then - SIGNAL=$(( ${RETVAL} - 128 )) - kill -l "${SIGNAL}" 2>/dev/null || echo "${RETVAL}" - else - echo "${RETVAL}" - fi + local RETVAL="$1" + local SIGNAL + # Suppress STDERR in case RETVAL is not an integer (in such cases, RETVAL + # is echoed verbatim) + if [ "${RETVAL}" -gt 128 ] 2>/dev/null; then + SIGNAL=$(( ${RETVAL} - 128 )) + kill -l "${SIGNAL}" 2>/dev/null || echo "${RETVAL}" + else + echo "${RETVAL}" + fi } function git_prompt_config() { @@ -427,11 +427,11 @@ function replaceSymbols() GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING=L fi - local VALUE=${1//_AHEAD_/${GIT_PROMPT_SYMBOLS_AHEAD}} - local VALUE1=${VALUE//_BEHIND_/${GIT_PROMPT_SYMBOLS_BEHIND}} + local VALUE=${1//_AHEAD_/${GIT_PROMPT_SYMBOLS_AHEAD}} + local VALUE1=${VALUE//_BEHIND_/${GIT_PROMPT_SYMBOLS_BEHIND}} local VALUE2=${VALUE1//_NO_REMOTE_TRACKING_/${GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING}} - echo ${VALUE2//_PREHASH_/${GIT_PROMPT_SYMBOLS_PREHASH}} + echo ${VALUE2//_PREHASH_/${GIT_PROMPT_SYMBOLS_PREHASH}} } function updatePrompt() {