static int getattr(int fd, conmode *t) { int x = GetConsoleMode((HANDLE)rb_w32_get_osfhandle(fd), t); if (!x) errno = LAST_ERROR; return x; }
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); if (h == (HANDLE)-1) rb_raise(rb_eIOError, "invalid io"); SetConsoleTextAttribute(h, NUM2INT(attr)); return Qnil; }
/* * call-seq: * io.winsize = [rows, columns] * * Tries to set console size. The effect depends on the platform and * the running environment. * * You must require 'io/console' to use this method. */ static VALUE console_set_winsize(VALUE io, VALUE size) { rb_io_t *fptr; rb_console_size_t ws; #if defined _WIN32 HANDLE wh; int newrow, newcol; #endif VALUE row, col, xpixel, ypixel; const VALUE *sz; int fd; GetOpenFile(io, fptr); size = rb_Array(size); rb_check_arity(RARRAY_LENINT(size), 2, 4); sz = RARRAY_CONST_PTR(size); row = sz[0], col = sz[1], xpixel = sz[2], ypixel = sz[3]; fd = GetWriteFD(fptr); #if defined TIOCSWINSZ ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0; #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) SET(row); SET(col); SET(xpixel); SET(ypixel); #undef SET if (!setwinsize(fd, &ws)) rb_sys_fail(0); #elif defined _WIN32 wh = (HANDLE)rb_w32_get_osfhandle(fd); #define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) SET(row); SET(col); #undef SET if (!NIL_P(xpixel)) (void)NUM2UINT(xpixel); if (!NIL_P(ypixel)) (void)NUM2UINT(ypixel); if (!GetConsoleScreenBufferInfo(wh, &ws)) { rb_syserr_fail(LAST_ERROR, "GetConsoleScreenBufferInfo"); } if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) || (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) { if (!SetConsoleScreenBufferSize(wh, ws.dwSize)) { rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo"); } } ws.srWindow.Left = 0; ws.srWindow.Top = 0; ws.srWindow.Right = newcol; ws.srWindow.Bottom = newrow; if (!SetConsoleWindowInfo(wh, FALSE, &ws.srWindow)) { rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo"); } #endif return io; }
/* * call-seq: * io.winsize = [rows, columns] * * Tries to set console size. The effect depends on the platform and * the running environment. * * You must require 'io/console' to use this method. */ static VALUE console_set_winsize(VALUE io, VALUE size) { rb_io_t *fptr; rb_console_size_t ws; #if defined _WIN32 HANDLE wh; int newrow, newcol; #endif VALUE row, col, xpixel, ypixel; #if defined TIOCSWINSZ int fd; #endif GetOpenFile(io, fptr); size = rb_Array(size); rb_scan_args((int)RARRAY_LEN(size), RARRAY_PTR(size), "22", &row, &col, &xpixel, &ypixel); #if defined TIOCSWINSZ fd = GetWriteFD(fptr); ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0; #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) SET(row); SET(col); SET(xpixel); SET(ypixel); #undef SET if (!setwinsize(fd, &ws)) rb_sys_fail(0); #elif defined _WIN32 wh = (HANDLE)rb_w32_get_osfhandle(GetReadFD(fptr)); newrow = (SHORT)NUM2UINT(row); newcol = (SHORT)NUM2UINT(col); if (!getwinsize(GetReadFD(fptr), &ws)) { rb_sys_fail("GetConsoleScreenBufferInfo"); } if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) || (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) { if (!(SetConsoleScreenBufferSize(wh, ws.dwSize) || SET_LAST_ERROR)) { rb_sys_fail("SetConsoleScreenBufferInfo"); } } ws.srWindow.Left = 0; ws.srWindow.Top = 0; ws.srWindow.Right = newcol; ws.srWindow.Bottom = newrow; if (!(SetConsoleWindowInfo(wh, FALSE, &ws.srWindow) || SET_LAST_ERROR)) { rb_sys_fail("SetConsoleWindowInfo"); } #endif return io; }
static VALUE console_cursor_pos(VALUE io) { rb_io_t *fptr; int fd; rb_console_size_t ws; GetOpenFile(io, fptr); fd = GetWriteFD(fptr); if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) { rb_syserr_fail(LAST_ERROR, 0); } return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.X), UINT2NUM(ws.dwCursorPosition.Y)); }
static VALUE console_info(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"); 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.wAttributes)); }
static VALUE console_goto(VALUE io, VALUE x, VALUE y) { rb_io_t *fptr; int fd; COORD pos; GetOpenFile(io, fptr); fd = GetWriteFD(fptr); pos.X = NUM2UINT(x); pos.Y = NUM2UINT(y); if (!SetConsoleCursorPosition((HANDLE)rb_w32_get_osfhandle(fd), pos)) { rb_syserr_fail(LAST_ERROR, 0); } return io; }