zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: New prompt themes
@ 1999-10-19 19:11 Oliver Kiddle
  1999-10-20  0:40 ` Bart Schaefer
  1999-10-20  4:16 ` Alternate patch " Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: Oliver Kiddle @ 1999-10-19 19:11 UTC (permalink / raw)
  To: Zsh workers

I've just had a look at the new prompt theme stuff. It seems to be a
very neat system for quickly switching between different prompts.

I came across one problem when writing my own prompt setup scripts.
Basically, the prompt_subst option is disabled in my setup but was
required by a prompt which I wanted to create. I know I could just
change my own setup but I think that the system should be independant
of people's options. The trouble is that the prompt function uses
emulate -L so I can't set options in my prompt setup function. The
following patch is a possible solution. It uses an associative array to
return the prompt options to a wrapper function which sets them. For
example, you can do:
prompt_opts=( subst set percent set bang unset )

The sickly colours of some of the example prompts also made me wonder
whether we should maybe separate the colour control from the prompt
content. One method would be to use an associative array which would
map a name for the content style onto the colour attributes. At a basic
level, there would be a content style corresponding to the prompt, line
editor and command output. Other ones could be used for components to
prompts - hostname, clock, current directory etc. If the different
prompt themes used the same content style names then colour schemes
could be selected independant of the prompt style.

Incidentally, I also think that Misc/colors should use associative
arrays - it would be useful to be able to do $fg[$color]. I'm not even
sure how to do this at the moment.

I've also added to the patch my main prompt (not the one which needs
prompt_subst) which you may want to add to the examples. I mainly
included it because I was wondering if anyone knows a better way to
match the hostnames to the array. I have an array ($normal_hosts)
listing hosts for which I don't want to include the hostname in the
prompt. It'd be nice if this array could be treated as a list of
patterns to be matched instead of fixed strings.

Oliver Kiddle

