When R is run interactively in a terminal which supports colors, it is possible to use ANSI escape sequences in order to put colors in the prompt, such as options(prompt = "\033[0;31mThis is red\033[0m> ") Unfortunately, something goes wrong because for long command lines, the line continuation override the prompt instead of being written in the next line. The problem gets worse when using several colors, because somehow each escape sequence "takes up some space" in the command line, *up to the point that the end of the prompt might overwrite the beginning*. On my configuration this happens with for instance options(prompt = paste("\033[0;31m With \033[0;32m multiple", "\033[0;33m colors \033[0;34m this", "\033[0;35m gets \033[0;36m really", "\033[0;37m wrong! \033[0m")) Why is it so? Is there a workaround? PS: This rather old post seems related http://r.789695.n4.nabble.com/Xterm-escape-sequences-in-Prompt-td906375.html PPS: I posted this question on [R] section of stack overflow http://stackoverflow.com/questions/42072092/buggy-ansi-escape-sequences-in-r-prompt [[alternative HTML version deleted]]
I guess that R does not know about ANSI sequences, and it calculates the width of the prompt including the characters in the sequences. You cannot do much about this, except maybe use a prompt that has two lines. You can put the colored text and whatever you like in the first line, and the second line can be non-ANSI. Note that this works in the terminal, but does not work in RStudio. RStudio does not support multi-line prompt currently. Maybe this is also interesting for you, if you want a dynamic R prompt: https://github.com/gaborcsardi/prompt#readme (Self-ad, sorry.) Gabor On Tue, Feb 7, 2017 at 8:59 AM, Hugo Raguet <hugo.raguet at centraliens.net> wrote:> When R is run interactively in a terminal which supports colors, it is > possible to use ANSI escape sequences in order to put colors in the prompt, > such as > > options(prompt = "\033[0;31mThis is red\033[0m> ") > > Unfortunately, something goes wrong because for long command lines, the > line continuation override the prompt instead of being written in the next > line. The problem gets worse when using several colors, because somehow > each escape sequence "takes up some space" in the command line, *up to the > point that the end of the prompt might overwrite the beginning*. On my > configuration this happens with for instance > > options(prompt = paste("\033[0;31m With \033[0;32m multiple", > "\033[0;33m colors \033[0;34m this", > "\033[0;35m gets \033[0;36m really", > "\033[0;37m wrong! \033[0m")) > > Why is it so? Is there a workaround? > > PS: This rather old post seems related > > http://r.789695.n4.nabble.com/Xterm-escape-sequences-in- > Prompt-td906375.html > > PPS: I posted this question on [R] section of stack overflow > > http://stackoverflow.com/questions/42072092/buggy-ansi- > escape-sequences-in-r-prompt > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
Thanks for the hint. I agree with your analysis, but that still would not explain why the continuation overrides the begining of the same line instead of going to the next line. I think I'll go for the two-lines prompt. If you have a Stack Overflow account, you can post your answer and I will accept it in a couple of days. That would also make some ad for your dynamic R prompt! It's really cool, but overkill for my use. Hugo 2017-02-07 14:44 GMT+01:00 G?bor Cs?rdi <csardi.gabor at gmail.com>:> I guess that R does not know about ANSI sequences, and it calculates the > width of the prompt including the characters in the sequences. You cannot > do much about this, except maybe use a prompt that has two lines. You can > put the colored text and whatever you like in the first line, and the > second line can be non-ANSI. > > Note that this works in the terminal, but does not work in RStudio. > RStudio does not support multi-line prompt currently. > > Maybe this is also interesting for you, if you want a dynamic R prompt: > https://github.com/gaborcsardi/prompt#readme (Self-ad, sorry.) > > Gabor > > On Tue, Feb 7, 2017 at 8:59 AM, Hugo Raguet <hugo.raguet at centraliens.net> > wrote: > >> When R is run interactively in a terminal which supports colors, it is >> possible to use ANSI escape sequences in order to put colors in the >> prompt, >> such as >> >> options(prompt = "\033[0;31mThis is red\033[0m> ") >> >> Unfortunately, something goes wrong because for long command lines, the >> line continuation override the prompt instead of being written in the next >> line. The problem gets worse when using several colors, because somehow >> each escape sequence "takes up some space" in the command line, *up to the >> point that the end of the prompt might overwrite the beginning*. On my >> configuration this happens with for instance >> >> options(prompt = paste("\033[0;31m With \033[0;32m multiple", >> "\033[0;33m colors \033[0;34m this", >> "\033[0;35m gets \033[0;36m really", >> "\033[0;37m wrong! \033[0m")) >> >> Why is it so? Is there a workaround? >> >> PS: This rather old post seems related >> >> http://r.789695.n4.nabble.com/Xterm-escape-sequences-in-Prom >> pt-td906375.html >> >> PPS: I posted this question on [R] section of stack overflow >> >> http://stackoverflow.com/questions/42072092/buggy-ansi-escap >> e-sequences-in-r-prompt >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > >[[alternative HTML version deleted]]