tcsh-org / tcsh

This is a read-only mirror of the tcsh code repository.
https://www.tcsh.org/
Other
232 stars 42 forks source link

tcsh rprompt doesn't clear colors at the end of the string #18

Open daveola opened 4 years ago

daveola commented 4 years ago

I am experimenting with colors in tcsh prompts, and I use a prompt and rprompt.

I'm able to add colors to the prompt (and then turn off the color formatting fine), but when I add colors to the rprompt, I am unable to turn off the formatting.

As an example, for red:

%{\033[1;31m%}     turn on red
%{\033[0m%}        turn off formatting

So I tried these simple prompt settings:

set prompt="prompt> "
set rprompt="%{\033[1;31m%} rprompt %{\033[0m%}"

And it turns everything red, including what I type and all my command results.

On the other hand, if you add any characters after the clear formatting codes, then the colors work as expected:

set rprompt="%{\033[1;31m%} rprompt %{\033[0m%} "

(Note the extra space)

This happens on multiple terminal types - is this a tcsh bug?

zoulasc commented 4 years ago

This is a mis-feature because the attribute is attached to the next character after the attribute specifier. The same happens for the regular prompt. Perhaps there should be special NUL attributed character that just sets the attribute and does not print the character, followed by a regular NUL terminator.

daveola commented 4 years ago

So by mis-feature you mean that this is a terminal issue and there's nothing that can be done about it? Is there not a zero-width character that I can place after?

suominen commented 2 years ago

There is a zero-width space in UTF-8, but it won't help, as the code doesn't understand it being zero-width, so the rprompt is displayed one column too early. Thus it visually looks the same as if you used a normal space at its end.

Getting the zero-width space into rprompt was an exercise in quoting (once again):

> perl -CO -e'print "set rprompt='"'"'%{\\033[1;31m%} rprompt%{\\033[0m%}\x{200b}'"'"'\n"' | vis
set rprompt='%{\134033[1;31m%} rprompt%{\134033[0m%}\M-b\M^@\M^K'

I saved that to a file to source it:

> perl -CO -e'print "set rprompt='"'"'%{\\033[1;31m%} rprompt%{\\033[0m%}\x{200b}'"'"'\n"' > rprompt.csh
> source rprompt.csh

By the way, vi(1) will not work correctly with that file, either, as it doesn't understand the zero-widthness of the zero-width space. So it e.g. $ places you one column too far (into "nothing"). Having the zero width space on the screen also confused screen(1) and/or mosh(1) so that some lines were stuck on the terminal display...

But taking your example:

set rprompt="%{\033[1;31m%} rprompt %{\033[0m%}"

You could just move the space preceding the colour code to be after it:

set rprompt="%{\033[1;31m%} rprompt%{\033[0m%} " 

This would effectively give you the same appearance on screen, wouldn't it?

The tcsh(1) manual page does document the fact that a literal escape sequence cannot be the last sequence in a prompt.

daveola commented 2 years ago

Thanks for the ideas.

I tried the zero-width space and, at least on my terminal (xterm-256color) it still left my color stuck at red.

And yes, there was a space after "rprompt" in my example, but that was for readability. I want the "rprompt" to be fully right-justified, so having that space after the color is admittedly a fix for my example, but doesn't actually solve the problem.

On top of that I just realized that there seems to be an additional space added after rprompt regardless. I.e., if I have 80 columns, then the last character of even just a simple "set rprompt='hi'" will actually be at col 79, which doesn't make sense to me and is not consistent with the "left" prompt. So now I've got two spaces to deal with.. :(