Skip to content

Instantly share code, notes, and snippets.

@Surzhko
Last active October 12, 2023 07:56
Show Gist options
  • Save Surzhko/9647582 to your computer and use it in GitHub Desktop.
Save Surzhko/9647582 to your computer and use it in GitHub Desktop.
colorized git ps1
#...
# Colorized output of git branch in prompt
# WORKING_TREE_CLEAN_MESSAGE must be changed due to git version and localization (git status)
# COLOR_DIRTY, COLOR_CLEAN and PS1 must be tweaked due to used OS
__git_color_ps1 ()
{
if [ -n "$(__git_ps1)" ]; then
local WORKING_TREE_CLEAN_MESSAGE="working tree clean"
local COLOR_DIRTY="\e[1;31m"
local COLOR_CLEAN="\e[1;37m"
local IS_CLEAN=$(git status | grep -c "$WORKING_TREE_CLEAN_MESSAGE");
if [ $IS_CLEAN -eq 0 ]; then
echo -e $COLOR_DIRTY
else
echo -e $COLOR_CLEAN
fi
fi
}
PS1="\u@\h:\w\[\$(__git_color_ps1)\]\$(__git_ps1)\[\e[0m\]\$ "
#
# Run 'nvm use' automatically every time there's
# a .node-version file in the directory. Also, revert to default
# version when entering a directory without .nvmrc
# (currently commented out due to slow switching)
#
enter_directory() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
if [[ -f ".node-version" ]]; then
local NODE_VERSION=$(cat .node-version)
local IS_CURRENT=$(nvm current | grep -c $NODE_VERSION)
if [ $IS_CURRENT -eq 0 ]; then
nvm use $NODE_VERSION
fi
# NVM_DIRTY=true
# elif [[ $NVM_DIRTY = true ]]; then
# nvm use default
# NVM_DIRTY=false
fi
}
export PROMPT_COMMAND=enter_directory
# my aliases
alias gg='git gui'
alias gw='dbcli'
alias bgdev='bin/dev all=1,web=0'
alias be='bundle exec'
alias docrun='docker run --rm -v `pwd`:/app -w /app'
#...
#enables git colorized output (http://stackoverflow.com/questions/10998792/how-to-color-the-git-console-in-ubuntu)
$ git config --global color.ui auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment