-
Notifications
You must be signed in to change notification settings - Fork 36
Add Window#attr_set and Window#attr_get #131
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 1 commit
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2951,6 +2951,64 @@ window_attrset(VALUE obj, VALUE attrs) | |||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| * Document-method: Curses::Window.attr_set | ||||||
| * call-seq: attr_set(attrs, pair) | ||||||
| * | ||||||
| * Sets the current attributes and color pair of the given window. | ||||||
| * Unlike Curses::Window.attrset, this method accepts an extended color | ||||||
| * pair number (> 255) when the ncurses extended colors API is available. | ||||||
| * | ||||||
| * Returns +true+ on success, +false+ on failure. | ||||||
| * | ||||||
| * see also system manual curs_attr(3) | ||||||
| */ | ||||||
| #ifdef HAVE_WATTR_SET | ||||||
| static VALUE | ||||||
| window_attr_set(VALUE obj, VALUE attrs, VALUE pair) | ||||||
| { | ||||||
| struct windata *winp; | ||||||
|
|
||||||
| GetWINDOW(obj, winp); | ||||||
| return (wattr_set(winp->window, NUM2ULONG(attrs), NUM2INT(pair), NULL) == OK) ? Qtrue : Qfalse; | ||||||
| } | ||||||
| #else | ||||||
| #define window_attr_set rb_f_notimplement | ||||||
| #endif | ||||||
|
|
||||||
| /* | ||||||
| * Document-method: Curses::Window.attr_get | ||||||
| * call-seq: attr_get => [attrs, pair] | ||||||
| * | ||||||
| * Returns a 2-element Array of the current attributes and color pair | ||||||
| * of the given window. The color pair number may exceed 255 when the | ||||||
| * ncurses extended colors API is available. | ||||||
| * | ||||||
| * Returns +nil+ on failure. | ||||||
| * | ||||||
| * see also system manual curs_attr(3) | ||||||
| */ | ||||||
| #ifdef HAVE_WATTR_GET | ||||||
| static VALUE | ||||||
| window_attr_get(VALUE obj) | ||||||
| { | ||||||
| struct windata *winp; | ||||||
| attr_t attrs; | ||||||
| #ifdef NCURSES_PAIRS_T | ||||||
| NCURSES_PAIRS_T pair; | ||||||
| #else | ||||||
| short pair; | ||||||
| #endif | ||||||
|
|
||||||
| GetWINDOW(obj, winp); | ||||||
| if (wattr_get(winp->window, &attrs, &pair, NULL) == ERR) | ||||||
| return Qnil; | ||||||
| return rb_ary_new3(2, ULONG2NUM(attrs), INT2FIX(pair)); | ||||||
|
||||||
| return rb_ary_new3(2, ULONG2NUM(attrs), INT2FIX(pair)); | |
| return rb_ary_new3(2, UINT2NUM(attrs), INT2NUM(pair)); |
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.
The
NUM2ULONGmacro converts the Ruby Integer tounsigned long, butwattr_set's second parameter expectsattr_t, which in ncurses isunsigned int(32-bit). On 64-bit platforms,unsigned longis 64-bit, so this causes an implicit narrowing conversion warning and potentially incorrect behavior if the value is wider than 32 bits. The correct conversion macro here should beNUM2UINTto matchattr_t(which is anunsigned int-sized type). Compare to how the existingwindow_color_setat line 2811 simply passesattrsas-is after reading it viawattr_get.