ruby-changes:42525
From: nobu <ko1@a...>
Date: Fri, 15 Apr 2016 20:16:58 +0900 (JST)
Subject: [ruby-changes:42525] nobu:r54599 (trunk): win32/console: io_handle
nobu 2016-04-15 21:13:35 +0900 (Fri, 15 Apr 2016) New Revision: 54599 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54599 Log: win32/console: io_handle * ext/-test-/win32/console/attribute.c (io_handle): extract conversion from IO instance to HANDLE. Modified files: trunk/ext/-test-/win32/console/attribute.c Index: ext/-test-/win32/console/attribute.c =================================================================== --- ext/-test-/win32/console/attribute.c (revision 54598) +++ ext/-test-/win32/console/attribute.c (revision 54599) @@ -2,29 +2,37 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/win32/console/attribute.c#L2 static VALUE rb_cConsoleScreenBufferInfo; -static VALUE -console_info(VALUE io) +static HANDLE +io_handle(VALUE io) { int fd = NUM2INT(rb_funcallv(io, rb_intern("fileno"), 0, 0)); HANDLE h = (HANDLE)rb_w32_get_osfhandle(fd); - CONSOLE_SCREEN_BUFFER_INFO csbi; if (h == (HANDLE)-1) rb_raise(rb_eIOError, "invalid io"); + return h; +} + +static VALUE +console_info(VALUE io) +{ + HANDLE h = io_handle(io); + CONSOLE_SCREEN_BUFFER_INFO csbi; + if (!GetConsoleScreenBufferInfo(h, &csbi)) rb_syserr_fail(rb_w32_map_errno(GetLastError()), "not console"); return rb_struct_new(rb_cConsoleScreenBufferInfo, - INT2FIX(csbi.dwSize.X), INT2FIX(csbi.dwSize.Y), - INT2FIX(csbi.dwCursorPosition.X), INT2FIX(csbi.dwCursorPosition.Y), + INT2FIX(csbi.dwSize.X), + INT2FIX(csbi.dwSize.Y), + INT2FIX(csbi.dwCursorPosition.X), + INT2FIX(csbi.dwCursorPosition.Y), INT2FIX(csbi.wAttributes)); } static VALUE console_set_attribute(VALUE io, VALUE attr) { - int fd = NUM2INT(rb_funcallv(io, rb_intern("fileno"), 0, 0)); - HANDLE h = (HANDLE)rb_w32_get_osfhandle(fd); + HANDLE h = io_handle(io); - if (h == (HANDLE)-1) rb_raise(rb_eIOError, "invalid io"); SetConsoleTextAttribute(h, (WORD)NUM2INT(attr)); return Qnil; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/