I'm trying to display "text images" generated by tiv in my python-curses application. Of course, I canot just print the text because (n)curses doesn't handle the escape sequences so I have to do it myself. I'm stuck on how to do the background colour. Here is a snippet of code:
def makeData(data):
out = []
pos = 0
ccol = ()
while pos < len(data):
if ord(data[pos]) == 27: #if it's a colour escape sequence
pos += 2
if data[pos] == "0": pos += 2; continue #tiv resets after every line, ignore that
if data[pos] == "4": fg = False; bg = True
else: fg = True; bg = False
pos += 5 #skip 8;5;
num = ""
while data[pos] != "m":
num += data[pos]
pos += 1
num = int(num)
pos += 1
ccol = (fg, bg, num)
else: #otherwise, add the character with the current colour to the buffer
out.append((ccol, data[pos]))
pos += 1
return out
def main(stdscr):
curses.use_default_colors()
for i in range(0, curses.COLORS):
curses.init_pair(i + 1, i, -1)
y = 1
x = 1
for char in self.data:
if char[1] == "\n":
y += 1
x = 1
else:
self.s.addstr(y, x, char[1], curses.color_pair(char[0][2]))
x += 1
self.s.refresh()
This works to parse the foreground and background colours however I am stuck on how to print the characters with the correct background. Right now, it works with the correct foreground but no background.
Any help would be much appriciated, including "this code is bad do this instead" :)