about summary refs log tree commit diff
path: root/Functions/Prompts
diff options
context:
space:
mode:
Diffstat (limited to 'Functions/Prompts')
-rw-r--r--Functions/Prompts/prompt_oliver_setup35
-rw-r--r--Functions/Prompts/promptinit140
2 files changed, 116 insertions, 59 deletions
diff --git a/Functions/Prompts/prompt_oliver_setup b/Functions/Prompts/prompt_oliver_setup
new file mode 100644
index 000000000..1f4e598fc
--- /dev/null
+++ b/Functions/Prompts/prompt_oliver_setup
@@ -0,0 +1,35 @@
+# oliver prompt theme
+
+prompt_oliver_help() {
+  cat - <<ENDHELP
+With this prompt theme, the prompt contains the current directory,
+history number and the previous command\'s exit code (if non-zero)
+and a final character which depends on priviledges.
+
+The colour of the prompt depends on two associative arrays -
+\$pcolour and $\tcolour. Each array is indexed by the name of the
+local host. Alternatively, the colour can be set with parameters
+to prompt.
+
+The hostname and username are also included unless they are in the
+\$normal_hosts or \$normal_users array.
+ENDHELP
+}
+
+prompt_oliver_setup() {
+  prompt_opts=( percent set )
+  
+  local pcol=$'\e['${1:-${pcolour[${HOST:=`hostname`}]:-33}}m
+  local tcol=$'\e['${2:-${tcolour[$HOST]:-37}}m
+  local a host="%M:" user="%n "
+  for a in $normal_hosts; do
+    [[ $HOST == $a ]] && host=""
+  done
+  for a in root $normal_users; do
+    [[ ${USER:-`whoami`} == $a ]] && user=""
+  done
+
+  PS1="%{$pcol%}$user$host%~ [%h%0(?..:%?)]%# %{$tcol%}"
+}
+
+prompt_oliver_setup "$@"
diff --git a/Functions/Prompts/promptinit b/Functions/Prompts/promptinit
index c3ce4b4fe..157cb1e38 100644
--- a/Functions/Prompts/promptinit
+++ b/Functions/Prompts/promptinit
@@ -35,10 +35,14 @@ promptinit () {
 }
 
 prompt () {
-  emulate -L zsh
-  local opt preview theme usage old_theme
+  local -A prompt_opts
+  local opt
+
+  set_prompt() {
+    emulate -L zsh
+    local opt preview theme usage old_theme
 
-  usage='Usage: prompt <options>
+    usage='Usage: prompt <options>
 Options:
     -l              List currently available prompt themes
     -p [<themes>]   Preview given themes (defaults to all)
@@ -46,63 +50,81 @@ Options:
     -s <theme>      Set and save theme
     <theme>         Switch to new theme immediately (changes not saved)'
 
-  getopts "hlps" opt
-  case "$opt" in
-    h) if [[ -n "$2" && -n $prompt_themes[(r)$2] ]]; then
-         if functions prompt_$2_help >/dev/null; then
-	   print "Help for $2 theme:\n"
-           prompt_$2_help
-         else
-           print "No help available for $2 theme"
-         fi
-       else
-         print "$usage"
-       fi
-       ;;
-    l) print Currently available prompt themes:
-       print $prompt_themes
-       return
-       ;;
-    p) if (( ! $+prompt_theme )); then
-         print "Cannot preview; current prompt is non-themeable and would"
-	 print "be destroyed."
-	 return
-       fi
-       preview=( $prompt_themes )
-       [[ -n "$2" && -n $prompt_themes[(r)$2] ]] && preview=( $*[2,-1] )
-       for theme in $preview; do
-         [[ $theme == $prompt_theme[1] ]] && continue
-         print "\nTheme: $theme"
-         prompt_${theme}_setup
-	 precmd
-	 print -n -P "${PS1}"
-	 preexec
-	 print "command arg1 arg2 ... argn"
-       done
-       print
-       prompt_${prompt_theme}_setup
-       ;;
-    s) print "Set and save not yet implemented.  Please ensure your ~/.zshrc"
-       print "contains something similar to the following:\n"
-       print "  autoload -U promptinit"
-       print "  promptinit"
-       print "  prompt $*[2,-1]"
-       ;;
-    *) if [[ -z "$1" || -z $prompt_themes[(r)$1] ]]; then
-         print "$usage"
-         return
-       fi
-       prompt_$1_setup $*[2,-1]
-       prompt_theme=( $* )
+    getopts "hlps" opt
+    case "$opt" in
+      h)
+        if [[ -n "$2" && -n $prompt_themes[(r)$2] ]]; then
+          if functions prompt_$2_help >/dev/null; then
+	    print "Help for $2 theme:\n"
+            prompt_$2_help
+          else
+            print "No help available for $2 theme"
+          fi
+	else
+          print "$usage"
+	fi
+      ;;
+      l)
+        print Currently available prompt themes:
+	print $prompt_themes
+	return
+      ;;
+      p)
+        if (( ! $+prompt_theme )); then
+          print "Cannot preview; current prompt is non-themeable and would"
+	  print "be destroyed."
+	  return
+	fi
+	preview=( $prompt_themes )
+	[[ -n "$2" && -n $prompt_themes[(r)$2] ]] && preview=( $*[2,-1] )
+	for theme in $preview; do
+          [[ $theme == $prompt_theme[1] ]] && continue
+          print "\nTheme: $theme"
+          prompt_${theme}_setup
+	  precmd
+	  print -n -P "${PS1}"
+	  preexec
+	  print "command arg1 arg2 ... argn"
+	done
+	print
+	prompt_${prompt_theme}_setup
+      ;;
+      s)
+        print "Set and save not yet implemented.  Please ensure your ~/.zshrc"
+	print "contains something similar to the following:\n"
+	print "  autoload -U promptinit"
+	print "  promptinit"
+	print "  prompt $*[2,-1]"
+      ;;
+      *)
+        if [[ -z "$1" || -z $prompt_themes[(r)$1] ]]; then
+          print "$usage"
+          return
+	fi
+	prompt_$1_setup $*[2,-1]
+	prompt_theme=( $* )
+
+	# Avoid screwing up the environment listing
+	PSZZZZ=$reset_color
+	RPSZZZZ=$reset_color
+	PROMPTZZZZ=$reset_color
+	RPROMPTZZZZ=$reset_color
+	promptzzzz=$reset_color
+      ;;
+    esac
+  }
+
+  set_prompt "$@"
 
-       # Avoid screwing up the environment listing
-       PSZZZZ=$reset_color
-       RPSZZZZ=$reset_color
-       PROMPTZZZZ=$reset_color
-       RPROMPTZZZZ=$reset_color
-       promptzzzz=$reset_color
-       ;;
-  esac
+  # Set prompt options
+  for opt in ${(k)prompt_opts}; do
+    if [[ $prompt_opts[$opt] != (|un)set ]]; then
+      echo "${0##*/}: value of prompt option must be 'set' or 'unset'" >&2
+      return 1
+    else
+      $prompt_opts[$opt]opt prompt$opt
+    fi
+  done
 }
 
 promptinit "$@"