Bug #8659

Curses::Window#bkgdset does not handle color correctly

Added by Alex Zepeda 9 months ago. Updated 7 months ago.

Assignee:Shugo Maeda
Target version:-
ruby -v:ruby 1.9.3p385 (2013-02-06 revision 39114) [x86_64-darwin12.2.1] Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Colors in curses are handled as high bits on a character. Logically ORing a character with a color pair should allow bkgdset to configure a colored background. This can be seen in the source for the Python curses module. The Python function takes one or two arguments (color + character). If there are two arguments they are ORed together and passed to curses as such. Back in ruby land, with one argument it should be possible to specify a background color for the whole screen thusly:

# Define a color pair
Curses.initpair(1, Curses::COLORGREEN, Curses::COLORBLUE)
# Set the screen background to blue
Curses.bkgdset(' '.ord | Curses.color

The following:

#!/usr/bin/env ruby

require 'curses'


Curses.initpair(1, Curses::COLORYELLOW, Curses::COLORBLUE)
Curses.bkgdset('='.ord | Curses.color


Should fill the screen with equals signs (yellow on blue background), and prompt the user to press any key to continue. With Ruby 1.9.3 this doesn't work. The curses module assumes curses characters are one byte (typeof(chtype) == char). Yet GNU ncurses defines the chtype data type as an unsigned integer (OSX 10.8) or an unsigned long (FreeBSD 9.1, RedHat 7.3). The curses module defines a macro "NUM2CH" to convert from ruby objects to chtype objects. At present NUM2CH is defined as NUM2CHR. Instead NUM2CH should be defined as NUM2INT to allow for values > 255 (ex: character + color).


Associated revisions

Revision 43074
Added by Shugo Maeda 7 months ago

  • ext/curses/extconf.rb: check the size of chtype.

  • ext/curses/curses.c (NUM2CH, CH2NUM): use proper macros for
    the size of chtype.

[Bug #8659]


#1 Updated by Thomas dickey 9 months ago

agree: not just ncurses, but any implementation of SVr4 or X/Open curses will use >8 bits for chtype.
8-bit values were for BSD-curses, which is rarely used (essentially only for antique programs).

#2 Updated by Eric Hodel 9 months ago

  • Status changed from Open to Assigned
  • Assignee set to Shugo Maeda

#3 Updated by Shugo Maeda 7 months ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r43074.
Alex, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

  • ext/curses/extconf.rb: check the size of chtype.

  • ext/curses/curses.c (NUM2CH, CH2NUM): use proper macros for
    the size of chtype.

[Bug #8659]

Also available in: Atom PDF