logrusorgru/aurora

Non consuming escapes

blaggacao opened this issue · 3 comments

For manipulating prompts, it is necessary to mark characters as non-greedy (not consuming a space in the column calculation of the tty).

Here is an example how this is done for bash and zsh. Maybe this is in scope for the ultimate color library?

https://github.com/jonmosco/kube-ps1/blob/master/kube-ps1.sh#L57-L73

Sorry, what is it?

With non-greedy, a word I borrowed lacking a better term from the regex world, i refer to control characters that at the same time do not consume a column. By consume, I mean that any specfic shell does not count such caracters as occupying a column space in the total available columns of a line.

A control character which "consumes" a column sabotages a specifix shell's calculation towards the remaining available columns.

This is relevant for colorizing prompts, since if control characters add up towards the column count the coursor is offset too much, which eigher leads additional white spaces if the offending control chars are on the left or a cursor that overlays the left side prompt if the offending control characters are on the right, such as in zsh's RPROMPT.

The gist of it is that the actual implementation of escaping the control chars appropriately is shell specific.

For zsh, for example, anything between %{ & %} does not count towards column count.
For bash it appears to be $'\001' & $'\002'.

I am best guessing that this behaviour is only relevant when colorizing prompts, so it might be necesary to implement some notion of a "prompt mode". But I could be wrong on this and generally escaping control chars would do no harm.

The shell probably would need to be deduced from well known environment variables.

Alternative wording:

put all special codes inside %{...%} brackets to treat code literally and prevent unwanted moving of the cursor position

https://stackoverflow.com/a/30581694

I'm keeping one eye on it.