ruby-changes:57697
From: Nobuyoshi <ko1@a...>
Date: Tue, 10 Sep 2019 08:25:43 +0900 (JST)
Subject: [ruby-changes:57697] 9844a349bf (master): [ruby/io-console] Added IO#goto_column
https://git.ruby-lang.org/ruby.git/commit/?id=9844a349bf From 9844a349bfdd5a97ae639e7798cc51aa897846fb Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 9 Sep 2019 23:53:05 +0900 Subject: [ruby/io-console] Added IO#goto_column https://github.com/ruby/io-console/commit/143a9d5764 diff --git a/ext/io/console/console.c b/ext/io/console/console.c index a99846c..fc35e82 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -820,6 +820,25 @@ console_move(VALUE io, int x, int y) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L820 return io; } +static VALUE +console_goto_column(VALUE io, VALUE val) +{ + rb_io_t *fptr; + HANDLE h; + rb_console_size_t ws; + COORD *pos = &ws.dwCursorPosition; + + GetOpenFile(io, fptr); + h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr)); + if (!GetConsoleScreenBufferInfo(h, &ws)) { + rb_syserr_fail(LAST_ERROR, 0); + } + pos->X = NUM2INT(val); + if (!SetConsoleCursorPosition(h, *pos)) { + rb_syserr_fail(LAST_ERROR, 0); + } + return io; +} #include "win32_vk.inc" static VALUE @@ -923,6 +942,13 @@ console_move(VALUE io, int x, int y) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L942 } return io; } + +static VALUE +console_goto_column(VALUE io, VALUE val) +{ + rb_io_write(io, rb_sprintf("\x1b[%dG", NUM2UINT(val)+1)); + return io; +} # define console_key_pressed_p rb_f_notimplement #endif @@ -1194,6 +1220,7 @@ InitVM_console(void) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L1220 rb_define_method(rb_cIO, "cursor_down", console_cursor_down, 1); rb_define_method(rb_cIO, "cursor_left", console_cursor_left, 1); rb_define_method(rb_cIO, "cursor_right", console_cursor_right, 1); + rb_define_method(rb_cIO, "goto_column", console_goto_column, 1); rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1); #if ENABLE_IO_GETPASS rb_define_method(rb_cIO, "getpass", console_getpass, -1); -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/