PYENV prompt.
[profile.git] / .profile.d / ps1.bashrc
index bb05253..7c4cf2c 100644 (file)
-# $Id$
-# VERSION 1.3 (2005-08-15)
+#!bash Coloured prompts.
+# profile-required: TERM.bashrc
 #
-# Coloured prompts for bash.
+# The prompt comprises multiple parts, some of which may be hidden by unsetting
+# shell variables or using the ``prompt'' function.
 #
-# Prompt is user@host:/dir$ where host is highlighted in green if the last 
-# command exited 0 or in red (followed by the error code) otherwise.
+# The first part of the prompt is user@host where host is highlighted in
+# green if the last command exited 0 or in red otherwise.
+# This part will be shown only if __ps1_user is 1.  By default it is 1.
+# The success and failure colours can be changed by modifying
+# $PROMPT_OK_COLOUR and $PROMPT_FAILED_COLOUR respectively.
 #
-# Override the PROMPT_OK_COLOUR and PROMPT_FAILED_COLOUR environment variables 
-# to set different colours.
+# Subsequent parts are taken from *.ps1 files in the .ps1.d directory.
+# Each file <plugin>.ps1 should contain:
+# * A line __ps1_<plugin>=${__ps1_<plugin>:-X} where X is 0 or 1 to activate
+#   the plugin by default (or not).
+# * A function __ps1_<plugin>() which returns the text to be displayed.
+#   The function will be passed the return code of the last command as its
+#   first argument and should return the same code.
+#   The function should print nothing unless __ps1_<plugin> is 1.
+#   The function should call "__ps1_prefix $1 __ps1_<plugin>" to retrieve a
+#   prefix string.  When the string is non-empty, it should be printed before
+#   any output from the plugin itself.  Doing so will ensure that the text is
+#   formatted correctly regardless of whether other parts of the prompt are
+#   being shown.
+# * One to three lines __ps1_<plugin>_colour256, __ps1_<plugin>_colour88 and
+#   __ps1_<plugin>_colour setting the colour for the plugin.
+# The plugin should not emit ANSI colour sequences itself.  PS1 needs to wrap
+# all escape sequences with the literal strings \[ and \] but these must be
+# embedded directly into the prompt and not evaluated programmatically.
+# Omitting them will cause the terminal to redraw incorrectly under certain
+# circumstances.  Trying to include them in a function will not work.
+# Instead the plugin should define at least __ps1_<plugin>_colour.  The
+# colour will be generated before any text from the plugin is printed.
+# To handle 88- and 256-colour terminals, the plugin may also declare
+# __ps1_<plugin>_colour88 and/or __ps1_<plugin>_colour256.
 #
