Skip to content

Instantly share code, notes, and snippets.

@martinus
Created May 4, 2018 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinus/b62a7ee8e7c351b058f35b242975ba30 to your computer and use it in GitHub Desktop.
Save martinus/b62a7ee8e7c351b058f35b242975ba30 to your computer and use it in GitHub Desktop.
awesome bash prompt
function prompt_timer_start {
PROMPT_TIMER=${PROMPT_TIMER:-`date +%s.%3N`}
echo -ne "\033]0;${@}\007"
}
function prompt_svn_stats() {
command -v svn >/dev/null
if [ $? != 0 ]; then
return
fi
local WCROOT=`svn info --show-item wc-root 2>/dev/null`
if [ -z "$WCROOT" ]; then
return
fi
local SVN_INFO=`svn info ${WCROOT} 2>/dev/null`
local CHECKEDOUTURL=`echo "${SVN_INFO}" |sed -ne 's#^URL: ##p'`
local REV=`echo "${SVN_INFO}" |sed -ne 's#^Revision: ##p'`
local ROOTURL=`echo "${SVN_INFO}" |sed -ne 's#^Repository Root: ##p'`
echo " (\e[32m${CHECKEDOUTURL/$ROOTURL\//}\e[1;30m@\e[0;100m${REV})"
}
function prompt_timer_stop {
local EXIT="$?" # MUST come first
local NOW=`date +%s.%3N` # should be high up, for accurate measurement
echo -ne "\033]0;$USER@$HOSTNAME: $PWD\007"
local ELAPSED=$(bc <<< "$NOW - $PROMPT_TIMER")
unset PROMPT_TIMER
local T=${ELAPSED%.*}
local AFTER_COMMA=${ELAPSED##*.}
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
local TIMER_SHOW=
[[ $D > 0 ]] && TIMER_SHOW=${TIMER_SHOW}$(printf '%dd ' $D)
[[ $H > 0 ]] && TIMER_SHOW=${TIMER_SHOW}$(printf '%dh ' $H)
[[ $M > 0 ]] && TIMER_SHOW=${TIMER_SHOW}$(printf '%dm ' $M)
TIMER_SHOW=${TIMER_SHOW}$(printf "%d.${AFTER_COMMA}s" $S)
PS1="\e[0m\n" # begin with a newline
if [ $EXIT != 0 ]; then
PS1+="\e[1;41m ✘ ${EXIT}" # red x with error status
else
PS1+="\e[1;42m ✔" # green tick
fi
PS1+=" \e[0;100;93m `date +%H:%M`" # date, e.g. 17:00
#local PSCHAR="┕▶"
local PSCHAR="▶"
if [ $(id -u) -eq 0 ]; then
PS1+=" \e[1;31m\h " # root: red hostname
PSCHAR="\e[1;31m#\e[0m"
else
PS1+=" \e[1;32m\h " # non-root: green hostname
fi
PS1+="\e[1;94m\w" # working directory
GIT_PS1_SHOWDIRTYSTATE=true # * unstaged, + staged
GIT_PS1_SHOWSTASHSTATE=true # $ stashed
GIT_PS1_SHOWUNTRACKEDFILES=true # % untracked
GIT_PS1_SHOWCOLORHINTS=true
# < behind, > ahead, <> diverged, = same as upstream
GIT_PS1_SHOWUPSTREAM="auto"
# git with 2 arguments *sets* PS1 (and uses color coding)
__git_ps1 "${PS1}\e[0;100m" "\e[0;100m"
# try to append svn
PS1+=`prompt_svn_stats`
PS1+=" \e[0;100;93m${TIMER_SHOW}" # runtime of last command
PS1+=" \e[0m\n${PSCHAR} " # prompt in new line
#PS1+="\e[K\e[0m\n${PSCHAR} " # prompt in new line
}
# see https://gnunn1.github.io/tilix-web/manual/vteconfig/
if [ $TILIX_ID ] || [ $VTE_VERSION ]; then
source /etc/profile.d/vte.sh
fi
trap 'prompt_timer_start "$BASH_COMMAND (`date +%H:%M:%S`)"' DEBUG
PROMPT_COMMAND=prompt_timer_stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment