Skip to content

Commit 143a9d5

Browse files
committed
Added IO#goto_column
1 parent 21d340e commit 143a9d5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

ext/io/console/console.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,25 @@ console_move(VALUE io, int x, int y)
820820
return io;
821821
}
822822

823+
static VALUE
824+
console_goto_column(VALUE io, VALUE val)
825+
{
826+
rb_io_t *fptr;
827+
HANDLE h;
828+
rb_console_size_t ws;
829+
COORD *pos = &ws.dwCursorPosition;
830+
831+
GetOpenFile(io, fptr);
832+
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
833+
if (!GetConsoleScreenBufferInfo(h, &ws)) {
834+
rb_syserr_fail(LAST_ERROR, 0);
835+
}
836+
pos->X = NUM2INT(val);
837+
if (!SetConsoleCursorPosition(h, *pos)) {
838+
rb_syserr_fail(LAST_ERROR, 0);
839+
}
840+
return io;
841+
}
823842
#include "win32_vk.inc"
824843

825844
static VALUE
@@ -923,6 +942,13 @@ console_move(VALUE io, int x, int y)
923942
}
924943
return io;
925944
}
945+
946+
static VALUE
947+
console_goto_column(VALUE io, VALUE val)
948+
{
949+
rb_io_write(io, rb_sprintf("\x1b[%dG", NUM2UINT(val)+1));
950+
return io;
951+
}
926952
# define console_key_pressed_p rb_f_notimplement
927953
#endif
928954

@@ -1194,6 +1220,7 @@ InitVM_console(void)
11941220
rb_define_method(rb_cIO, "cursor_down", console_cursor_down, 1);
11951221
rb_define_method(rb_cIO, "cursor_left", console_cursor_left, 1);
11961222
rb_define_method(rb_cIO, "cursor_right", console_cursor_right, 1);
1223+
rb_define_method(rb_cIO, "goto_column", console_goto_column, 1);
11971224
rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1);
11981225
#if ENABLE_IO_GETPASS
11991226
rb_define_method(rb_cIO, "getpass", console_getpass, -1);

0 commit comments

Comments
 (0)