-# To use, add a call to __ps1 in your .bash_profile.
+# Example plugin code:
+#
+# __ps1_example_colour='0;33'
+#
+# function __ps1_example() {
+#   if [ -n "$__ps1_example" ]; then
+#     echo -n "$(__ps1_prefix $1 __ps1_example)"
+#     echo -n "example!"
+#   fi
+#   return $1
+# }
+#
+# The next part of the prompt is the exit status of the last command.
+# This part will be shown only if __ps1_user is set and the exit status is
+# non-zero.
+#
+# The penultimate part of the prompt is the number of background jobs managed
+# by the shell, in square brackets.  If all background jobs are running,
+# their number will be shown as [n].  If some are stopped, the number of
+# running (r) and total (t) jobs will be shown as [r/t].
+# This part will be shown only if __ps1_bg is 1.  By default it is 0.
+
+# The final part of the prompt is the (full) working directory and $ string.
+# If the shell is running as root the # string's colour can be changed by
+# modifying $ROOT_OK_COLOUR and $ROOT_FAILED_COLOUR.
+#
+# Colouring is performed by the __ps1_col() and __ps1_ret() functions.
+# We redirect stderr to /dev/null when calling these functions to prevent
+# bash complaining about not knowing them when you su to another user,
+# retaining PS1 but not the function definitions.
+#
+# Note that $? is passed as an argument to - and is returned from - all
+# functions.  As $? is set following any shell activity it is only guaranteed
+# to represent the return code of the last command at the beginning of __ps1().
+# By passing between subsequent functions we ensure that it is available for
+# __ps1_ret().
+#
+
+# Pick a colour based on the terminal capabilities.
+# OK: dark green.
+# Failed: dark red.
+case $(tput colors) in
+  256)
+    __ps1_colours=256
+    PROMPT_BACKGROUND_COLOUR="0;48;5;26"
+    PROMPT_OK_COLOUR="1;38;5;34"
+    PROMPT_FAILED_COLOUR="1;38;5;160"
+    ROOT_OK_COLOUR="0"
+    ROOT_FAILED_COLOUR="0"
+  ;;
+
+  88)
+    __ps1_colours=88
+    PROMPT_BACKGROUND_COLOUR="0;48;5;18"
+    PROMPT_OK_COLOUR="1;38;5;24"
+    PROMPT_FAILED_COLOUR="1;38;5;48"
+    ROOT_OK_COLOUR="0"
+    ROOT_FAILED_COLOUR="0"
+  ;;
+
+  *)
+    __ps1_colours=
+    PROMPT_BACKGROUND_COLOUR="0;44"
+    PROMPT_OK_COLOUR="1;32"
+    PROMPT_FAILED_COLOUR="1;31"
+    ROOT_OK_COLOUR="0"
+    ROOT_FAILED_COLOUR="0"
+  ;;
+esac
 
-export PROMPT_OK_COLOUR=${PROMPT_OK_COLOUR:-32}
-export PROMPT_FAILED_COLOUR=${PROMPT_OK_COLOUR:-31}
+__ps1_all='$__ps1_user'
 
 function __ps1() {
-  export PS1='\u@\[\033[1;$(__ps1_col $?)m\]\h\[\033[0m\]$(__ps1_ret $?):\w\$ '
+  # Default __ps1_user to 1.
+  [ -z "$__ps1_user" ] && __ps1_user=1
+
+  local pre='\[\033[$(__ps1_background $?)m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[0m\]'
+  local post='\[\033[$(__ps1_background $?)m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_bg $?)$(__ps1_colon $?)$(__ps1_short $?)\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] '
+  local snippets=''
+  local snippet=
+  for snippet in ${PROFILE_HOME:-~}/.ps1.d/*.ps1; do
+    . $snippet
+    local name=${snippet##*/}
+    name=__ps1_${name%.ps1}
+    __ps1_all="$__ps1_all\$$name"
+    snippets="$snippets"'\[$(__ps1_colour_start $? '"$name"')\]$('"$name"' $? $__ps1_all 2>/dev/null)\[$(__ps1_colour_end $? '"$name"')\]'
+  done
+  PS1=$pre$snippets$post
+
   return 0
 }
 
