Skip to content

Commit a54b6e4

Browse files
committed
Try to write DSR query to writable IO
1 parent 7cba765 commit a54b6e4

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

ext/io/console/console.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,13 +1042,32 @@ console_key_pressed_p(VALUE io, VALUE k)
10421042
return GetKeyState(vk) & 0x80 ? Qtrue : Qfalse;
10431043
}
10441044
#else
1045+
static int
1046+
direct_query(VALUE io, VALUE query)
1047+
{
1048+
if (RB_TYPE_P(io, T_FILE)) {
1049+
rb_io_t *fptr;
1050+
VALUE wio;
1051+
GetOpenFile(io, fptr);
1052+
wio = fptr->tied_io_for_writing;
1053+
if (wio) {
1054+
rb_io_write(wio, query);
1055+
rb_io_flush(wio);
1056+
return 1;
1057+
}
1058+
if (write(fptr->fd, RSTRING_PTR(query), RSTRING_LEN(query)) != -1) {
1059+
return 1;
1060+
}
1061+
}
1062+
return 0;
1063+
}
1064+
10451065
static VALUE
10461066
read_vt_response(VALUE io, VALUE query)
10471067
{
10481068
VALUE result, b;
10491069
int num = 0;
1050-
if (!NIL_P(query)) rb_io_write(io, query);
1051-
rb_io_flush(io);
1070+
if (!NIL_P(query) && !direct_query(io, query)) return Qnil;
10521071
if (rb_io_getbyte(io) != INT2FIX(0x1b)) return Qnil;
10531072
if (rb_io_getbyte(io) != INT2FIX('[')) return Qnil;
10541073
result = rb_ary_new();

0 commit comments

Comments
 (0)