diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-09-09 23:17:38 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-09-10 08:18:03 +0900 |
commit | 53ed4fb37632789db69815bb9b9df0e3572779ef (patch) | |
tree | 8987f3dc5f906eb8ef9d58fb49f4e5a1987a6f94 | |
parent | 803dc9e1e48325515bf7c5ce176cf2b5bc0792a9 (diff) | |
download | ruby-53ed4fb37632789db69815bb9b9df0e3572779ef.tar.gz |
[ruby/io-console] Added IO#cursor for VT
https://github.com/ruby/io-console/commit/41a6a6cace
-rw-r--r-- | ext/io/console/console.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/ext/io/console/console.c b/ext/io/console/console.c index b50a73ee0b..8c30684113 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -836,7 +836,6 @@ console_key_pressed_p(VALUE io, VALUE k) } #else # define console_goto rb_f_notimplement -# define console_cursor_pos rb_f_notimplement # define console_cursor_set rb_f_notimplement static VALUE read_vt_response(VALUE io, VALUE query) @@ -875,6 +874,24 @@ console_vt_response(int argc, VALUE *argv, VALUE io) VALUE ret = ttymode_with_io(io, read_vt_response, query, set_rawmode, optp); return ret; } + +static VALUE +console_cursor_pos(VALUE io) +{ + VALUE query = rb_str_new_cstr("\e[6n"); + VALUE resp = console_vt_response(1, &query, io); + VALUE row, column, term; + if (!RB_TYPE_P(resp, T_ARRAY) || RARRAY_LEN(resp) != 3) return Qnil; + term = RARRAY_AREF(resp, 2); + if (!RB_TYPE_P(term, T_STRING) || RSTRING_LEN(term) != 1) return Qnil; + if (RSTRING_PTR(term)[0] != 'R') return Qnil; + row = RARRAY_AREF(resp, 0); + column = RARRAY_AREF(resp, 1); + rb_ary_resize(resp, 2); + RARRAY_ASET(resp, 0, column); + RARRAY_ASET(resp, 1, row); + return resp; +} # define console_key_pressed_p rb_f_notimplement #endif |