Better bash prompt
[pkgusr] / etc / pkgusr / bash_profile
index 19ed801..0123438 100644 (file)
@@ -1,5 +1,5 @@
 # -*- shell-script -*-
-# couple of environment settings to ensure sanity
+## Ensure sanity
 set +h
 umask 022
 
@@ -8,10 +8,11 @@ LANG=en_AU.UTF-8
 LC_CTYPE=en_AU.UTF-8
 
 ## PATH
-# The wrappers directory must be the first entry in the PATH.  
+# The wrappers directory must be the first entry in the PATH, and
+# /tools/bin should be last.
 #
-# Once you are no longer needing or using the tools dir you can remove it
-# from the PATH if you want, but leaving it there won't hurt.
+# Once you are no longer needing or using the tools dir you can remove
+# it from the PATH.
 #PATH=/usr/lib/pkgusr:/usr/bin:/usr/X11R6/bin:/opt/qt/bin:/tools/bin
 PATH=/usr/lib/pkgusr:/usr/bin:/usr/X11R6/bin:/opt/qt/bin
 
@@ -28,7 +29,7 @@ QTDIR=/opt/qt
 
 ## pkg-config
 _XORG=/usr/X11R6/lib/pkgconfig:/usr/X11R6/share/pkgconfig
-_KDE=/opt/kde/lib/pkgconfig:/opt/kde/share/pkgconfig
+_KDE=/opt/kde/lib/pkgconfig
 _QT=${QTDIR}/lib/pkgconfig
 _PKGCFG=$(pkg-config --variable pc_path pkg-config)
 PKG_CONFIG_PATH=${_PKGCFG}:${_XORG}:${_KDE}:${_QT}
@@ -68,43 +69,116 @@ CHECKUPDATES=1
 SLACKPKG=/home/steve/download/Slackware/slackware64-current/source
 #  Obviously edit to suit your situation
 
-### export everything
+## Export everything
 export LANG LC_CTYPE PATH LESS LESSCHARSET LESSOPEN TZ PKG_CONFIG_PATH QTDIR
 export SUPPRESSLOCALEDIR CHECKUPDATES SLACKPKG
 
-# Make prompt reflect that we are a package user.
-export PROMPT_COMMAND='PS1="[pkgusr (\u)] \w> "'
-
-# Pretty colours
+## Bash Prompt
+#  Shell prompts are not just eye-candy they should show what you need
+#  to know most and they should do it in such a way that you don't
+#  even notice.  This sets up a two-line prompt that reminds you that
+#  you're currently logged in as a pkgusr, which pkgusr, which group
+#  if that pkgusr has multiple groups and you've newgrp'd, if you're
+#  inside a git repo display the branch and other pertinent info, if
+#  the previous command had a non-zero exit display the code, display
+#  the current time, and current working directory (truncated to
+#  prevent "prompt-creep").  Colourised so that often you won't even
+#  need to read it to take in the info.
+
+# Colours (doesn't include black (30))
+CO="\[\e[0m\]"        # Colour Off
+RED="\[\e[0;31m\]"    # Red
+GRN="\[\e[0;32m\]"    # Green
+YEL="\[\e[0;33m\]"    # Yellow
+BLU="\[\e[0;34m\]"    # Blue
+MAG="\[\e[0;35m\]"    # Magenta
+CYN="\[\e[0;36m\]"    # Cyan
+WTE="\[\e[0;37m\]"    # White
+# Bold (bright)
+BRED="\[\e[1;31m\]"   # Bold Red
+BGRN="\[\e[1;32m\]"   # Bold Green
+BYEL="\[\e[1;33m\]"   # Bold Yellow
+BBLU="\[\e[1;34m\]"   # Bold Blue
+BMAG="\[\e[1;35m\]"   # Bold Magenta
+BCYN="\[\e[1;36m\]"   # Bold Cyan
+BWTE="\[\e[1;37m\]"   # Bold White
+
+# Some pkgusrs may use extra groups so the prompt should reflect that.
+# This function adds the group name to the prompt if necessary.
+_sg()
+{
+    [[ $(id -gn) != $(id -un) ]] && echo "$WTE:$CYN$(id -gn)"
+}
+
+# This function, called from $PROMPT_COMMAND, puts all the pieces of
+# the prompt together.
+make_prompt()
+{
+    rc=$?                      # Return code from previous cmd
+    [[ $rc -gt 0 ]] && prc="$BWTE[$BRED$rc$BWTE] " || prc=
+
+    local sg gitp p
+
+    # Using a subgroup
+    sg=$(_sg)
+
+    # Git
+    [[ -f /etc/pkgusr/git-prompt ]] && gitp=$(__git_ps1) || gitp=
+
+    p="pkgusr"
+
+    # Build up the PS1
+    PS1="$BWTE[$WTE$p $BWTE($CYN\u$sg$BWTE)]"  # [pkgusr (usr:grp)]
+    PS1+="$CO"                                 # reset to not mess up git prompt
+    PS1+="$gitp"                               # (gitstuff)
+    PS1+="\n$prc$BYEL\A$WTE:$BMAG\w$BWTE>$CO " # [rc]TIME:CWD>
+}
+
+# The git goodies
+[[ -f /etc/pkgusr/git-prompt ]] && source /etc/pkgusr/git-prompt
+GIT_PS1_SHOWDIRTYSTATE=true
+GIT_PS1_SHOWSTASHSTATE=true
+GIT_PS1_SHOWUNTRACKEDFILES=true
+GIT_PS1_SHOWUPSTREAM=auto
+GIT_PS1_DESCRIBE_STYLE=branch
+GIT_PS1_SHOWCOLORHINTS=true
+
+# So the prompt doesn't fly off the right-hand edge of the screen
+# when you're down a long directory tree.
+PROMPT_DIRTRIM=4
+
+# Finally, make the prompt happen!
+PROMPT_COMMAND=make_prompt
+
+
+## Pretty colours for ls
 eval $(dircolors -b)
 alias ls='ls --color=always -Fb'
 
-# Suck in some handy shell functions
+## Suck in some handy shell functions
 . /etc/pkgusr/handy_funcs
 
-# Go to the home directory whenever we su to a package user.
+## Go to the home directory whenever we su to a package user.
 cd
 
-# Setup the git config if needed
+## Setup the git config if needed
 if [[ ! -d ${HOME}/.config/git && -f ${HOME}/.gitconfig ]]; then
     prep_git
 fi
 
-# Bash Completion
+## Bash Completion
 if [ -f /usr/share/bash-completion/bash_completion ]; then
     . /usr/share/bash-completion/bash_completion
 fi
 
-# If they exist, load any private settings.  This comes last so that
-# we can override any system defaults if need be
-if [ -f ${HOME}/.pkgusrrc ]; then
-    . ${HOME}/.pkgusrrc
-fi
+## Per-user settings
+#  Called last so that defaults can be overridden.
+[[ -f ${HOME}/.pkgusrrc ]] && . ${HOME}/.pkgusrrc
 
-###
-# Don't put anything below here unless it relies on something being
-# set in ~/.pkgusrrc
-###
+###===============================================================###
+# Don't put anything below here unless it relies on something being #
+# set in ~/.pkgusrrc                                                #
+###===============================================================###
 
 # Maybe check if the build script can be updated, but not on dumb
 # terms so that TRAMP still works.