-function __ps1_col() {
-  [ $1 -gt 0 ] && echo -n "$PROMPT_FAILED_COLOUR" || echo -n "$PROMPT_OK_COLOUR"
+function __ps1_colour_for() {
+  local colour=
+  local ret=
+  for colour in "${1}_colour${__ps1_colours}" "${1}_colour"; do
+    eval "export ret=\$$colour"
+    [ -n "$ret" ] && break
+  done
+  echo -n $ret
+}
+
+function __ps1_prefix() {
+  local var=\$${2#\$}
+  local prefix=${__ps1_all%$var*}
+  eval "all=\$$prefix"
+  [ "${all/1/}" = "$all" ] || echo -n " "
+  return $1
+}
+
+# iTerm doesn't like it if you set bold and colour at the same time.
+function __ps1_colour_escape() {
+  local ret=$1; shift
+  local bold="${1%%;*}"
+  local colour="${1#*;}"
+  local bgcolour="${2#*;}"
+
+  echo -en "${bold}m\033[$colour"
+  [ "$__ps1_background" = 1 ] && echo -en "m\033[$bgcolour"
+  return $ret
+}
+
+function __ps1_colour_start() {
+  local colour=$(__ps1_colour_for $2)
+  echo -en '\033['
+  __ps1_colour_escape 0 "$colour" "$PROMPT_BACKGROUND_COLOUR"
+  echo -n m
+  return $1
+}
+
+function __ps1_colour_end() {
+  [ -n "$(__ps1_colour_for $2)" ] && echo -en '\033[0m'
   return $1
 }
 
+function __ps1_background() {
+  local ret=$1; shift
+  echo -n "0"
+  [ "$__ps1_background" = 1 ] && echo -en "m\033[${PROMPT_BACKGROUND_COLOUR}"
+  return $ret
+}
+
+function __ps1_col() {
+  local ret=$1; shift
+  if [ $ret -gt 0 ]; then
+    __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
+  else
+    __ps1_colour_escape $ret "$PROMPT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
+  fi
+  return $ret
+}
+
+function __ps1_root() {
+  local ret=$1; shift
+  if [ $ret -gt 0 ]; then
+    __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
+  else
+    __ps1_colour_escape $ret "$ROOT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
+  fi
+  return $ret
+}
+
 function __ps1_ret() {
+  [ "$__ps1_user" = "1" ] || return $1
   [ $1 -gt 0 ] && echo -n " ($1)"
-  return 0
+  return $1
+}
+
+function __ps1_user() {
+  local ret=$1; shift
+  [ "$__ps1_user" = "1" ] || return $ret
+  echo -n ${1+"$@"}
+  return $ret
+}
+
+function __ps1_bg() {
+  [ "$__ps1_bg" = "1" ] || return $1
+  local job
+  local running=0; for job in $(builtin jobs -pr); do running=$((running+1)); done
+  local total=0; for job in $(builtin jobs -p); do total=$((total+1)); done
+  [ $total = 0 ] && return $1
+  if [ -z "$2" ]; then
+    [ "$__ps1_user" = "1" ] && echo -n " "
+    echo -n "["
+    [ $running = $total ] || echo -n "$running/"
+    echo -n "$total]"
+  else
+    echo $2
+  fi
+  return $1
+}
+
+function __ps1_colon() {
+  local all="$__ps1_user$(eval echo $__ps1_all)$(__ps1_bg $1 1)"
+  [ "${all/1/}" = "$all" ] || echo -n ":"
+  return $1
 }
+
+function __ps1_short() {
+  local home=${HOME%%/}
+  local pwd=${PWD/#$home/\~}
+  local dirtrim=${PROMPT_DIRTRIM//[^0-9]/}
+
+  if [ "${dirtrim:0:1}" = "0" -o "$PWD" = "$HOME" ]; then
+    echo "$pwd"
+    return $1
+  fi
+
+  dirtrim=${dirtrim##0}
+  if [ -z "$dirtrim" ]; then
+    local prompt="$USER$HOSTNAME$pwd"
+    local width=$(((COLUMNS*2)/3))
+    if [ ${#prompt} -le ${width:-53} ]; then
+      echo "$pwd"
+      return $1
+    else
+      dirtrim=1
+    fi
+  fi
+
+  local dirname=${pwd##*/}
+  local basename=${pwd%/$dirname}
+  local reversed=
+  local component
+  for component in ${basename//\// }; do
+    reversed="$component $reversed"
+  done
+  local n=1
+  local short=
+  for component in $reversed; do
+    [ $n = 1 ] || short="/$short"
+    if [ $n -ge $dirtrim ]; then
+      short="${component:0:1}$short"
+    else
+      short="$component$short"
+    fi
+    n=$((n+1))
+  done
+
+  [ "${short:0:1}" = "~" -o -z "$basename" ] || short="/$short"
+  echo "$short/$dirname"
+  return $1
+}
+
+function prompt() {
+  local blurb="Usage: prompt hide|show <what>"
+
+  if [ $# -lt 2 ]; then
+    echo >&2 "$blurb"
+    return 1
+  fi
+
+  action="$1"
+  if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
+    echo >&2 "$blurb"
+    return 1
+  fi
+  if [ "$action" = "hide" ]; then
+    action=0
+  else
+    action=1
+  fi
+
+  what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
+  eval "__ps1_$what=$action"
+}
+
+__ps1