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

Outputting colored zsh prompts from an external script


I'm trying to write a Python script to handle my zsh prompts. The idea
is to call my Python script from my zshrc to define my PROMPT and

    PROMPT='$(zshprompt.py left)'
    RPROMPT='$(zshprompt.py right --last-exit-status=$?)'

(I also want to make it fall back on a default, pure-zsh prompt if
zshprompt.py returns a non-zero exist status, but that's to do later.)

The Python script then prints whatever it wants to stdout to form the
prompt and right prompt. The full code is here:


This all works fine, I've got my Python script ouptutting a nicely
truncated current working directory, name of the active virtualenv if
any, name of the current git branch if in a git repo, exit status of the
last command if non-zero, and user@host if using an ssh connection.

The problem comes when I try to introduce colors into the prompts. As
you can see from the Python code it's printing out ANSI escape sequences
to color the prompts, but this messes up the positioning of the right
prompt and cursor in zsh. Here's a screenshot showing the problem:


The cursor has been pushed right along to left edge of the terminal
(instead of being after the left prompt as it should be), and the right
prompt seems to get pushed further and further to the left as more ANSI
escape codes come into the prompts.

Here's a screenshot of the same thing but with zshprompt.py's color()
function disabled, so you can see where everything's supposed to be:


I also tried getting zshprompt.py to print out zsh color codes like
%{$fg[red]%}FOO%{$reset_color%}, but zsh doesn't seem to interpret these
when they're printed by zshprompt.py, it just prints them out into the
prompts literally. I tried various zsh options with this, such as
setopt promptpercent and setopt promptsubst, and tried various other ways of
calling the Python script, such as within double-quotes or back-ticks
instead of single-quotes, or without any quotes at all. Nothing worked.

Any idea how I can do colour prompts from within my Python script?


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