ansi2html
ansi2html copied to clipboard
The library can not render some escape codes about positions.
Hi.
I have a tiny test.
The next code puts a text in the bottom of terminal with urwid:
import urwid
def exit_on_q(key):
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
txt = urwid.Text(u"Hello World")
fill = urwid.Filler(txt, 'bottom')
loop = urwid.MainLoop(fill, unhandled_input=exit_on_q)
loop.run()
And it shows in term:

And the code to get the output in other python file is:
import os
import fcntl
import subprocess
p = subprocess.Popen(['python3', '/tmp/test00.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
fd = p.stdout.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
fd = p.stderr.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
a = p.stdout.read()
from ansi2html import Ansi2HTMLConverter
conv = Ansi2HTMLConverter()
with open('/tmp/caca.html', 'w') as f:
f.write(conv.convert(a.decode('utf8')))
a.decode('utf8')
And the output is:

The string with the escape codes is
'\x1b7\x1b[?47h\x1b[?1002l\x1b[?1000l\x1b[?1000h\x1b[?1002h\x1b)0\x1b[?25l\x1b[0;39;49m\x1b[H\x1b[1;1H\x1b[0;39;49m\x0f\x1b[K\x1b[2;1H\x1b[0;39;49m\x0f\x1b[K\x1b[3;1H\x1b[0;39;49m\x0f\x1b[K\x1b[4;1H\x1b[0;39;49m\x0f\x1b[K\x1b[5;1H\x1b[0;39;49m\x0f\x1b[K\x1b[6;1H\x1b[0;39;49m\x0f\x1b[K\x1b[7;1H\x1b[0;39;49m\x0f\x1b[K\x1b[8;1H\x1b[0;39;49m\x0f\x1b[K\x1b[9;1H\x1b[0;39;49m\x0f\x1b[K\x1b[10;1H\x1b[0;39;49m\x0f\x1b[K\x1b[11;1H\x1b[0;39;49m\x0f\x1b[K\x1b[12;1H\x1b[0;39;49m\x0f\x1b[K\x1b[13;1H\x1b[0;39;49m\x0f\x1b[K\x1b[14;1H\x1b[0;39;49m\x0f\x1b[K\x1b[15;1H\x1b[0;39;49m\x0f\x1b[K\x1b[16;1H\x1b[0;39;49m\x0f\x1b[K\x1b[17;1H\x1b[0;39;49m\x0f\x1b[K\x1b[18;1H\x1b[0;39;49m\x0f\x1b[K\x1b[19;1H\x1b[0;39;49m\x0f\x1b[K\x1b[20;1H\x1b[0;39;49m\x0f\x1b[K\x1b[21;1H\x1b[0;39;49m\x0f\x1b[K\x1b[22;1H\x1b[0;39;49m\x0f\x1b[K\x1b[23;1H\x1b[0;39;49m\x0f\x1b[K\x1b[24;1H\x1b[0;39;49m\x0fHello World\x1b[K'