Better bash prompt
[pkgusr] / etc / pkgusr / bash_profile
index bc756dd..0123438 100644 (file)
@@ -1,19 +1,23 @@
 # -*- shell-script -*-
-# couple of environment settings to ensure sanity
+## Ensure sanity
 set +h
 umask 022
-LC_ALL=POSIX
+
+## We live in a unicode world now
+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.
-#PATH=/usr/lib/pkgusr:/usr/bin:/bin:/usr/X11R6/bin:/opt/qt/bin:/tools/bin
-PATH=/usr/lib/pkgusr:/usr/bin:/bin:/usr/X11R6/bin:/opt/qt/bin
+# 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
 
 ## A couple things to make less(1) nicer.
-LESS='-MRgiswFX'
+LESS='-FgiMrswX --use-color'
 LESSCHARSET=utf-8
 LESSOPEN='|lesspipe.sh %s'
 
@@ -25,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}
@@ -54,24 +58,127 @@ SUPPRESSLOCALEDIR=1
 #  Override this in ~/.pkgusrrc.
 CHECKUPDATES=1
 
-### export everything
-export LC_ALL PATH LESS LESSCHARSET LESSOPEN TZ PKG_CONFIG_PATH QTDIR
-export SUPPRESSLOCALEDIR CHECKUPDATES
+## Slackware PKG local repo
+#  Having a shortcut to a local Slackware PKG repo is very useful.
+#  Pat Volkerding and his team are of a minority who actually know
+#  what they are doing.  I always check Slackware for patches.
+#
+#  I get the Slack source via rsync.
+#  See https://mirrors.slackware.com/mirrorlist/
+#
+SLACKPKG=/home/steve/download/Slackware/slackware64-current/source
+#  Obviously edit to suit your situation
+
+## Export everything
+export LANG LC_CTYPE PATH LESS LESSCHARSET LESSOPEN TZ PKG_CONFIG_PATH QTDIR
+export SUPPRESSLOCALEDIR CHECKUPDATES SLACKPKG
+
+## 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
+. /etc/pkgusr/handy_funcs
 
-# Make prompt reflect that we are a package user.
-export PROMPT_COMMAND='PS1="[pkgusr (\u)] \w> "'
+## Go to the home directory whenever we su to a package user.
+cd
 
-# Suck in some handy shell functions
-. /etc/pkgusr/handy_funcs
+## Setup the git config if needed
+if [[ ! -d ${HOME}/.config/git && -f ${HOME}/.gitconfig ]]; then
+    prep_git
+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
+## Bash Completion
+if [ -f /usr/share/bash-completion/bash_completion ]; then
+    . /usr/share/bash-completion/bash_completion
 fi
 
-# Go to the home directory whenever we su to a package user.
-cd
+## 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                                                #
+###===============================================================###
 
 # Maybe check if the build script can be updated, but not on dumb
 # terms so that TRAMP still works.