-
Notifications
You must be signed in to change notification settings - Fork 36
Support ncurses extended colors API #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1321,7 +1321,11 @@ curses_init_pair(VALUE obj, VALUE pair, VALUE f, VALUE b) | |||||||||||||
| { | ||||||||||||||
| /* may have to raise exception on ERR */ | ||||||||||||||
| curses_stdscr(); | ||||||||||||||
| #ifdef HAVE_INIT_EXTENDED_PAIR | ||||||||||||||
| return (init_extended_pair(NUM2INT(pair), NUM2INT(f), NUM2INT(b)) == OK) ? Qtrue : Qfalse; | ||||||||||||||
| #else | ||||||||||||||
| return (init_pair(NUM2INT(pair),NUM2INT(f),NUM2INT(b)) == OK) ? Qtrue : Qfalse; | ||||||||||||||
| #endif | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
|
|
@@ -1345,8 +1349,13 @@ curses_init_color(VALUE obj, VALUE color, VALUE r, VALUE g, VALUE b) | |||||||||||||
| { | ||||||||||||||
| /* may have to raise exception on ERR */ | ||||||||||||||
| curses_stdscr(); | ||||||||||||||
| #ifdef HAVE_INIT_EXTENDED_COLOR | ||||||||||||||
| return (init_extended_color(NUM2INT(color), NUM2INT(r), | ||||||||||||||
| NUM2INT(g), NUM2INT(b)) == OK) ? Qtrue : Qfalse; | ||||||||||||||
| #else | ||||||||||||||
| return (init_color(NUM2INT(color),NUM2INT(r), | ||||||||||||||
| NUM2INT(g),NUM2INT(b)) == OK) ? Qtrue : Qfalse; | ||||||||||||||
| #endif | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
|
|
@@ -1397,11 +1406,22 @@ curses_colors(VALUE obj) | |||||||||||||
| static VALUE | ||||||||||||||
| curses_color_content(VALUE obj, VALUE color) | ||||||||||||||
| { | ||||||||||||||
| short r,g,b; | ||||||||||||||
|
|
||||||||||||||
| curses_stdscr(); | ||||||||||||||
| color_content(NUM2INT(color),&r,&g,&b); | ||||||||||||||
| return rb_ary_new3(3,INT2FIX(r),INT2FIX(g),INT2FIX(b)); | ||||||||||||||
| #ifdef HAVE_EXTENDED_COLOR_CONTENT | ||||||||||||||
| { | ||||||||||||||
| int r, g, b; | ||||||||||||||
| if (extended_color_content(NUM2INT(color), &r, &g, &b) == ERR) | ||||||||||||||
| return Qnil; | ||||||||||||||
| return rb_ary_new3(3, INT2FIX(r), INT2FIX(g), INT2FIX(b)); | ||||||||||||||
| } | ||||||||||||||
| #else | ||||||||||||||
| { | ||||||||||||||
| short r, g, b; | ||||||||||||||
| if (color_content(NUM2INT(color), &r, &g, &b) == ERR) | ||||||||||||||
| return Qnil; | ||||||||||||||
| return rb_ary_new3(3, INT2FIX(r), INT2FIX(g), INT2FIX(b)); | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
|
@@ -1430,11 +1450,22 @@ curses_color_pairs(VALUE obj) | |||||||||||||
| static VALUE | ||||||||||||||
| curses_pair_content(VALUE obj, VALUE pair) | ||||||||||||||
| { | ||||||||||||||
| short f,b; | ||||||||||||||
|
|
||||||||||||||
| curses_stdscr(); | ||||||||||||||
| pair_content(NUM2INT(pair),&f,&b); | ||||||||||||||
| return rb_ary_new3(2,INT2FIX(f),INT2FIX(b)); | ||||||||||||||
| #ifdef HAVE_EXTENDED_PAIR_CONTENT | ||||||||||||||
| { | ||||||||||||||
| int f, b; | ||||||||||||||
| if (extended_pair_content(NUM2INT(pair), &f, &b) == ERR) | ||||||||||||||
| return Qnil; | ||||||||||||||
| return rb_ary_new3(2, INT2FIX(f), INT2FIX(b)); | ||||||||||||||
| } | ||||||||||||||
| #else | ||||||||||||||
| { | ||||||||||||||
| short f, b; | ||||||||||||||
| if (pair_content(NUM2INT(pair), &f, &b) == ERR) | ||||||||||||||
| return Qnil; | ||||||||||||||
| return rb_ary_new3(2, INT2FIX(f), INT2FIX(b)); | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
|
|
@@ -1465,6 +1496,41 @@ curses_pair_number(VALUE obj, VALUE attrs) | |||||||||||||
| curses_stdscr(); | ||||||||||||||
| return INT2FIX(PAIR_NUMBER(NUM2CHTYPE(attrs))); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
| * Document-method: Curses.support_extended_colors? | ||||||||||||||
| * | ||||||||||||||
| * Returns +true+ if the ncurses library was compiled with extended color | ||||||||||||||
| * support (i.e., init_extended_pair, init_extended_color, etc. are available), | ||||||||||||||
| * +false+ otherwise. | ||||||||||||||
| */ | ||||||||||||||
| static VALUE | ||||||||||||||
| curses_support_extended_colors(VALUE obj) | ||||||||||||||
| { | ||||||||||||||
| #if defined(HAVE_INIT_EXTENDED_PAIR) && defined(HAVE_INIT_EXTENDED_COLOR) && \ | ||||||||||||||
| defined(HAVE_EXTENDED_COLOR_CONTENT) && defined(HAVE_EXTENDED_PAIR_CONTENT) | ||||||||||||||
| return Qtrue; | ||||||||||||||
| #else | ||||||||||||||
| return Qfalse; | ||||||||||||||
| #endif | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
| * Document-method: Curses.reset_color_pairs | ||||||||||||||
| * | ||||||||||||||
| * Resets all color pairs to undefined. Requires ncurses 6.1+. | ||||||||||||||
| */ | ||||||||||||||
| #ifdef HAVE_RESET_COLOR_PAIRS | ||||||||||||||
| static VALUE | ||||||||||||||
| curses_reset_color_pairs(VALUE obj) | ||||||||||||||
| { | ||||||||||||||
| curses_stdscr(); | ||||||||||||||
| reset_color_pairs(); | ||||||||||||||
|
||||||||||||||
| reset_color_pairs(); | |
| int ret = reset_color_pairs(); | |
| if (ret == ERR) { | |
| rb_sys_fail("reset_color_pairs"); | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |||||||
|
|
||||||||
| # The TERM environment variable should be set to xterm-256color etc. to | ||||||||
| # use 256 colors. Curses.colors returns the color numbers of the terminal. | ||||||||
| # With ncurses 6+ extended color support, color_pairs may exceed 256. | ||||||||
|
|
||||||||
| begin | ||||||||
| init_screen | ||||||||
|
|
@@ -14,14 +15,24 @@ | |||||||
| else | ||||||||
| start_color | ||||||||
|
|
||||||||
| addstr "This Terminal supports #{colors} colors.\n" | ||||||||
|
|
||||||||
| Curses.colors.times { |i| | ||||||||
| Curses.init_pair(i, i, 0) | ||||||||
| attrset(color_pair(i)) | ||||||||
| extended = Curses.support_extended_colors? | ||||||||
| addstr "This Terminal supports #{colors} colors, #{color_pairs} pairs" | ||||||||
| addstr extended ? " (extended).\n" : ".\n" | ||||||||
|
|
||||||||
| (extended ? [512, color_pairs].min : colors).times { |i| | ||||||||
|
||||||||
| (extended ? [512, color_pairs].min : colors).times { |i| | |
| (extended ? [512, color_pairs].min : colors).times { |i| | |
| next if i == 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curses.reset_color_pairsshould follow the pattern of other color APIs by callingcurses_stdscr()before invoking ncurses functions. As written, calling it beforeinit_screencan behave inconsistently vs. the rest of the API.