Skip to content

Instantly share code, notes, and snippets.

@Jeff-Russ
Last active February 3, 2017 21:55
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 Jeff-Russ/912c78e88bbb3fb9a2579eacceed4cb6 to your computer and use it in GitHub Desktop.
Save Jeff-Russ/912c78e88bbb3fb9a2579eacceed4cb6 to your computer and use it in GitHub Desktop.
Bash: Pretty Prompt

Bash: Pretty Prompt

place this in ~/.bashrc or ~/.bash_profile

_pretty_prompt() {
	# args: $maincolor_int, $dircolor_int, $gitcolor_int, $style_str 
	# color ints: green 32, yellow 33, cyan 36, white 37, reset(none) 00
	# style: '1;2;4;' means bold dim and underline. leave off for no style

	[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
	# GIT_PS1_SHOWDIRTYSTATE=true

	# abbrevates path to show only directory and parent directory,
	# showing ~ symbol if appropriate:
	PROMPT_COMMAND='case $PWD in
		$HOME) HPWD="~";;
		$HOME/*/*) HPWD="${PWD#"${PWD%/*/*}/"}";;
		$HOME/*) HPWD="~/${PWD##*/}";;
		/*/*/*) HPWD="${PWD#"${PWD%/*/*}/"}";;
		*) HPWD="$PWD";;
	esac'

	_git_nfo() {
		git status >/dev/null 2>&1;
		if [ $? -eq 0 ]; then 
			local gitstatus=$(git status 2> /dev/null)
			local repo=$(basename $(git rev-parse --show-toplevel))
			local branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
			local marker=''

			if   [[ `echo $gitstatus | grep "nothing to commit"` != "" ]];      then marker=''
			elif [[ `echo $gitstatus | grep "Changes to be committed"` != "" ]];then marker='***'
			elif [[ `echo $gitstatus | grep "Changes not staged"` != "" ]];     then marker='**'
			elif [[ `echo $gitstatus | grep "Untracked"` != "" ]];              then marker='*'
			else marker='?'
			fi
			echo " ($repo $branch$marker)";
		else echo "";
		fi
	}
	
	[ "$SSH_CONNECTION" ] && local userhost='\u@\h' || local userhost='\u'

	local C='\[\033['$4 # color start
	local E='m\]'       # color end
	export PS1="$C$1$E$userhost$C$2$E "'$HPWD'$C$3$E'$(_git_nfo)\n'$C$1$E\$$C"00$E "
}
_pretty_prompt 36 33 36 '1;2;4;' # see function def for args explanation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment