python - How to print terminal formatted output to a variable -


is there method print terminal formatted output variable?

print 'a\bb' --> 'b' 

i want string 'b' variable - how it?

i working text string telnet. want work string printed screen.

so looking this:

simplify_string('a\bb') ==> 'b' 

another example carriage return:

simplify_string('aaaaaaa\rbb') ==> 'bbaaaaa' 

this turns out quite tricky because there lot of terminal formatting commands (including e.g. cursor up/down/left/right commands, terminal colour codes, vertical , horizontal tabs, etc.).

so, if want emulate terminal properly, terminal emulator! pyte (pip install pyte) implements vt102-compatible in-memory virtual terminal. so, can feed text, , formatted text it:

import pyte  screen = pyte.screen(80, 24) stream = pyte.bytestream() stream.attach(screen) stream.feed('xyzzz\by\rfoo') print ''.join(c.data c in screen[0]).rstrip() # prints foozy 

to handle multiple lines, join of lines in text (e.g. '\n'.join(''.join(c.data c in row).rstrip() row in screen).rstrip('\n')).

note doesn't handle trailing spaces, indistinguishable on real terminal anyway.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -