aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/prompt.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2013-06-20 22:51:35 +1200
committerTom Ryder <tom@sanctum.geek.nz>2013-06-20 22:51:35 +1200
commitbebbc9bb1c32f3f460ca6d6e81446fcc996dc9d6 (patch)
treeb10dfe3d6269550146c4d1184921f379cd79af83 /bash/bashrc.d/prompt.bash
parentNix DISPLAY, otherwise don't change environment (diff)
downloaddotfiles-bebbc9bb1c32f3f460ca6d6e81446fcc996dc9d6.tar.gz
dotfiles-bebbc9bb1c32f3f460ca6d6e81446fcc996dc9d6.zip
Massive overhaul of structure for Bash files
* No longer using .bash_logout * No longer using .bash_profile, using POSIX-compliant .profile * instead; in particular, moved most of environment settings into .profile * Moved some of the separable functionality of both .profile and .bashrc into subdirectories (some scripts shared, some not) * Tidied implementation of ls/grep aliases * Updated install script to reflect all of the above
Diffstat (limited to 'bash/bashrc.d/prompt.bash')
-rw-r--r--bash/bashrc.d/prompt.bash82
1 files changed, 82 insertions, 0 deletions
diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash
new file mode 100644
index 00000000..f82ef12f
--- /dev/null
+++ b/bash/bashrc.d/prompt.bash
@@ -0,0 +1,82 @@
+# Frontend to controlling prompt
+function prompt {
+ local colors=$(tput colors 2>/dev/null)
+ local color reset branch head state url root ret
+
+ if [[ $colors -ge 256 ]]; then
+ color='\[\e[38;5;10m\]'
+ reset='\[\e[0m\]'
+ elif [[ $colors -ge 8 ]]; then
+ color='\[\e[1;32m\]'
+ reset='\[\e[0m\]'
+ fi
+
+ case "$1" in
+
+ # Turn complex coloured prompt on
+ on)
+ PROMPT_COMMAND='history -a'
+ PS1='\[\a\][\u@\h:\w]$(prompt return)$(prompt vcs)$(prompt jobs)\$'
+ PS1="${color}${PS1}${reset} "
+ ;;
+
+ # Revert to simple inexpensive prompt
+ off)
+ PROMPT_COMMAND=
+ PS1='\[\a\]\$ '
+ ;;
+
+ # Git prompt function
+ git)
+ git branch &>/dev/null || return 1
+ head="$(git symbolic-ref HEAD 2>/dev/null)"
+ branch="${head##*/}"
+ [[ -n "$(git status 2>/dev/null | \
+ grep -F 'nothing to commit')" ]] || state="!"
+ printf '(git:%s)' "${branch:-unknown}${state}"
+ ;;
+
+ # Mercurial prompt function
+ hg)
+ hg branch &>/dev/null || return 1
+ branch="$(hg branch 2>/dev/null)"
+ [[ -n "$(hg status 2>/dev/null)" ]] && state="!"
+ printf '(hg:%s)' "${branch:-unknown}${state}"
+ ;;
+
+ # Subversion prompt function
+ svn)
+ svn info &>/dev/null || return 1
+ url="$(svn info 2>/dev/null | \
+ awk -F': ' '$1 == "URL" {print $2}')"
+ root="$(svn info 2>/dev/null | \
+ awk -F': ' '$1 == "Repository Root" {print $2}')"
+ branch=${url/$root}
+ branch=${branch#/}
+ branch=${branch#branches/}
+ branch=${branch%%/*}
+ [[ -n "$(svn status 2>/dev/null)" ]] && state="!"
+ printf '(svn:%s)' "${branch:-unknown}${state}"
+ ;;
+
+ # VCS wrapper prompt function
+ vcs)
+ prompt git || prompt svn || prompt hg
+ ;;
+
+ # Return status prompt function
+ return)
+ ret=$?
+ [[ $ret -ne 0 ]] && printf '<%d>' ${ret}
+ ;;
+
+ # Job count prompt function
+ jobs)
+ [[ -n "$(jobs)" ]] && printf '{%d}' $(jobs | sed -n '$=')
+ ;;
+ esac
+}
+
+# Start with full-fledged prompt
+prompt on
+