@ -1,14 +1,12 @@
#!/bin/sh
#!/bin/sh
function async_run( )
function async_run( ) {
{
{
{
eval " $@ " & > /dev/null
eval " $@ " & > /dev/null
} &
} &
}
}
function git_prompt_dir( )
function git_prompt_dir( ) {
{
# assume the gitstatus.sh is in the same directory as this script
# assume the gitstatus.sh is in the same directory as this script
# code thanks to http://stackoverflow.com/questions/59895
# code thanks to http://stackoverflow.com/questions/59895
if [ -z " $__GIT_PROMPT_DIR " ] ; then
if [ -z " $__GIT_PROMPT_DIR " ] ; then
@ -26,8 +24,7 @@ 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 CUSTOM_THEME_FILE = " ${ HOME } /.git-prompt-colors.sh "
local DEFAULT_THEME_FILE = " ${ __GIT_PROMPT_DIR } /themes/Default.bgptheme "
local DEFAULT_THEME_FILE = " ${ __GIT_PROMPT_DIR } /themes/Default.bgptheme "
@ -52,9 +49,11 @@ function get_theme()
local theme = ""
local theme = ""
# use default theme, if theme was not found
# use default theme, if theme was not found
for themefile in $( cd " $__GIT_PROMPT_DIR /themes " && echo *) ; do
for themefile in " ${ __GIT_PROMPT_DIR } /themes/ " *.bgptheme; do
if [ [ " ${ themefile } " = " ${ GIT_PROMPT_THEME } .bgptheme " ] ] ; then
local basename = ${ themefile ##*/ }
if [ [ " ${ basename %.bgptheme } " = " ${ GIT_PROMPT_THEME } " ] ] ; then
theme = $GIT_PROMPT_THEME
theme = $GIT_PROMPT_THEME
break
fi
fi
done
done
@ -67,25 +66,23 @@ function get_theme()
fi
fi
}
}
function git_prompt_load_theme( )
function git_prompt_load_theme( ) {
{
get_theme
get_theme
local DEFAULT_THEME_FILE = " ${ __GIT_PROMPT_DIR } /themes/Default.bgptheme "
local DEFAULT_THEME_FILE = " ${ __GIT_PROMPT_DIR } /themes/Default.bgptheme "
source " ${ DEFAULT_THEME_FILE } "
source " ${ DEFAULT_THEME_FILE } "
source " ${ __GIT_PROMPT_THEME_FILE } "
source " ${ __GIT_PROMPT_THEME_FILE } "
}
}
function git_prompt_list_themes( )
function git_prompt_list_themes( ) {
{
local oldTheme
local oldTheme
local oldThemeFile
local oldThemeFile
git_prompt_dir
git_prompt_dir
get_theme
get_theme
for themefile in ` ls " $__GIT_PROMPT_DIR /themes " ` ; do
for themefile in " ${ __GIT_PROMPT_DIR } /themes/ " *.bgptheme; do
local theme = " $( basename $themefile .bgptheme) "
local basename = ${ themefile ##*/ }
local theme = " ${ basename %.bgptheme } "
if [ [ " ${ GIT_PROMPT_THEME } " = " ${ theme } " ] ] ; then
if [ [ " ${ GIT_PROMPT_THEME } " = " ${ theme } " ] ] ; then
echoc ${ Red } " * ${ theme } "
echoc ${ Red } " * ${ theme } "
else
else
@ -117,7 +114,7 @@ function git_prompt_make_custom_theme() {
if [ [ " ${ base } " = "Custom" ] ] ; then
if [ [ " ${ base } " = "Custom" ] ] ; then
echoc ${ Red } "You cannot use the custom theme as base"
echoc ${ Red } "You cannot use the custom theme as base"
else
else
echoc ${ Green } " Creating new cutom theme in \" ${ HOME } /.git-prompt-colors.sh\" "
echoc ${ Green } " Creating new cus tom theme in \" ${ HOME } /.git-prompt-colors.sh\" "
echoc ${ DimYellow } " Please add ${ Magenta } \"GIT_PROMPT_THEME=Custom\" ${ DimYellow } to your .bashrc to use this theme "
echoc ${ DimYellow } " Please add ${ Magenta } \"GIT_PROMPT_THEME=Custom\" ${ DimYellow } to your .bashrc to use this theme "
if [ [ " ${ base } " = = "Default" ] ] ; then
if [ [ " ${ base } " = = "Default" ] ] ; then
cp " ${ __GIT_PROMPT_DIR } /themes/Custom.bgptemplate " " ${ HOME } /.git-prompt-colors.sh "
cp " ${ __GIT_PROMPT_DIR } /themes/Custom.bgptemplate " " ${ HOME } /.git-prompt-colors.sh "
@ -160,7 +157,7 @@ function gp_set_file_var() {
# return 0 (true) if any FILEPATH is readable, set ENVAR to it
# return 0 (true) if any FILEPATH is readable, set ENVAR to it
# return 1 (false) if not
# return 1 (false) if not
function gp_maybe_set_envar_to_path( ) {
function gp_maybe_set_envar_to_path( ) {
local envar = " $1 "
local envar = " $1 "
shift
shift
local file
local file
@ -203,15 +200,14 @@ gp_format_exit_status() {
fi
fi
}
}
function git_prompt_config( )
function git_prompt_config( ) {
{
#Checking if root to change output
#Checking if root to change output
_isroot = false
_isroot = false
[ [ $UID -eq 0 ] ] && _isroot = true
[ [ $UID -eq 0 ] ] && _isroot = true
# There are two files related to colors:
# There are two files related to colors:
#
#
# prompt-colors.sh -- sets generic color names suitable for bash `PS1` prompt
# prompt-colors.sh -- sets generic color names suitable for bash 'PS1' prompt
# git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh
# git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh
if gp_set_file_var __PROMPT_COLORS_FILE prompt-colors.sh ; then
if gp_set_file_var __PROMPT_COLORS_FILE prompt-colors.sh ; then
@ -225,6 +221,12 @@ function git_prompt_config()
git_prompt_load_theme
git_prompt_load_theme
if is_function prompt_callback; then
prompt_callback = "prompt_callback"
else
prompt_callback = "prompt_callback_default"
fi
if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ] ; then
if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ] ; then
LAST_COMMAND_INDICATOR = " $GIT_PROMPT_COMMAND_OK " ;
LAST_COMMAND_INDICATOR = " $GIT_PROMPT_COMMAND_OK " ;
else
else
@ -270,16 +272,7 @@ function git_prompt_config()
if [ [ " $GIT_PROMPT_ONLY_IN_REPO " = 1 ] ] ; then
if [ [ " $GIT_PROMPT_ONLY_IN_REPO " = 1 ] ] ; then
EMPTY_PROMPT = " $OLD_GITPROMPT "
EMPTY_PROMPT = " $OLD_GITPROMPT "
else
else
local ps = ""
local ps = " $( gp_add_virtualenv_to_prompt) $PROMPT_START $( $prompt_callback ) $PROMPT_END "
if [ [ -n " $VIRTUAL_ENV " ] ] ; then
VENV = $( basename " ${ VIRTUAL_ENV } " )
ps = " ${ ps } ${ GIT_PROMPT_VIRTUALENV //_VIRTUALENV_/ ${ VENV } } "
fi
if [ [ -n " $CONDA_DEFAULT_ENV " ] ] ; then
VENV = $( basename " ${ CONDA_DEFAULT_ENV } " )
ps = " ${ ps } ${ GIT_PROMPT_VIRTUALENV //_VIRTUALENV_/ ${ VENV } } "
fi
ps = " $ps $PROMPT_START $( $prompt_callback ) $PROMPT_END "
EMPTY_PROMPT = " ${ ps //_LAST_COMMAND_INDICATOR_/ ${ LAST_COMMAND_INDICATOR } } "
EMPTY_PROMPT = " ${ ps //_LAST_COMMAND_INDICATOR_/ ${ LAST_COMMAND_INDICATOR } } "
fi
fi
@ -289,8 +282,8 @@ function git_prompt_config()
fi
fi
if [ [ -z " $__GIT_STATUS_CMD " ] ] ; then # if GIT_STATUS_CMD not defined..
if [ [ -z " $__GIT_STATUS_CMD " ] ] ; then # if GIT_STATUS_CMD not defined..
git_prompt_dir
git_prompt_dir
if ! gp_maybe_set_envar_to_path __GIT_STATUS_CMD " $__GIT_PROMPT_DIR /gitstatus.sh " ; then
if ! gp_maybe_set_envar_to_path __GIT_STATUS_CMD " $__GIT_PROMPT_DIR / $GIT_PROMPT_STATUS_COMMAND " ; then
echo 1>& 2 "Cannot find gitstatus.sh! "
echo 1>& 2 " Cannot find $GIT_PROMPT_STATUS_COMMAND ! "
fi
fi
# __GIT_STATUS_CMD defined
# __GIT_STATUS_CMD defined
fi
fi
@ -319,7 +312,7 @@ function update_old_git_prompt() {
function setGitPrompt( ) {
function setGitPrompt( ) {
update_old_git_prompt
update_old_git_prompt
local repo = ` git rev-parse --show-toplevel 2> /dev/null`
local repo = $( git rev-parse --show-toplevel 2> /dev/null)
if [ [ ! -e " $repo " ] ] && [ [ " $GIT_PROMPT_ONLY_IN_REPO " = 1 ] ] ; then
if [ [ ! -e " $repo " ] ] && [ [ " $GIT_PROMPT_ONLY_IN_REPO " = 1 ] ] ; then
# we do not permit bash-git-prompt outside git repos, so nothing to do
# we do not permit bash-git-prompt outside git repos, so nothing to do
PS1 = " $OLD_GITPROMPT "
PS1 = " $OLD_GITPROMPT "
@ -375,7 +368,7 @@ function olderThanMinutes() {
fi
fi
if [ [ " $_have_find_mmin " = 1 ] ] ; then
if [ [ " $_have_find_mmin " = 1 ] ] ; then
matches = ` " $_find_command " " $1 " -mmin +" $2 " 2> /dev/null`
matches = $( " $_find_command " " $1 " -mmin +" $2 " 2> /dev/null)
find_exit_code = " $? "
find_exit_code = " $? "
if [ [ -n " $matches " ] ] ; then
if [ [ -n " $matches " ] ] ; then
return 0
return 0
@ -418,8 +411,7 @@ function checkUpstream() {
fi
fi
}
}
function replaceSymbols( )
function replaceSymbols( ) {
{
if [ [ -z ${ GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING } ] ] ; then
if [ [ -z ${ GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING } ] ] ; then
GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING = L
GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING = L
fi
fi
@ -499,23 +491,46 @@ function updatePrompt() {
__chk_gitvar_status 'CLEAN' '-eq 1' -
__chk_gitvar_status 'CLEAN' '-eq 1' -
__add_status " $ResetColor $GIT_PROMPT_SUFFIX "
__add_status " $ResetColor $GIT_PROMPT_SUFFIX "
NEW_PROMPT = ""
NEW_PROMPT = " $( gp_add_virtualenv_to_prompt) $PROMPT_START $( $prompt_callback ) $STATUS $PROMPT_END "
if [ [ -n " $VIRTUAL_ENV " ] ] ; then
else
VENV = $( basename " ${ VIRTUAL_ENV } " )
NEW_PROMPT = " $EMPTY_PROMPT "
NEW_PROMPT = " $NEW_PROMPT ${ GIT_PROMPT_VIRTUALENV //_VIRTUALENV_/ ${ VENV } } "
fi
fi
PS1 = " ${ NEW_PROMPT //_LAST_COMMAND_INDICATOR_/ ${ LAST_COMMAND_INDICATOR } } "
}
# Helper function that returns virtual env information to be set in prompt
# Honors virtualenvs own setting VIRTUAL_ENV_DISABLE_PROMPT
function gp_add_virtualenv_to_prompt {
local ACCUMULATED_VENV_PROMPT = ""
local VENV = ""
if [ [ -n " $VIRTUAL_ENV " && -z " ${ VIRTUAL_ENV_DISABLE_PROMPT - } " ] ] ; then
VENV = $( basename " ${ VIRTUAL_ENV } " )
ACCUMULATED_VENV_PROMPT = " ${ ACCUMULATED_VENV_PROMPT } ${ GIT_PROMPT_VIRTUALENV //_VIRTUALENV_/ ${ VENV } } "
fi
if [ [ -n " $CONDA_DEFAULT_ENV " ] ] ; then
if [ [ -n " $CONDA_DEFAULT_ENV " ] ] ; then
VENV = $( basename " ${ CONDA_DEFAULT_ENV } " )
VENV = $( basename " ${ CONDA_DEFAULT_ENV } " )
NEW_PROMPT = " $NEW_PROMPT ${ GIT_PROMPT_VIRTUALENV //_VIRTUALENV_/ ${ VENV } } "
ACCUMULATED_VENV_PROMPT = " ${ ACCUMULATED_VENV_PROMPT } ${ GIT_PROMPT_VIRTUALENV //_VIRTUALENV_/ ${ VENV } } "
fi
fi
echo " $ACCUMULATED_VENV_PROMPT "
}
NEW_PROMPT = " $NEW_PROMPT $PROMPT_START $( $prompt_callback ) $STATUS $PROMPT_END "
# Use exit status from declare command to determine whether input argument is a
else
# bash function
NEW_PROMPT = " $EMPTY_PROMPT "
function is_function {
fi
declare -Ff " $1 " >/dev/null;
}
#Helper function that truncates $PWD depending on window width
function gp_truncate_pwd {
local newPWD = " ${ PWD /# $HOME /~ } "
local pwdmaxlen = $(( ${ COLUMNS :- 80 } / 3 ))
[ ${# newPWD } -gt $pwdmaxlen ] && newPWD = " ... ${ newPWD : 3 - $pwdmaxlen } "
echo -n " $newPWD "
}
PS1 = " ${ NEW_PROMPT //_LAST_COMMAND_INDICATOR_/ ${ LAST_COMMAND_INDICATOR } } "
#Sets the window title to the given argument string
function gp_set_window_title {
echo -ne "\033]0;" $@ "\007"
}
}
function prompt_callback_default {
function prompt_callback_default {
@ -523,12 +538,6 @@ function prompt_callback_default {
}
}
function gp_install_prompt {
function gp_install_prompt {
if [ "`type -t prompt_callback`" = 'function' ] ; then
prompt_callback = "prompt_callback"
else
prompt_callback = "prompt_callback_default"
fi
if [ -z " $OLD_GITPROMPT " ] ; then
if [ -z " $OLD_GITPROMPT " ] ; then
OLD_GITPROMPT = $PS1
OLD_GITPROMPT = $PS1
fi
fi