1

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" :)

Jachdich
  • 782
  • 8
  • 23
  • Those who voted to close: please elaborate why, since I can avoid the mistake next time – Jachdich Sep 23 '20 at 13:07
  • The third parameter to `init_pair` is the background color, and `-1` tells ncurses to (try to) use the terminal's default color. – Thomas Dickey Sep 23 '20 at 20:08
  • @ThomasDickey yeah but the issue is that I need 256 colours for the background *and* 256 colours for the foreground, i.e. every colour combination. That would require 65536 colours which afaik curses doesn't support. – Jachdich Sep 24 '20 at 12:37
  • It's done that for a few years (ncurses 6.1 in 2018 - see [announcement](https://invisible-island.net/ncurses/announce-6.1.html#h3-database)). – Thomas Dickey Sep 24 '20 at 18:22
  • It takes time for python itself to catch up, but the [3.10](https://docs.python.org/3.10/library/curses.html#curses.has_extended_color_support) page states that it supports the ncurses 6.1 features. – Thomas Dickey Sep 24 '20 at 20:11
  • @ThomasDickey ah that's why it wasn't working, I'm still on 2.2 – Jachdich Sep 25 '20 at 21:16
  • @ThomasDickey ok so I have ncurses 6.2 and python 3.9 but still `import curses; curses.version` reports 2.2. What can I do? – Jachdich Sep 25 '20 at 21:46
  • There are different python arrangements - some use "python3" and "python2" to allow both, and use a symbolic link for "python". Whether 3.9 has what you need is a different matter - 3.10 is what the documentation said. – Thomas Dickey Sep 25 '20 at 23:45
  • @ThomasDickey yeah I figured it out, long story short I thought 3.10 was the *curses* version I needed. Installing python3.10 now (And I made a mistake even with that: I thought 3.10 == 3.1 haha. Too much maths I'd say) – Jachdich Sep 26 '20 at 13:45
  • sounds good (I'm curious how well the Python developers used the changes in ncurses 6.1). fwiw, there was never an ncurses 2.x or 3.x (it went from 1x. to 4.0 in 1996 as mentioned in the licensing page). – Thomas Dickey Sep 26 '20 at 13:52
  • @ThomasDickey yeah well since `curses.version` in python returned `2.2` I assumed the python curses module's latest version was 3.10 – Jachdich Sep 26 '20 at 13:56
  • @ThomasDickey well now I have another issue... It still doesn't seem to work. I'll make a seperate post for it: https://stackoverflow.com/questions/64078902/python-curses-doesnt-seem-to-work-with-more-than-256-colour-pairs – Jachdich Sep 26 '20 at 14:34

0 Answers0