zsh-users
 help / color / mirror / code / Atom feed
* How can I maintain my last command’s exit-status while resetting my prompt in ZSH?
@ 2011-02-11  6:03 Elliott Cable
  2011-02-11 16:26 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Elliott Cable @ 2011-02-11  6:03 UTC (permalink / raw)
  To: zsh-users

(I’m really not a mailing-list kinda guy, usually Google/IRC/Twitter
and such for help, but I’ve hit dead-ends everywhere I’ve gone with
this. Forgive me if I make some sort of major mailing-list faux-pas!)

I’m trying to make an über-simple (single-character) prompt that gives
me as much information as possible. Here’s what I have at the moment
(had it for ages, can’t remember where I found the original):


    # RPS1="['%1v', '%2v', '%3v', '%4v', '%5v', '%6v', '%7v', '%8v',
'%9v']" # debug
    PS1=" %(?|%2F|%1F)%1(V|%1v|%(#|#|:))%(?|%2f|%1f) "

    function zle-line-init {
      zle -K vicmd
    }
    zle -N   zle-line-init

    function zle-keymap-select {
      psvar[1]="${${KEYMAP/(main|viins)/>}/vicmd/}"
      zle reset-prompt
      psvar[1]=""
    }
    zle -N   zle-keymap-select


This is fairly simple; it initializes the prompt into command mode on
rendering, and displays a `:` prompt when in the vi’s “command” mode,
and a `>` prompt (by changing `psvar[1]`) when in vi’s “insert” mode.
In addition, the `:` is replaced with `#` if you are acting as root,
and the color of the character exhibits the exit status of the last
command.

The problem: When the keymap changes (that is, when I toggle through
the “command” and “insert” modes using, as an example, `a` and then
the escape key, the `$?` exit-status is overtrodden with a successful
`0` status, thus causing the prompt to display in green instead of red
(even if the previous command failed). How can I save or set the
prompt such that the `%(?|…|…)` portions of `$PS1` will properly
display the exit status of the *last command sent to the shell*?


(I also posted this to Stack Overflow, but I don’t expect to see very
much ZSH expertise there… it seems to be primarily popular with Java
and C# developers:
http://stackoverflow.com/questions/4965994/how-can-i-maintain-my-last-commands-exit-status-while-resetting-my-prompt-in-zsh)


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

* Re: How can I maintain my last command’s exit-status while resetting my prompt in ZSH?
  2011-02-11  6:03 How can I maintain my last command’s exit-status while resetting my prompt in ZSH? Elliott Cable
@ 2011-02-11 16:26 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2011-02-11 16:26 UTC (permalink / raw)
  To: Elliott Cable; +Cc: zsh-users

On Thu, Feb 10, 2011 at 10:03 PM, Elliott Cable <me@ell.io> wrote:
>
> The problem: When the keymap changes (that is, when I toggle through
> the “command” and “insert” modes using, as an example, `a` and then
> the escape key, the `$?` exit-status is overtrodden with a successful
> `0` status, thus causing the prompt to display in green instead of red
> (even if the previous command failed). How can I save or set the
> prompt such that the `%(?|…|…)` portions of `$PS1` will properly
> display the exit status of the *last command sent to the shell*?

This was fixed in July 2010, so eventually you won't need to do anything:

        * users/15217: Src/Zle/zle_main.c: use top-level status
        when redrawing prompt.

In the meantime, try this:

   function zle-keymap-select {
     local Q=$?
     psvar[1]="${${KEYMAP/(main|viins)/>}/vicmd/}"
     (return $Q)
     zle reset-prompt
     psvar[1]=""
   }

If your zsh is recent enough to have anonymous functions, you can replace
the subshell (return $Q) with (){ return $Q }.


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

end of thread, other threads:[~2011-02-11 16:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-11  6:03 How can I maintain my last command’s exit-status while resetting my prompt in ZSH? Elliott Cable
2011-02-11 16:26 ` 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).