0

I'm using Zsh. I'd like to have a custom prompt in Python interactive mode. Specifically, I'd like to add color.

If I do in Python something like

sys.ps1 = "\033[38;5;76m>>>\033[0m " 

then the prompt displays correctly, but the terminal seems to be confused about the prompt's length, so things get messed up sometimes. This is probably due to the fact that the terminal counts non-printing characters. I need to tell it somehow not to count those characters. But I can't figure out how to do that.

If I use Zsh prompt escapes such as %F{...} or %{ %}, then instead of undergoing expansion, as described here, those escapes are just displayed literally. I guess they don't undergo expansion since the prompt is set from Python rather than in the shell itself.

Zsh counts non-printing characters on default, it doesn't seem to respect [ ] like Bash does, and %{ %} don't undergo expansion when PS1 is set from Python. I'm stuck.

Is there a way to do what I want?

Orius
  • 1,093
  • 5
  • 11
  • 1
    Does this answer your question? [How to fix column calculation in Python readline if using color prompt](https://stackoverflow.com/questions/9468435/how-to-fix-column-calculation-in-python-readline-if-using-color-prompt) – Ture Pålsson Aug 03 '20 at 09:01
  • This has nothing to do with the shell you start Python from. – chepner Aug 03 '20 at 12:59
  • To summarize the duplicate, `\[` and `\]` are `bash` prompt aliases for the characters `\001` and `\002` that Readline (which is what your Python interpreter is using) uses to ignore non-printable characters for length determination. – chepner Aug 03 '20 at 13:01
  • Thank you all! Should I just delete this question? Wouldn't that be rude to you who bothered to answer and comment? I'm not sure what the etiquette here is... – Orius Aug 04 '20 at 04:35

0 Answers0