Search code examples
pythoncolorscurses

Is it possible to get the default background color using curses in python?


Using curses in python you can easily use the default color scheme for the terminal using:

curses.use_default_colors()

However once you try to recolor any character, using a color pair you have to declare a background color:

curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)

I really don't want to change the background from the default but I would like to change the foreground.

Is there any way to either get the default background color? or to change just the foreground color?

I am aware that I could use ANSI escape codes to adjust just the foreground color, however ANSI codes are not compatible with curses and I would rather work with curses than rewrite everything in ANSI codes.


Solution

  • Ok, I figured it out,

    If you call init_pair with -1 as a value it will fill in the terminal default. For example to make red text with the default background:

    curses.init_pair(1, curses.COLOR_RED, -1)
    

    Now curses.color_pair(1) will be set to the background. This will even work if you change the terminal's default while the program is running.

    You do have to call curses.use_default_colors() first to use this.