From: Patrick Mahoney Date: 2007-08-03T15:20:24+09:00 Subject: [PATCH] Curses - add default_color() routines --1yeeQ81UyVL57Vl7 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline I added conditionally compiled functions to ext/curses of Ruby 1.9 to wrap the curses extensions use_default_colors() and assume_default_colors(). I have tested this with ncurses 5.5.20060422 on an Ubuntu Linux system i686. Could apply to Ruby 1.8 as well. Normally, Curses.start_color causes the terminal window default colors to be white text on black background. Curses.use_default_colors() assigns the terminal's default foreground and background colors to color number -1. The default colors are thus not changed which is important for customized X terminals and those with transparent backgrounds, etc. See default_colors(3NCURSES) manpage for more details. The code documentation was shamelessly ripped from the manpage. Old behavior: ruby -r curses -e 'include Curses; init_screen; start_color; \ addstr("hello"); stdscr.refresh; sleep(1); close_screen' New behavior: ruby -r curses -e 'include Curses; init_screen; start_color; use_default_colors; \ addstr("hello"); stdscr.refresh; sleep(1); close_screen' ChangeLog: Thu Aug 2 23:36:12 2007 Patrick Mahoney * ext/curses/extconf.rb: add checks for use_default_colors() and assume_default_colors() library functions. * ext/curses/curses.c (curses_use_default_colors): new method compiled conditionally if available in the curses library. * ext/curses/curses.c (curses_assume_default_colors): new method compiled conditionally if available in the curses library. --1yeeQ81UyVL57Vl7 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="ruby_curses_add_default_color_funcs.patch" Index: ext/curses/curses.c =================================================================== --- ext/curses/curses.c (revision 12869) +++ ext/curses/curses.c (working copy) @@ -613,6 +613,45 @@ } #endif /* USE_COLOR */ +#ifdef HAVE_USE_DEFAULT_COLORS +/* + * Assign terminal's default foreground and background colors to color number + * -1. So Curses.init_pair(x,Curses::COLOR_RED,-1) will initialize pair x as + * red on default background and init_pair(x,-1,Curses::COLOR_BLUE) will + * initialize pair x as default foreground on blue. + * + * Returns true on success; false on failure. + * + * This function is not available on all platforms. + */ +static VALUE +curses_use_default_colors(VALUE obj) +{ + return (use_default_colors() == OK) ? Qtrue : Qfalse; +} +#endif + +#ifdef HAVE_ASSUME_DEFAULT_COLORS +/* + * Related to Curses.use_default_colors, tells which colors to paint for color + * pair 0. This function recognizes a special color number -1, which denotes + * the default terminal color. + * + * Returns true on success; false on failure. + * + * The following are equivalent: + * Curses.use_default_colors() + * Curses.assume_default_colors(-1,-1) + * + * This function is not available on all platforms. + */ +static VALUE +curses_assume_default_colors(VALUE obj, VALUE fg, VALUE bg) +{ + return (assume_default_colors(NUM2INT(fg), NUM2INT(bg)) == OK) ? Qtrue : Qfalse; +} +#endif + #ifdef USE_MOUSE struct mousedata { MEVENT *mevent; @@ -1432,6 +1471,13 @@ rb_define_module_function(mCurses, "color_pair", curses_color_pair, 1); rb_define_module_function(mCurses, "pair_number", curses_pair_number, 1); #endif /* USE_COLOR */ +#ifdef HAVE_USE_DEFAULT_COLORS + rb_define_module_function(mCurses, "use_default_colors", curses_use_default_colors, 0); +#endif +#ifdef HAVE_ASSUME_DEFAULT_COLORS + rb_define_module_function(mCurses, "assume_default_colors", curses_assume_default_colors, 2); +#endif + #ifdef USE_MOUSE rb_define_module_function(mCurses, "getmouse", curses_getmouse, 0); rb_define_module_function(mCurses, "ungetmouse", curses_ungetmouse, 1); Index: ext/curses/extconf.rb =================================================================== --- ext/curses/extconf.rb (revision 12869) +++ ext/curses/extconf.rb (working copy) @@ -19,7 +19,7 @@ end if make - for f in %w(beep bkgd bkgdset curs_set deleteln doupdate flash getbkgd getnstr init isendwin keyname keypad resizeterm scrl set setscrreg ungetch wattroff wattron wattrset wbkgd wbkgdset wdeleteln wgetnstr wresize wscrl wsetscrreg def_prog_mode reset_prog_mode timeout wtimeout nodelay init_color wcolor_set) + for f in %w(beep bkgd bkgdset curs_set deleteln doupdate flash getbkgd getnstr init isendwin keyname keypad resizeterm scrl set setscrreg ungetch wattroff wattron wattrset wbkgd wbkgdset wdeleteln wgetnstr wresize wscrl wsetscrreg def_prog_mode reset_prog_mode timeout wtimeout nodelay init_color wcolor_set use_default_colors assume_default_colors) have_func(f) end flag = "-D_XOPEN_SOURCE_EXTENDED" --1yeeQ81UyVL57Vl7--