summaryrefslogtreecommitdiff
path: root/ext/io
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-09-09 23:53:05 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-09-10 08:18:03 +0900
commit9844a349bfdd5a97ae639e7798cc51aa897846fb (patch)
tree0c884570eefb3b2ee991d29da9a6adfd916d7b14 /ext/io
parente8be056af92b2f13162567c057322a2c16797b79 (diff)
[ruby/io-console] Added IO#goto_column
https://github.com/ruby/io-console/commit/143a9d5764
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2440
Diffstat (limited to 'ext/io')
-rw-r--r--ext/io/console/console.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/ext/io/console/console.c b/ext/io/console/console.c
index a99846cc24..fc35e82129 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)
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)
}
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)
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);