Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Prompt exit code return



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
    }



Messages sorted by: Reverse Date, Date, Thread, Author