Skip to content

Commit 9b3c95b

Browse files
authored
Merge pull request #131 from ruby/feature/wattr
Add Window#attr_set and Window#attr_get
2 parents ffcc80f + fdf92d1 commit 9b3c95b

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

ext/curses/curses.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,6 +2951,64 @@ window_attrset(VALUE obj, VALUE attrs)
29512951
#endif
29522952
}
29532953

2954+
/*
2955+
* Document-method: Curses::Window.attr_set
2956+
* call-seq: attr_set(attrs, pair)
2957+
*
2958+
* Sets the current attributes and color pair of the given window.
2959+
* Unlike Curses::Window.attrset, this method accepts an extended color
2960+
* pair number (> 255) when the ncurses extended colors API is available.
2961+
*
2962+
* Returns +true+ on success, +false+ on failure.
2963+
*
2964+
* see also system manual curs_attr(3)
2965+
*/
2966+
#ifdef HAVE_WATTR_SET
2967+
static VALUE
2968+
window_attr_set(VALUE obj, VALUE attrs, VALUE pair)
2969+
{
2970+
struct windata *winp;
2971+
2972+
GetWINDOW(obj, winp);
2973+
return (wattr_set(winp->window, NUM2UINT(attrs), NUM2INT(pair), NULL) == OK) ? Qtrue : Qfalse;
2974+
}
2975+
#else
2976+
#define window_attr_set rb_f_notimplement
2977+
#endif
2978+
2979+
/*
2980+
* Document-method: Curses::Window.attr_get
2981+
* call-seq: attr_get => [attrs, pair]
2982+
*
2983+
* Returns a 2-element Array of the current attributes and color pair
2984+
* of the given window. The color pair number may exceed 255 when the
2985+
* ncurses extended colors API is available.
2986+
*
2987+
* Returns +nil+ on failure.
2988+
*
2989+
* see also system manual curs_attr(3)
2990+
*/
2991+
#ifdef HAVE_WATTR_GET
2992+
static VALUE
2993+
window_attr_get(VALUE obj)
2994+
{
2995+
struct windata *winp;
2996+
attr_t attrs;
2997+
#ifdef NCURSES_PAIRS_T
2998+
NCURSES_PAIRS_T pair;
2999+
#else
3000+
short pair;
3001+
#endif
3002+
3003+
GetWINDOW(obj, winp);
3004+
if (wattr_get(winp->window, &attrs, &pair, NULL) == ERR)
3005+
return Qnil;
3006+
return rb_ary_new3(2, UINT2NUM(attrs), INT2NUM(pair));
3007+
}
3008+
#else
3009+
#define window_attr_get rb_f_notimplement
3010+
#endif
3011+
29543012
/*
29553013
* Document-method: Curses::Window.bkgdset
29563014
* call-seq: bkgdset(ch)
@@ -5319,6 +5377,8 @@ Init_curses(void)
53195377
rb_define_method(cWindow, "attroff", window_attroff, 1);
53205378
rb_define_method(cWindow, "attron", window_attron, 1);
53215379
rb_define_method(cWindow, "attrset", window_attrset, 1);
5380+
rb_define_method(cWindow, "attr_set", window_attr_set, 2);
5381+
rb_define_method(cWindow, "attr_get", window_attr_get, 0);
53225382
rb_define_method(cWindow, "bkgdset", window_bkgdset, 1);
53235383
rb_define_method(cWindow, "bkgd", window_bkgd, 1);
53245384
rb_define_method(cWindow, "getbkgd", window_getbkgd, 0);

0 commit comments

Comments
 (0)