Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO#goto
(::int, ::int) -> self
io.goto(line, column) → io
ext/io/console/console.c
View on GitHub ↗
static VALUE
console_goto(VALUE io, VALUE y, VALUE x)
{
#ifdef _WIN32
HANDLE h;
rb_console_size_t ws;
COORD *pos = &ws.dwCursorPosition;
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
if (!GetConsoleScreenBufferInfo(h, &ws)) {
rb_syserr_fail(LAST_ERROR, 0);
}
pos->X = NUM2UINT(x);
pos->Y = ws.srWindow.Top + NUM2UINT(y);
if (!SetConsoleCursorPosition(h, *pos)) {
rb_syserr_fail(LAST_ERROR, 0);
}
#else
rb_io_write(io, rb_sprintf(CSI "%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1));
#endif
return io;
}
Set the cursor position at line and column.
You must require ‘io/console’ to use this method.