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

Re: Outputting colored zsh prompts from an external script



> > > So far this color issue is the only thing getting in my way...
> > 
> > Your python script needs to either output %F{color} / %K{color} instead
> > of the raw ANSI sequences, or (might be easier) put %{ and %} around the
> > ANSI.  Both methods tell zsh that this output is "zero-width" on the
> > terminal, so it doesn't count those bytes when determing how much screen
> > space the prompt uses.  See also %G in the manual.
> 
> That was exactly what I needed, thanks! Took all of two seconds to apply
> and worked perfectly. I've updated the gist. The Python code could be
> written more clearly, to make it explicit that the ANSI codes are
> getting wrapped in zsh zero-width codes. Once I've given it some testing
> and finished it off, I'll publish the script properly somewhere.

Here's the "finished" version as I'm using it for now:

https://gist.github.com/seanh/5233082

Anyone know how I can make zsh call this script for PROMPT and RPROMPT,
but fall back on a simple pure-zsh prompt if trying to call the script
exits with non-zero? (Yes, my zsh-scripting skills really are that
limited.)

It does what I wanted from my prompt, with maybe a couple of minor
wrinkles that could be smoothed out. If I can get custom format options
working, it might be useful to others who want to design their own
prompts. I'm thinking it'd work something like this, for example to get
a zsh prompt with current working dir, virtualenv, git branch and last
exit status, you would put something like this in your zshrc:

    PROMPT='$(zshprompt.py --format "{cwd:green} {venv:blue} {git:yellow} {les:red,bold}> ")'

and zshprompt.py would do all the work to handle current working
directory truncation and git branch, virtualenv and non-zero exit status
detection, handle colours and bold etc and transform that (hopefully
simple) string specification into the much more complex string that zsh
needs for the prompt.



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