--- Functions/Prompts/promptinit.bak	Tue Oct 19 16:24:29 1999
+++ Functions/Prompts/promptinit	Tue Oct 19 17:38:30 1999
@@ -36,10 +36,14 @@
 }
 
 prompt () {
-  emulate -L zsh
-  local opt preview theme usage old_theme
+  local -A prompt_opts
+  local opt
 
-  usage='Usage: prompt <options>
+  set_prompt() {
+    emulate -L zsh
+    local opt preview theme usage old_theme
+
+    usage='Usage: prompt <options>
 Options:
     -l              List currently available prompt themes
     -p [<themes>]   Preview given themes (defaults to all)
@@ -47,63 +51,81 @@
     -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=( $* )
-
-       # Avoid screwing up the environment listing
-       PSZZZZ=$reset_color
-       RPSZZZZ=$reset_color
-       PROMPTZZZZ=$reset_color
-       RPROMPTZZZZ=$reset_color
-       promptzzzz=$reset_color
-       ;;
-  esac
+    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 "$@"
+
+  # 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 "$@"
--- /dev/null	Tue Oct 19 04:01:01 1999
+++ Functions/Prompts/prompt_oliver_setup	Tue Oct 19 19:20:32 1999
@@ -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 "$@"


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: PATCH: New prompt themes
  1999-10-19 19:11 PATCH: New prompt themes Oliver Kiddle
@ 1999-10-20  0:40 ` Bart Schaefer
  1999-10-20  4:16 ` Alternate patch " Bart Schaefer
  1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 1999-10-20  0:40 UTC (permalink / raw)
  To: Oliver Kiddle, Zsh workers

On Oct 19,  8:11pm, Oliver Kiddle wrote:
> Subject: PATCH: New prompt themes
> 
> I came across one problem when writing my own prompt setup scripts.
> Basically, the prompt_subst option is disabled in my setup but was
> required by a prompt which I wanted to create. I know I could just
> change my own setup but I think that the system should be independant
> of people's options. The trouble is that the prompt function uses
> emulate -L so I can't set options in my prompt setup function.

That's why the prompt themes created by bash2zshprompt install a precmd
function that includes "setop promptsubst", and why I patched several of
the themes Adam supplied (before that version of bash2zshprompt existed)
to have such a precmd.  (Article 8271.)

> The following patch is a possible solution. It uses an associative
> array to return the prompt options to a wrapper function which sets
> them. For example, you can do:
> prompt_opts=( subst set percent set bang unset )

I posted code to do a similar thing some while back ... article 6185.

> The sickly colours of some of the example prompts also made me wonder
> whether we should maybe separate the colour control from the prompt
> content.

Adam has said he intends to do this eventually.

> Incidentally, I also think that Misc/colors should use associative
> arrays - it would be useful to be able to do $fg[$color].

I mentioned this privately to Adam last week and he agreed it would
be a good idea.

> I was wondering if anyone knows a better way to match the hostnames to
> the array. I have an array ($normal_hosts) listing hosts for which I
> don't want to include the hostname in the prompt. It'd be nice if this
> array could be treated as a list of patterns to be matched instead of
> fixed strings.

Instead of

> +  for a in $normal_hosts; do
> +    [[ $HOST == $a ]] && host=""
> +  done

Just do

	[[ $HOST == (${(j(|))~normal_hosts}) ]] && host=""

The same will work for $normal_users.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Alternate patch Re: PATCH: New prompt themes
  1999-10-19 19:11 PATCH: New prompt themes Oliver Kiddle
  1999-10-20  0:40 ` Bart Schaefer
@ 1999-10-20  4:16 ` Bart Schaefer
  1999-10-21  8:54   ` Oliver Kiddle
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 1999-10-20  4:16 UTC (permalink / raw)
  To: Zsh workers

On Oct 19,  8:11pm, Oliver Kiddle wrote:
} Subject: PATCH: New prompt themes
}
} The trouble is that the prompt function uses emulate -L so I can't set
} options in my prompt setup function. The following patch is a possible
} solution. It uses an associative array to return the prompt options to
} a wrapper function which sets them. For example, you can do:
} prompt_opts=( subst set percent set bang unset )

There doesn't seem to be any reason to put the set_prompt function inside
the prompt function, so I suggest the following smaller patch.  It's the
same code Oliver sent, without a lot of the reindentation, so it would be
used in place of Oliver's original.

I'm not sure that it's that useful to factor out the prefix "prompt" from
each of the strings in prompt_opts, but I'm not going to change that here.
Further, I might suggest that instead of an assoc. array with both set and
unset values, just use a regular array of only the values that are meant to
be set; e.g. one command instead of a loop:

  setopt noprompt{bang,cr,percent,subst} prompt$^prompt_opts

However, I didn't make that change either.

Index: Functions/Prompts/promptinit
===================================================================
@@ -34,7 +34,7 @@
   prompt_newline=$(echo -ne "\n%{\r%}")
 }
 
-prompt () {
+set_prompt() {
   emulate -L zsh
   local opt preview theme usage old_theme
 
@@ -103,6 +103,23 @@
        promptzzzz=$reset_color
        ;;
   esac
+}
+
+prompt () {
+  local -A prompt_opts
+  local opt
+
+  set_prompt "$@"
+
+  # 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 "$@"

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Alternate patch Re: PATCH: New prompt themes
  1999-10-20  4:16 ` Alternate patch " Bart Schaefer
@ 1999-10-21  8:54   ` Oliver Kiddle
  1999-10-21 15:09     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Oliver Kiddle @ 1999-10-21  8:54 UTC (permalink / raw)
  To: Zsh workers

Bart Schaefer wrote:

> That's why the prompt themes created by bash2zshprompt install a precmd
> function that includes "setop promptsubst", and why I patched several of

I hadn't thought of doing it that way and had only read the text not the patch in your message so hadn't seen it. I agree with your past comments about stomping on precmd though and think it is better not to use precmd just for one setopt which doesn't need to be run every time the prompt is printed anyway.
 
> Just do
>         [[ $HOST == (${(j(|))~normal_hosts}) ]] && host=""

That works well, thanks.

Bart Schaefer wrote:
> I'm not sure that it's that useful to factor out the prefix "prompt" from
> each of the strings in prompt_opts, but I'm not going to change that here.

I did that because it seemed an easy way to restrict it to the prompt options.

> Further, I might suggest that instead of an assoc. array with both set and
> unset values, just use a regular array of only the values that are meant to
> be set; e.g. one command instead of a loop:
>   setopt noprompt{bang,cr,percent,subst} prompt$^prompt_opts
> However, I didn't make that change either.

That probably is better. An alternative patch to do it that way follows. The only thing I'm not sure about is whether it is wise that promptcr is controlled by the prompt themes. I'd consider it to be down to personal preference whether it's set or not. I also can't see that it could be crucial to a particular theme so you might want to remove cr from the brace expansion.

Incidentally, I had to use prompt${^prompt_opts[@]} instead of what you suggested because after emulate -R ksh, only the first word in the array would be expanded.

*** Functions/Prompts/promptinit.bak	Wed Oct 20 18:25:20 1999
--- Functions/Prompts/promptinit	Wed Oct 20 18:37:18 1999
***************
*** 34,40 ****
    prompt_newline=$(echo -ne "\n%{\r%}")
  }
  
! prompt () {
    emulate -L zsh
    local opt preview theme usage old_theme
  
--- 34,40 ----
    prompt_newline=$(echo -ne "\n%{\r%}")
  }
  
! set_prompt() {
    emulate -L zsh
    local opt preview theme usage old_theme
  
***************
*** 103,108 ****
--- 103,117 ----
         promptzzzz=$reset_color
         ;;
    esac
+ }
+ 
+ prompt() {
+   local prompt_opts
+   
+   set_prompt "$@"
+   
+   (( $#prompt_opts )) &&
+       setopt noprompt{bang,cr,percent,subst} prompt${^prompt_opts[@]}
  }
  
  promptinit "$@"


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Alternate patch Re: PATCH: New prompt themes
  1999-10-21  8:54   ` Oliver Kiddle
@ 1999-10-21 15:09     ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 1999-10-21 15:09 UTC (permalink / raw)
  To: Zsh workers

On Oct 21,  9:54am, Oliver Kiddle wrote:
} Subject: Re: Alternate patch Re: PATCH: New prompt themes
}
} > Further, I might suggest that instead of an assoc. array with both set and
} > unset values, just use a regular array of only the values that are meant to
} > be set; e.g. one command instead of a loop:
} >   setopt noprompt{bang,cr,percent,subst} prompt$^prompt_opts
} > However, I didn't make that change either.
} 
} That probably is better. An alternative patch to do it that way
} follows.

