zsh-users
 help / color / mirror / code / Atom feed
* Emulating tcsh:s prompt
@ 1998-06-09 20:50 Johan Sundström
  1998-06-09 22:15 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Johan Sundström @ 1998-06-09 20:50 UTC (permalink / raw)
  To: zsh-users

I'm trying to emulate tcsh:s cool prompt parameter %T in zsh using the
precmd function. Tcsh:s version of %T gives a five character long field in
the format HH:MM, with a slight exception; if the prompt is displayed on a
full hour (MM=00), the text changes to "Ding!". This only happens the first
time the prompt is generated that minute, the second time a common HH:MM is
generated. Example output near the hour shift for a prompt "%T>":

01:58>
01:58>
01:59>
01:59>
Ding!>
02:00>
02:01>
02:01>

I tried to emulate this behaviour using the precmd function, and even
managed to do it in a rather ugly way:

precmd () { PS1="`HH=\`date +%H\`;MM=\`date +%M\`;if [ -f /var/tmp/Hour -a
$HH != \`cat /var/tmp/Hour\` -a $MM = 00 ];then echo Ding\!;else echo
$HH:$MM;fi;echo $HH >/var/tmp/Hour`> " }

In a more readable fasion:

precmd () {
PS1="`HH=\`date +%H\`
      MM=\`date +%M\`
      if [ -f /var/tmp/Hour -a $HH != \`cat /var/tmp/Hour\` -a $MM = 00 ]
         then
             echo Ding\!
         else
             echo $HH:$MM
      fi
      echo $HH >/var/tmp/Hour
     `> " }

I was wondering, whether there is any way I can set an environment variable
in the parent shell from this function. The kludge using /var/tmp/Hour to
store the hour of the last prompt was a last resort when I didn't manage
setting $Hour. Is there a way? How should it be done?

I had hoped "exec export Hour=$HH" would do the job, but it didn't. I'm
using the Linux version 3.0.5, if that should be of any relevance.

/Johan Sundström



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

* Re: Emulating tcsh:s prompt
  1998-06-09 20:50 Emulating tcsh:s prompt Johan Sundström
@ 1998-06-09 22:15 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 1998-06-09 22:15 UTC (permalink / raw)
  To: Johan Sundström, zsh-users

On Jun 9, 10:50pm, =?iso-8859-1?Q?Johan_Sundstr=F6m?= wrote:
> Subject: Emulating tcsh:s prompt
> I'm trying to emulate tcsh:s cool prompt parameter %T in zsh using the
> precmd function. Tcsh:s version of %T gives a five character long field in
> the format HH:MM, with a slight exception; if the prompt is displayed on a
> full hour (MM=00), the text changes to "Ding!".

PS1="%(t.Ding\!.%D{%I:%M})> "

> ... the second time [that minute] a common HH:MM is generated.

Hardly seems worth the effort, but

precmd() {
  thisminute=$(print -P %D{%M})
  [[ $thisminute = ${lastminute:-0} ]] && {
	PS1="%D{%I:%M}> " ||
	PS1="%(t.Ding\!.%D{%I:%M})> " }
  lastminute=$thisminute
}

> I was wondering, whether there is any way I can set an environment variable
> in the parent shell from this function.

Any variable you set in a function is set in the "parent" shell.  Functions
run in the same context as builtins, by definition.

> I had hoped "exec export Hour=$HH" would do the job, but it didn't.

Leave off the "exec".


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

end of thread, other threads:[~1998-06-09 22:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-06-09 20:50 Emulating tcsh:s prompt Johan Sundström
1998-06-09 22:15 ` 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).