zsh-users
 help / color / mirror / code / Atom feed
* Prompt exit code return
@ 2011-07-14 13:51 Andrei Onoie
  2011-07-14 15:22 ` Bart Schaefer
  2011-07-14 16:23 ` Bart Schaefer
  0 siblings, 2 replies; 3+ messages in thread
From: Andrei Onoie @ 2011-07-14 13:51 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 426 bytes --]

Hello there.
This is my $PS1 var:
PS1='%(?.%F{green}.%F{red})%#%f '
So it is green if exit code is 0 and red if it is not...
So when exit code != 0 , it is red no matter how much you hit return..
I want for %# to be red if exit code != 0 only after the command is
finished,and then after I hit return it is green again...I was thinking
there could be a postcmd() like precmd but that wouldn't work would it?
Thanks in advance

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

* Re: Prompt exit code return
  2011-07-14 13:51 Prompt exit code return Andrei Onoie
@ 2011-07-14 15:22 ` Bart Schaefer
  2011-07-14 16:23 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2011-07-14 15:22 UTC (permalink / raw)
  To: zsh-users

On Jul 14,  4:51pm, Andrei Onoie wrote:
} Subject: Prompt exit code return
}
} 
} Hello there.
} This is my $PS1 var:
} PS1='%(?.%F{green}.%F{red})%#%f '
} So it is green if exit code is 0 and red if it is not...
} So when exit code != 0 , it is red no matter how much you hit return..
} I want for %# to be red if exit code != 0 only after the command is
} finished,and then after I hit return it is green again...I was thinking
} there could be a postcmd() like precmd but that wouldn't work would it?

It wouldn't help to have a postcmd because it would always be called
immediately before precmd, so anything you can do there, you can also
do in precmd, and there's already preexec which is called only when
a command is executed -- so I take it you're looking for something
that is called only when a command is NOT executed?

You can do this pretty easily with a simple combination of preexec and
precmd:

    typeset -ghi _next_command _last_command
    preexec() { (( _next_command++ )) }
    precmd() {
      if (( _next_command == _last_command ))
      then # the command buffer was empty when accepted
	PS1='%F{green}%#%f '
      else # a command was executed
	(( _last_command = _next_command ))
	PS1='%(?.%F{green}.%F{red})%#%f '
      fi
    }


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

* Re: Prompt exit code return
  2011-07-14 13:51 Prompt exit code return Andrei Onoie
  2011-07-14 15:22 ` Bart Schaefer
@ 2011-07-14 16:23 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2011-07-14 16:23 UTC (permalink / raw)
  To: zsh-users

On Jul 14,  4:51pm, Andrei Onoie wrote:
}
} I want for %# to be red if exit code != 0 only after the command is
} finished,and then after I hit return it is green again...

It occurs to me to wonder:  My previous response only deals with
making the prompt look green when you hit return on an empty buffer.
Is it possible that you actually want the exit status ($?) reset to
zero instead?

That's a bit more difficult, because you have to actually execute a
command in the normal execution flow (i.e., not inside preexec or
precmd) to get the status to change.  Which means you probably want
to keep it from being entered into the history, etc.

The pieces are all there to accomplish it; override the accept-line
widget to convert an empty buffer into a ":" command, install a
zshaddhistory function to discard it, etc. -- but it's convoluted
to get it right.


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

end of thread, other threads:[~2011-07-14 16:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-14 13:51 Prompt exit code return Andrei Onoie
2011-07-14 15:22 ` Bart Schaefer
2011-07-14 16:23 ` 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).