In that case:

Index: Functions/Prompts/prompt_oliver_setup
===================================================================
@@ -17,7 +17,7 @@
 }
 
 prompt_oliver_setup() {
-  prompt_opts=( percent set )
+  prompt_opts=( percent )
   
   local pcol=$'\e['${1:-${pcolour[${HOST:=`hostname`}]:-33}}m
   local tcol=$'\e['${2:-${tcolour[$HOST]:-37}}m

} The only thing I'm not sure about is whether it is wise that
} promptcr is controlled by the prompt themes. I'd consider it to be
} down to personal preference whether it's set or not. I also can't see
} that it could be crucial to a particular theme so you might want to
} remove cr from the brace expansion.

I can't make up my mind about this.  If you're going to remove it from
the brace expansion, then you should probably also filter it out of the
value of prompt_opts.  On the other hand, almost no fancy prompt -- and
certainly not one that uses the width of the screen and/or an RPROMPT --
works correctly when promptcr is turned off.  Even Zle doesn't work very
well when it only thinks it started out in the leftmost column.


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1999-10-21 15:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-10-19 19:11 PATCH: New prompt themes Oliver Kiddle
1999-10-20  0:40 ` Bart Schaefer
1999-10-20  4:16 ` Alternate patch " Bart Schaefer
1999-10-21  8:54   ` Oliver Kiddle
1999-10-21 15:09     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).