Search code examples
rubyconsole-applicationcurses

Ruby curses: how to get pure white


I'm having a hard time trying to get a pure white (background) and black (foreground) text with ruby and curses.

With this code

Curses.init_pair(1,COLOR_BLACK,COLOR_WHITE) 
Curses.attron(Curses.color_pair(1))
Curses.stdscr.addstr str
Curses.attroff(Curses.color_pair(1))

I get black text on a grayish background (the bottom three lines in screenshot)

What could I be doing wrong? I tried switching from iterm2 to mac terminal still the same.

screenshot


Solution

  • Most of the terminals which implement "ANSI color" use a more intense foreground color when told to render bold. But there is no corresponding workaround for the background.

    However, the majority of these also implement SGR 39 and SGR 49, which reset the foreground and background to its "default" colors. Using that would get your original terminal background (which is likely what you want). That is a feature of ncurses called use_default_colors, which I see is available in ruby.

    Using that feature, your example would

    • call the Curses.use_default_colors method,
    • not bother (for example) to create a color pair, since the default pair 0 would draw using the terminal's default colors.
    • other color pairs (1 through the maximum number of pairs) would work just like regular curses.

    The example given uses color pair 1. If you were using the default-colors extension, you could initialize Curses with the use_default_colors method. Then, using the default color pair 0, you would see on most terminals the original terminal colors. This method is from the underlying ncurses library irregardless of whether the Ruby package is called "curses" or "ncurses".

    Color pair 0 is special (see manpage). It cannot be set to specific colors by your application. As an extension, ncurses provides a way to alter this in a useful way.