I am able to print ANSI color codes from python to the terminal using code such as print "\033[31m red text \033[0m", which prints "red text" in red.
However, when wrapping text, the terminal still counts the invisible control characters, so the text wraps incorrectly, acting as if the line is longer than it actually is. Reverse-i-search also acts weird, displaying text that does not match up with what is actually under the cursor.
The following link (and others)
https://wiki.archlinux.org/index.php/Color_Bash_Prompt#Regular_user_2 seems to indicate that I need to wrap colors codes in \[ and \] so that terminal can place the cursor correctly on the screen. However, when I print "\[\033[31m\]" in python, the brackets show up on the screen.
How can I send the \[ and \] control characters to terminal from python?
(I've found that color libraries, such as colored, have the same problem)
Do not use [ ] around your color codes. just create variables such as below.