Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the opposite of \b character, i.e. a kind of non-erasing space?

The \b control character, as I understand it, is not supposed to erase the previous character (this would be \b + a del character as well), so something like this works:

>>> print 'pototo\b\b\ba'
potato

Is there a character for moving forwards, like a non-overwriting space? Expected usage would be something like (I've called this character \x):

>>> print 'pototo\r\x\x\xa'
potato

Obviously on a typewriter a normal space would do this just fine. But on a terminal a space erases the letter underneath.

My use case is a pexpect matching kind of scenario where I want to retrospectively go back and decorate certain parts of the output of a character stream with colours, and I'm wondering whether keeping a cache of the whole current line in memory will be necessary or not.

like image 616
wim Avatar asked Dec 10 '12 10:12

wim


1 Answers

If you can rely on ANSI escape code sequences in your terminal (*), you can use the Cursor Forward (CUF) sequence "CSI n C", like this:

print "Pototo\b\b\ba\x1b[2Ces"

and get:

Potatoes

CSI is \x1b[, and is used to start ANSI escape code sequences. 2 is the number of characters to move to the right, and C is the command to move rightwards.


(*) A good approximation is that you can rely on ANSI codes unless you need to support Windows.

like image 75
Magnus Hoff Avatar answered Sep 28 '22 03:09

Magnus Hoff