I use an external command to populate a bash prompt that runs every time PS1 is evaluated. However, I have a problem when this command prints non-printable characters (like color escape codes). Here is an example:
$ cat green_cheese.sh #!/bin/bash echo -e "\033[32mcheese\033[0m" $ export PS1="\$(./green_cheese.sh) \$" cheese $ # <- cheese is green! cheese $ <now type really long command>
The canonical way to handle non-printable characters at the PS1 prompt is to wrap them in the escape sequence \[ and \] . The problem is that if you do this from an external command, these escape files are not processed by the PS1 interpreter:
$ cat green_cheese.sh #!/bin/bash echo -e "\[\033[32m\]cheese\[\033[0m\]" $ export PS1="\$(./green_cheese.sh) \$" \[\]cheese\[\] $ # <- FAIL!
Is there a specific escape sequence that I can use from an external command to achieve the desired result? Or is there a way that I can manually tell the tooltip how many characters specify the width of the invitation?
Suppose that I can print anything that I like from an external command, and that this command can be pretty smart (for example, counting the characters in the output). I can also make the export PS1=... command as complex as necessary. However, color exit codes must come from an external command.
Thanks in advance!
source share