Skip to content

Instantly share code, notes, and snippets.

@ferki
Created April 27, 2018 08:54
Show Gist options
  • Save ferki/5fc97a2c1cbc673c595fb6f897a46209 to your computer and use it in GitHub Desktop.
Save ferki/5fc97a2c1cbc673c595fb6f897a46209 to your computer and use it in GitHub Desktop.
git bash prompt with elapsed time and return code
function timer_start {
timer=${timer:-$SECONDS}
}
function timer_stop {
exit_code=$?
timer_show=$(($SECONDS - $timer))
unset timer
}
trap 'timer_start' DEBUG
exit_code_ps1() {
timer_stop;
#[[ $exit_code -ne 0 ]] && echo "\[\e[01;31m\]✘ " || echo "\[\e[01;32m\]✔ "
[[ $exit_code -ne 0 ]] && echo -n "\[\e[01;31m\]" || echo -n "\[\e[01;32m\]"
echo [${timer_show}s]
}
source /usr/share/git/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWSTASHSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWUPSTREAM="auto"
#export GIT_PS1_STATESEPARATOR="|"
export GIT_PS1_SHOWCOLORHINTS=true
#export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#export PROMPT_COMMAND='__git_ps1 "\e[01;32m\]\u@\h \e[01;34m\]\W " "\e[01;34m\]\\\$ \e[00m\]" "\e[01;33m\]%s) "'
#export PROMPT_COMMAND='__git_ps1 "\e[01;32m\]\u@\h \e[01;34m\]\W " "$(exit_code_ps1)\e[01;34m\]\\\$ \e[00m\]" "\e[01;33m\](%s) "'
#export PROMPT_COMMAND='__git_ps1 "\e[01;32m\]\u@\h \e[01;34m\]\W " "$(exit_code_ps1)\e[01;34m\]\\\$ \e[00m\]" "\e[01;33m\](%s) "; timer_stop; PS1="[${timer_show}s] $PS1"'
#export PROMPT_COMMAND='__git_ps1 "\e[01;32m\]\u@\h \e[01;34m\]\W " "$(exit_code_ps1)\e[01;34m\]\\\$ \e[00m\]" "\e[01;33m\][%s\e[01;33m\]] "; timer_stop; PS1="[${timer_show}s] $PS1"'
#export PROMPT_COMMAND='__git_ps1 "\[\e[01;32m\]\u@\h \[\e[01;34m\]\W " "$(exit_code_ps1)\[\e[01;34m\]\\\$ \[\e[00m\]" "\[\e[01;33m\][%s\[\e[01;33m\]] "; timer_stop; PS1="[${timer_show}s] $PS1"'
export PROMPT_COMMAND='__git_ps1 "$(exit_code_ps1) \[\e[01;32m\]\u@\h \[\e[01;34m\]\W " "\[\e[01;34m\]\\\$ \[\e[00m\]" "\[\e[01;33m\][%s\[\e[01;33m\]] "; timer_stop'
# if [ "$PROMPT_COMMAND" == "" ]; then
# PROMPT_COMMAND="timer_stop"
# else
# PROMPT_COMMAND="$PROMPT_COMMAND; timer_stop"
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment