io-console 0.4.8 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e47d9d1d48607091ca031404108b70d572c2007e313deaf5264d310159563dbb
4
- data.tar.gz: 7e7c494fe8011cd0a5e26741c7529eb3e574d7326c65cc9e3c1410170c07291e
3
+ metadata.gz: f87daa288fc605a3bf48b70558316f9427552fcf895e3a40363f71ba095d3e1b
4
+ data.tar.gz: bddde3f65430cad405fdd4ef576f9d18b85368001f5ea27dc16fb941e0065a04
5
5
  SHA512:
6
- metadata.gz: 43f74f32f96daa78be45a1a8584fe11ed71ab8576c8602b70f603e5803f97c43b13e0abc20adbd7fffce4337907595fbe2121637b612874a07454af1d9c310ed
7
- data.tar.gz: e4abad7c152fae66c4cecfba8d0a473bb7af25149ed22e2a459a319111295bf3e48be1894489ae8f6d73e21b2c64eccde8a44ffab4d58cf335c28a2f53e749e3
6
+ metadata.gz: bd7bb0e4285b7a3de96006386f262cc74fde97fc34a157ad8b834661e52a1aefe210d683c8264e6a98cf8ce80610b7fa8917e79d2b80f87ea4ae052e60c98839
7
+ data.tar.gz: 23a0ac10ecd3f77ca899f5c00a9cc0689bc1c593540b116a6656eaef7d50aad887a3cfbcd66e6f954ebcac3e93f7f9eddbcda0f620d7804a196d541359e230bc
@@ -4,6 +4,7 @@
4
4
  */
5
5
  #include "ruby.h"
6
6
  #include "ruby/io.h"
7
+ #include "ruby/thread.h"
7
8
 
8
9
  #ifdef HAVE_UNISTD_H
9
10
  #include <unistd.h>
@@ -48,6 +49,7 @@ typedef struct sgttyb conmode;
48
49
  # endif
49
50
  #elif defined _WIN32
50
51
  #include <winioctl.h>
52
+ #include <conio.h>
51
53
  typedef DWORD conmode;
52
54
 
53
55
  #define LAST_ERROR rb_w32_map_errno(GetLastError())
@@ -73,7 +75,7 @@ getattr(int fd, conmode *t)
73
75
  #define SET_LAST_ERROR (0)
74
76
  #endif
75
77
 
76
- static ID id_getc, id_console, id_close, id_min, id_time;
78
+ static ID id_getc, id_console, id_close, id_min, id_time, id_intr;
77
79
  #if ENABLE_IO_GETPASS
78
80
  static ID id_gets;
79
81
  #endif
@@ -100,20 +102,33 @@ rb_f_send(int argc, VALUE *argv, VALUE recv)
100
102
  typedef struct {
101
103
  int vmin;
102
104
  int vtime;
105
+ int intr;
103
106
  } rawmode_arg_t;
104
107
 
105
108
  static rawmode_arg_t *
106
- rawmode_opt(int argc, VALUE *argv, rawmode_arg_t *opts)
109
+ rawmode_opt(int *argcp, VALUE *argv, int min_argc, int max_argc, rawmode_arg_t *opts)
107
110
  {
111
+ int argc = *argcp;
108
112
  rawmode_arg_t *optp = NULL;
109
- VALUE vopts;
110
- rb_scan_args(argc, argv, "0:", &vopts);
113
+ VALUE vopts = Qnil;
114
+ if (argc > min_argc) {
115
+ vopts = rb_check_hash_type(argv[argc-1]);
116
+ if (!NIL_P(vopts)) {
117
+ argv[argc-1] = vopts;
118
+ vopts = rb_extract_keywords(&argv[argc-1]);
119
+ if (!argv[argc-1]) *argcp = --argc;
120
+ if (!vopts) vopts = Qnil;
121
+ }
122
+ }
123
+ rb_check_arity(argc, min_argc, max_argc);
111
124
  if (!NIL_P(vopts)) {
112
125
  VALUE vmin = rb_hash_aref(vopts, ID2SYM(id_min));
113
126
  VALUE vtime = rb_hash_aref(vopts, ID2SYM(id_time));
127
+ VALUE intr = rb_hash_aref(vopts, ID2SYM(id_intr));
114
128
  /* default values by `stty raw` */
115
129
  opts->vmin = 1;
116
130
  opts->vtime = 0;
131
+ opts->intr = 0;
117
132
  if (!NIL_P(vmin)) {
118
133
  opts->vmin = NUM2INT(vmin);
119
134
  optp = opts;
@@ -124,6 +139,21 @@ rawmode_opt(int argc, VALUE *argv, rawmode_arg_t *opts)
124
139
  opts->vtime = NUM2INT(vtime);
125
140
  optp = opts;
126
141
  }
142
+ switch (intr) {
143
+ case Qtrue:
144
+ opts->intr = 1;
145
+ optp = opts;
146
+ break;
147
+ case Qfalse:
148
+ opts->intr = 0;
149
+ optp = opts;
150
+ break;
151
+ case Qnil:
152
+ break;
153
+ default:
154
+ rb_raise(rb_eArgError, "true or false expected as intr: %"PRIsVALUE,
155
+ intr);
156
+ }
127
157
  }
128
158
  return optp;
129
159
  }
@@ -146,13 +176,22 @@ set_rawmode(conmode *t, void *arg)
146
176
  #elif defined _WIN32
147
177
  *t = 0;
148
178
  #endif
149
- #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
150
179
  if (arg) {
151
180
  const rawmode_arg_t *r = arg;
181
+ #ifdef VMIN
152
182
  if (r->vmin >= 0) t->c_cc[VMIN] = r->vmin;
183
+ #endif
184
+ #ifdef VTIME
153
185
  if (r->vtime >= 0) t->c_cc[VTIME] = r->vtime;
154
- }
155
186
  #endif
187
+ #ifdef ISIG
188
+ if (r->intr) {
189
+ t->c_iflag |= BRKINT|IXON;
190
+ t->c_lflag |= ISIG;
191
+ }
192
+ #endif
193
+ (void)r;
194
+ }
156
195
  }
157
196
 
158
197
  static void
@@ -232,7 +271,7 @@ get_write_fd(const rb_io_t *fptr)
232
271
  #define FD_PER_IO 2
233
272
 
234
273
  static VALUE
235
- ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void *arg)
274
+ ttymode(VALUE io, VALUE (*func)(VALUE), VALUE farg, void (*setter)(conmode *, void *), void *arg)
236
275
  {
237
276
  rb_io_t *fptr;
238
277
  int status = -1;
@@ -263,7 +302,7 @@ ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void
263
302
  }
264
303
  }
265
304
  if (status == 0) {
266
- result = rb_protect(func, io, &status);
305
+ result = rb_protect(func, farg, &status);
267
306
  }
268
307
  GetOpenFile(io, fptr);
269
308
  if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) {
@@ -287,6 +326,31 @@ ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void
287
326
  return result;
288
327
  }
289
328
 
329
+ #if !defined _WIN32
330
+ struct ttymode_callback_args {
331
+ VALUE (*func)(VALUE, VALUE);
332
+ VALUE io;
333
+ VALUE farg;
334
+ };
335
+
336
+ static VALUE
337
+ ttymode_callback(VALUE args)
338
+ {
339
+ struct ttymode_callback_args *argp = (struct ttymode_callback_args *)args;
340
+ return argp->func(argp->io, argp->farg);
341
+ }
342
+
343
+ static VALUE
344
+ ttymode_with_io(VALUE io, VALUE (*func)(VALUE, VALUE), VALUE farg, void (*setter)(conmode *, void *), void *arg)
345
+ {
346
+ struct ttymode_callback_args cargs;
347
+ cargs.func = func;
348
+ cargs.io = io;
349
+ cargs.farg = farg;
350
+ return ttymode(io, ttymode_callback, (VALUE)&cargs, setter, arg);
351
+ }
352
+ #endif
353
+
290
354
  /*
291
355
  * call-seq:
292
356
  * io.raw(min: nil, time: nil) {|io| }
@@ -310,8 +374,8 @@ ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void
310
374
  static VALUE
311
375
  console_raw(int argc, VALUE *argv, VALUE io)
312
376
  {
313
- rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
314
- return ttymode(io, rb_yield, set_rawmode, optp);
377
+ rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
378
+ return ttymode(io, rb_yield, io, set_rawmode, optp);
315
379
  }
316
380
 
317
381
  /*
@@ -332,7 +396,7 @@ console_set_raw(int argc, VALUE *argv, VALUE io)
332
396
  conmode t;
333
397
  rb_io_t *fptr;
334
398
  int fd;
335
- rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
399
+ rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
336
400
 
337
401
  GetOpenFile(io, fptr);
338
402
  fd = GetReadFD(fptr);
@@ -357,7 +421,7 @@ console_set_raw(int argc, VALUE *argv, VALUE io)
357
421
  static VALUE
358
422
  console_cooked(VALUE io)
359
423
  {
360
- return ttymode(io, rb_yield, set_cookedmode, NULL);
424
+ return ttymode(io, rb_yield, io, set_cookedmode, NULL);
361
425
  }
362
426
 
363
427
  /*
@@ -385,11 +449,34 @@ console_set_cooked(VALUE io)
385
449
  return io;
386
450
  }
387
451
 
452
+ #ifndef _WIN32
388
453
  static VALUE
389
454
  getc_call(VALUE io)
390
455
  {
391
456
  return rb_funcallv(io, id_getc, 0, 0);
392
457
  }
458
+ #else
459
+ static void *
460
+ nogvl_getch(void *p)
461
+ {
462
+ int len = 0;
463
+ wint_t *buf = p, c = _getwch();
464
+
465
+ switch (c) {
466
+ case WEOF:
467
+ break;
468
+ case 0x00:
469
+ case 0xe0:
470
+ buf[len++] = c;
471
+ c = _getwch();
472
+ /* fall through */
473
+ default:
474
+ buf[len++] = c;
475
+ break;
476
+ }
477
+ return (void *)(VALUE)len;
478
+ }
479
+ #endif
393
480
 
394
481
  /*
395
482
  * call-seq:
@@ -404,8 +491,56 @@ getc_call(VALUE io)
404
491
  static VALUE
405
492
  console_getch(int argc, VALUE *argv, VALUE io)
406
493
  {
407
- rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
408
- return ttymode(io, getc_call, set_rawmode, optp);
494
+ rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
495
+ #ifndef _WIN32
496
+ return ttymode(io, getc_call, io, set_rawmode, optp);
497
+ #else
498
+ rb_io_t *fptr;
499
+ VALUE str;
500
+ wint_t c;
501
+ int w, len;
502
+ char buf[8];
503
+ wint_t wbuf[2];
504
+ struct timeval *to = NULL, tv;
505
+
506
+ GetOpenFile(io, fptr);
507
+ if (optp) {
508
+ if (optp->vtime) {
509
+ to = &tv;
510
+ tv.tv_sec = optp->vtime / 10;
511
+ tv.tv_usec = (optp->vtime % 10) * 100000;
512
+ }
513
+ if (optp->vmin != 1) {
514
+ rb_warning("min option ignored");
515
+ }
516
+ if (optp->intr) {
517
+ w = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, to);
518
+ if (w < 0) rb_eof_error();
519
+ if (!(w & RB_WAITFD_IN)) return Qnil;
520
+ }
521
+ else {
522
+ rb_warning("vtime option ignored if intr flag is unset");
523
+ }
524
+ }
525
+ len = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getch, wbuf, RUBY_UBF_IO, 0);
526
+ switch (len) {
527
+ case 0:
528
+ return Qnil;
529
+ case 2:
530
+ buf[0] = (char)wbuf[0];
531
+ c = wbuf[1];
532
+ len = 1;
533
+ do {
534
+ buf[len++] = (unsigned char)c;
535
+ } while ((c >>= CHAR_BIT) && len < (int)sizeof(buf));
536
+ return rb_str_new(buf, len);
537
+ default:
538
+ c = wbuf[0];
539
+ len = rb_uv_to_utf8(buf, c);
540
+ str = rb_utf8_str_new(buf, len);
541
+ return rb_str_conv_enc(str, NULL, rb_default_external_encoding());
542
+ }
543
+ #endif
409
544
  }
410
545
 
411
546
  /*
@@ -423,7 +558,7 @@ console_getch(int argc, VALUE *argv, VALUE io)
423
558
  static VALUE
424
559
  console_noecho(VALUE io)
425
560
  {
426
- return ttymode(io, rb_yield, set_noecho, NULL);
561
+ return ttymode(io, rb_yield, io, set_noecho, NULL);
427
562
  }
428
563
 
429
564
  /*
@@ -475,6 +610,115 @@ console_echo_p(VALUE io)
475
610
  return echo_p(&t) ? Qtrue : Qfalse;
476
611
  }
477
612
 
613
+ static const rb_data_type_t conmode_type = {
614
+ "console-mode",
615
+ {0, RUBY_TYPED_DEFAULT_FREE,},
616
+ 0, 0,
617
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
618
+ };
619
+ static VALUE cConmode;
620
+
621
+ static VALUE
622
+ conmode_alloc(VALUE klass)
623
+ {
624
+ return rb_data_typed_object_zalloc(klass, sizeof(conmode), &conmode_type);
625
+ }
626
+
627
+ static VALUE
628
+ conmode_new(VALUE klass, const conmode *t)
629
+ {
630
+ VALUE obj = conmode_alloc(klass);
631
+ *(conmode *)DATA_PTR(obj) = *t;
632
+ return obj;
633
+ }
634
+
635
+ static VALUE
636
+ conmode_init_copy(VALUE obj, VALUE obj2)
637
+ {
638
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
639
+ conmode *t2 = rb_check_typeddata(obj2, &conmode_type);
640
+ *t = *t2;
641
+ return obj;
642
+ }
643
+
644
+ static VALUE
645
+ conmode_set_echo(VALUE obj, VALUE f)
646
+ {
647
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
648
+ if (RTEST(f))
649
+ set_echo(t, NULL);
650
+ else
651
+ set_noecho(t, NULL);
652
+ return obj;
653
+ }
654
+
655
+ static VALUE
656
+ conmode_set_raw(int argc, VALUE *argv, VALUE obj)
657
+ {
658
+ conmode *t = rb_check_typeddata(obj, &conmode_type);
659
+ rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
660
+
661
+ set_rawmode(t, optp);
662
+ return obj;
663
+ }
664
+
665
+ static VALUE
666
+ conmode_raw_new(int argc, VALUE *argv, VALUE obj)
667
+ {
668
+ conmode *r = rb_check_typeddata(obj, &conmode_type);
669
+ conmode t = *r;
670
+ rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
671
+
672
+ set_rawmode(&t, optp);
673
+ return conmode_new(rb_obj_class(obj), &t);
674
+ }
675
+
676
+ /*
677
+ * call-seq:
678
+ * io.console_mode -> mode
679
+ *
680
+ * Returns a data represents the current console mode.
681
+ *
682
+ * You must require 'io/console' to use this method.
683
+ */
684
+ static VALUE
685
+ console_conmode_get(VALUE io)
686
+ {
687
+ conmode t;
688
+ rb_io_t *fptr;
689
+ int fd;
690
+
691
+ GetOpenFile(io, fptr);
692
+ fd = GetReadFD(fptr);
693
+ if (!getattr(fd, &t)) rb_sys_fail(0);
694
+
695
+ return conmode_new(cConmode, &t);
696
+ }
697
+
698
+ /*
699
+ * call-seq:
700
+ * io.console_mode = mode
701
+ *
702
+ * Sets the console mode to +mode+.
703
+ *
704
+ * You must require 'io/console' to use this method.
705
+ */
706
+ static VALUE
707
+ console_conmode_set(VALUE io, VALUE mode)
708
+ {
709
+ conmode *t, r;
710
+ rb_io_t *fptr;
711
+ int fd;
712
+
713
+ TypedData_Get_Struct(mode, conmode, &conmode_type, t);
714
+ r = *t;
715
+ GetOpenFile(io, fptr);
716
+ fd = GetReadFD(fptr);
717
+ if (!setattr(fd, &r)) rb_sys_fail(0);
718
+
719
+ return mode;
720
+ }
721
+
478
722
  #if defined TIOCGWINSZ
479
723
  typedef struct winsize rb_console_size_t;
480
724
  #define getwinsize(fd, buf) (ioctl((fd), TIOCGWINSZ, (buf)) == 0)
@@ -593,6 +837,30 @@ console_set_winsize(VALUE io, VALUE size)
593
837
  }
594
838
  #endif
595
839
 
840
+ #ifdef _WIN32
841
+ static VALUE
842
+ console_check_winsize_changed(VALUE io)
843
+ {
844
+ rb_io_t *fptr;
845
+ HANDLE h;
846
+ DWORD num;
847
+
848
+ GetOpenFile(io, fptr);
849
+ h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(fptr));
850
+ while (GetNumberOfConsoleInputEvents(h, &num) && num > 0) {
851
+ INPUT_RECORD rec;
852
+ if (ReadConsoleInput(h, &rec, 1, &num)) {
853
+ if (rec.EventType == WINDOW_BUFFER_SIZE_EVENT) {
854
+ rb_yield(Qnil);
855
+ }
856
+ }
857
+ }
858
+ return io;
859
+ }
860
+ #else
861
+ #define console_check_winsize_changed rb_f_notimplement
862
+ #endif
863
+
596
864
  /*
597
865
  * call-seq:
598
866
  * io.iflush
@@ -688,9 +956,24 @@ console_beep(VALUE io)
688
956
  return io;
689
957
  }
690
958
 
959
+ static int
960
+ mode_in_range(VALUE val, int high, const char *modename)
961
+ {
962
+ int mode;
963
+ if (NIL_P(val)) return 0;
964
+ if (!RB_INTEGER_TYPE_P(val)) {
965
+ wrong_value:
966
+ rb_raise(rb_eArgError, "wrong %s mode: %"PRIsVALUE, modename, val);
967
+ }
968
+ if ((mode = NUM2INT(val)) < 0 || mode > high) {
969
+ goto wrong_value;
970
+ }
971
+ return mode;
972
+ }
973
+
691
974
  #if defined _WIN32
692
975
  static VALUE
693
- console_goto(VALUE io, VALUE x, VALUE y)
976
+ console_goto(VALUE io, VALUE y, VALUE x)
694
977
  {
695
978
  rb_io_t *fptr;
696
979
  int fd;
@@ -718,15 +1001,159 @@ console_cursor_pos(VALUE io)
718
1001
  if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) {
719
1002
  rb_syserr_fail(LAST_ERROR, 0);
720
1003
  }
721
- return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.X), UINT2NUM(ws.dwCursorPosition.Y));
1004
+ return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.Y), UINT2NUM(ws.dwCursorPosition.X));
722
1005
  }
723
1006
 
724
1007
  static VALUE
725
- console_cursor_set(VALUE io, VALUE cpos)
1008
+ console_move(VALUE io, int y, int x)
726
1009
  {
727
- cpos = rb_convert_type(cpos, T_ARRAY, "Array", "to_ary");
728
- if (RARRAY_LEN(cpos) != 2) rb_raise(rb_eArgError, "expected 2D coordinate");
729
- return console_goto(io, RARRAY_AREF(cpos, 0), RARRAY_AREF(cpos, 1));
1010
+ rb_io_t *fptr;
1011
+ HANDLE h;
1012
+ rb_console_size_t ws;
1013
+ COORD *pos = &ws.dwCursorPosition;
1014
+
1015
+ GetOpenFile(io, fptr);
1016
+ h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
1017
+ if (!GetConsoleScreenBufferInfo(h, &ws)) {
1018
+ rb_syserr_fail(LAST_ERROR, 0);
1019
+ }
1020
+ pos->X += x;
1021
+ pos->Y += y;
1022
+ if (!SetConsoleCursorPosition(h, *pos)) {
1023
+ rb_syserr_fail(LAST_ERROR, 0);
1024
+ }
1025
+ return io;
1026
+ }
1027
+
1028
+ static VALUE
1029
+ console_goto_column(VALUE io, VALUE val)
1030
+ {
1031
+ rb_io_t *fptr;
1032
+ HANDLE h;
1033
+ rb_console_size_t ws;
1034
+ COORD *pos = &ws.dwCursorPosition;
1035
+
1036
+ GetOpenFile(io, fptr);
1037
+ h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
1038
+ if (!GetConsoleScreenBufferInfo(h, &ws)) {
1039
+ rb_syserr_fail(LAST_ERROR, 0);
1040
+ }
1041
+ pos->X = NUM2INT(val);
1042
+ if (!SetConsoleCursorPosition(h, *pos)) {
1043
+ rb_syserr_fail(LAST_ERROR, 0);
1044
+ }
1045
+ return io;
1046
+ }
1047
+
1048
+ static void
1049
+ constat_clear(HANDLE handle, WORD attr, DWORD len, COORD pos)
1050
+ {
1051
+ DWORD written;
1052
+
1053
+ FillConsoleOutputAttribute(handle, attr, len, pos, &written);
1054
+ FillConsoleOutputCharacterW(handle, L' ', len, pos, &written);
1055
+ }
1056
+
1057
+ static VALUE
1058
+ console_erase_line(VALUE io, VALUE val)
1059
+ {
1060
+ rb_io_t *fptr;
1061
+ HANDLE h;
1062
+ rb_console_size_t ws;
1063
+ COORD *pos = &ws.dwCursorPosition;
1064
+ DWORD w;
1065
+ int mode = mode_in_range(val, 2, "line erase");
1066
+
1067
+ GetOpenFile(io, fptr);
1068
+ h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
1069
+ if (!GetConsoleScreenBufferInfo(h, &ws)) {
1070
+ rb_syserr_fail(LAST_ERROR, 0);
1071
+ }
1072
+ w = winsize_col(&ws);
1073
+ switch (mode) {
1074
+ case 0: /* after cursor */
1075
+ w -= pos->X;
1076
+ break;
1077
+ case 1: /* before *and* cursor */
1078
+ w = pos->X + 1;
1079
+ pos->X = 0;
1080
+ break;
1081
+ case 2: /* entire line */
1082
+ pos->X = 0;
1083
+ break;
1084
+ }
1085
+ constat_clear(h, ws.wAttributes, w, *pos);
1086
+ return io;
1087
+ }
1088
+
1089
+ static VALUE
1090
+ console_erase_screen(VALUE io, VALUE val)
1091
+ {
1092
+ rb_io_t *fptr;
1093
+ HANDLE h;
1094
+ rb_console_size_t ws;
1095
+ COORD *pos = &ws.dwCursorPosition;
1096
+ DWORD w;
1097
+ int mode = mode_in_range(val, 3, "screen erase");
1098
+
1099
+ GetOpenFile(io, fptr);
1100
+ h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
1101
+ if (!GetConsoleScreenBufferInfo(h, &ws)) {
1102
+ rb_syserr_fail(LAST_ERROR, 0);
1103
+ }
1104
+ w = winsize_col(&ws);
1105
+ switch (mode) {
1106
+ case 0: /* erase after cursor */
1107
+ w = (w * (ws.srWindow.Bottom - pos->Y + 1) - pos->X);
1108
+ break;
1109
+ case 1: /* erase before *and* cursor */
1110
+ w = (w * (pos->Y - ws.srWindow.Top) + pos->X + 1);
1111
+ pos->X = 0;
1112
+ pos->Y = ws.srWindow.Top;
1113
+ break;
1114
+ case 2: /* erase entire screen */
1115
+ w = (w * winsize_row(&ws));
1116
+ pos->X = 0;
1117
+ pos->Y = ws.srWindow.Top;
1118
+ break;
1119
+ case 3: /* erase entire screen */
1120
+ w = (w * ws.dwSize.Y);
1121
+ pos->X = 0;
1122
+ pos->Y = 0;
1123
+ break;
1124
+ }
1125
+ constat_clear(h, ws.wAttributes, w, *pos);
1126
+ return io;
1127
+ }
1128
+
1129
+ static VALUE
1130
+ console_scroll(VALUE io, int line)
1131
+ {
1132
+ rb_io_t *fptr;
1133
+ HANDLE h;
1134
+ rb_console_size_t ws;
1135
+
1136
+ GetOpenFile(io, fptr);
1137
+ h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
1138
+ if (!GetConsoleScreenBufferInfo(h, &ws)) {
1139
+ rb_syserr_fail(LAST_ERROR, 0);
1140
+ }
1141
+ if (line) {
1142
+ SMALL_RECT scroll;
1143
+ COORD destination;
1144
+ CHAR_INFO fill;
1145
+ scroll.Left = 0;
1146
+ scroll.Top = line > 0 ? line : 0;
1147
+ scroll.Right = winsize_col(&ws) - 1;
1148
+ scroll.Bottom = winsize_row(&ws) - 1 + (line < 0 ? line : 0);
1149
+ destination.X = 0;
1150
+ destination.Y = line < 0 ? -line : 0;
1151
+ fill.Char.UnicodeChar = L' ';
1152
+ fill.Attributes = ws.wAttributes;
1153
+
1154
+ ScrollConsoleScreenBuffer(h, &scroll, NULL, destination, &fill);
1155
+ }
1156
+ return io;
730
1157
  }
731
1158
 
732
1159
  #include "win32_vk.inc"
@@ -757,12 +1184,210 @@ console_key_pressed_p(VALUE io, VALUE k)
757
1184
  return GetKeyState(vk) & 0x80 ? Qtrue : Qfalse;
758
1185
  }
759
1186
  #else
760
- # define console_goto rb_f_notimplement
761
- # define console_cursor_pos rb_f_notimplement
762
- # define console_cursor_set rb_f_notimplement
1187
+ struct query_args {
1188
+ const char *qstr;
1189
+ int opt;
1190
+ };
1191
+
1192
+ static int
1193
+ direct_query(VALUE io, const struct query_args *query)
1194
+ {
1195
+ if (RB_TYPE_P(io, T_FILE)) {
1196
+ rb_io_t *fptr;
1197
+ VALUE wio;
1198
+ GetOpenFile(io, fptr);
1199
+ wio = fptr->tied_io_for_writing;
1200
+ if (wio) {
1201
+ VALUE s = rb_str_new_cstr(query->qstr);
1202
+ rb_io_write(wio, s);
1203
+ rb_io_flush(wio);
1204
+ return 1;
1205
+ }
1206
+ if (write(fptr->fd, query->qstr, strlen(query->qstr)) != -1) {
1207
+ return 1;
1208
+ }
1209
+ if (fptr->fd == 0 &&
1210
+ write(1, query->qstr, strlen(query->qstr)) != -1) {
1211
+ return 1;
1212
+ }
1213
+ }
1214
+ return 0;
1215
+ }
1216
+
1217
+ static VALUE
1218
+ read_vt_response(VALUE io, VALUE query)
1219
+ {
1220
+ struct query_args *qargs = (struct query_args *)query;
1221
+ VALUE result, b;
1222
+ int opt = 0;
1223
+ int num = 0;
1224
+ if (qargs) {
1225
+ opt = qargs->opt;
1226
+ if (!direct_query(io, qargs)) return Qnil;
1227
+ }
1228
+ if (rb_io_getbyte(io) != INT2FIX(0x1b)) return Qnil;
1229
+ if (rb_io_getbyte(io) != INT2FIX('[')) return Qnil;
1230
+ result = rb_ary_new();
1231
+ while (!NIL_P(b = rb_io_getbyte(io))) {
1232
+ int c = NUM2UINT(b);
1233
+ if (c == ';') {
1234
+ rb_ary_push(result, INT2NUM(num));
1235
+ num = 0;
1236
+ }
1237
+ else if (ISDIGIT(c)) {
1238
+ num = num * 10 + c - '0';
1239
+ }
1240
+ else if (opt && c == opt) {
1241
+ opt = 0;
1242
+ }
1243
+ else {
1244
+ char last = (char)c;
1245
+ rb_ary_push(result, INT2NUM(num));
1246
+ b = rb_str_new(&last, 1);
1247
+ break;
1248
+ }
1249
+ }
1250
+ return rb_ary_push(result, b);
1251
+ }
1252
+
1253
+ static VALUE
1254
+ console_vt_response(int argc, VALUE *argv, VALUE io, const struct query_args *qargs)
1255
+ {
1256
+ rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 1, &opts);
1257
+ VALUE query = (VALUE)qargs;
1258
+ VALUE ret = ttymode_with_io(io, read_vt_response, query, set_rawmode, optp);
1259
+ return ret;
1260
+ }
1261
+
1262
+ static VALUE
1263
+ console_cursor_pos(VALUE io)
1264
+ {
1265
+ static const struct query_args query = {"\033[6n", 0};
1266
+ VALUE resp = console_vt_response(0, 0, io, &query);
1267
+ VALUE row, column, term;
1268
+ unsigned int r, c;
1269
+ if (!RB_TYPE_P(resp, T_ARRAY) || RARRAY_LEN(resp) != 3) return Qnil;
1270
+ term = RARRAY_AREF(resp, 2);
1271
+ if (!RB_TYPE_P(term, T_STRING) || RSTRING_LEN(term) != 1) return Qnil;
1272
+ if (RSTRING_PTR(term)[0] != 'R') return Qnil;
1273
+ row = RARRAY_AREF(resp, 0);
1274
+ column = RARRAY_AREF(resp, 1);
1275
+ rb_ary_resize(resp, 2);
1276
+ r = NUM2UINT(row) - 1;
1277
+ c = NUM2UINT(column) - 1;
1278
+ RARRAY_ASET(resp, 0, INT2NUM(r));
1279
+ RARRAY_ASET(resp, 1, INT2NUM(c));
1280
+ return resp;
1281
+ }
1282
+
1283
+ static VALUE
1284
+ console_goto(VALUE io, VALUE y, VALUE x)
1285
+ {
1286
+ rb_io_write(io, rb_sprintf("\x1b[%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1));
1287
+ return io;
1288
+ }
1289
+
1290
+ static VALUE
1291
+ console_move(VALUE io, int y, int x)
1292
+ {
1293
+ if (x || y) {
1294
+ VALUE s = rb_str_new_cstr("");
1295
+ if (y) rb_str_catf(s, "\x1b[%d%c", y < 0 ? -y : y, y < 0 ? 'A' : 'B');
1296
+ if (x) rb_str_catf(s, "\x1b[%d%c", x < 0 ? -x : x, x < 0 ? 'D' : 'C');
1297
+ rb_io_write(io, s);
1298
+ rb_io_flush(io);
1299
+ }
1300
+ return io;
1301
+ }
1302
+
1303
+ static VALUE
1304
+ console_goto_column(VALUE io, VALUE val)
1305
+ {
1306
+ rb_io_write(io, rb_sprintf("\x1b[%dG", NUM2UINT(val)+1));
1307
+ return io;
1308
+ }
1309
+
1310
+ static VALUE
1311
+ console_erase_line(VALUE io, VALUE val)
1312
+ {
1313
+ int mode = mode_in_range(val, 2, "line erase");
1314
+ rb_io_write(io, rb_sprintf("\x1b[%dK", mode));
1315
+ return io;
1316
+ }
1317
+
1318
+ static VALUE
1319
+ console_erase_screen(VALUE io, VALUE val)
1320
+ {
1321
+ int mode = mode_in_range(val, 3, "screen erase");
1322
+ rb_io_write(io, rb_sprintf("\x1b[%dJ", mode));
1323
+ return io;
1324
+ }
1325
+
1326
+ static VALUE
1327
+ console_scroll(VALUE io, int line)
1328
+ {
1329
+ if (line) {
1330
+ VALUE s = rb_sprintf("\x1b[%d%c", line < 0 ? -line : line,
1331
+ line < 0 ? 'T' : 'S');
1332
+ rb_io_write(io, s);
1333
+ }
1334
+ return io;
1335
+ }
763
1336
  # define console_key_pressed_p rb_f_notimplement
764
1337
  #endif
765
1338
 
1339
+ static VALUE
1340
+ console_cursor_set(VALUE io, VALUE cpos)
1341
+ {
1342
+ cpos = rb_convert_type(cpos, T_ARRAY, "Array", "to_ary");
1343
+ if (RARRAY_LEN(cpos) != 2) rb_raise(rb_eArgError, "expected 2D coordinate");
1344
+ return console_goto(io, RARRAY_AREF(cpos, 0), RARRAY_AREF(cpos, 1));
1345
+ }
1346
+
1347
+ static VALUE
1348
+ console_cursor_up(VALUE io, VALUE val)
1349
+ {
1350
+ return console_move(io, -NUM2INT(val), 0);
1351
+ }
1352
+
1353
+ static VALUE
1354
+ console_cursor_down(VALUE io, VALUE val)
1355
+ {
1356
+ return console_move(io, +NUM2INT(val), 0);
1357
+ }
1358
+
1359
+ static VALUE
1360
+ console_cursor_left(VALUE io, VALUE val)
1361
+ {
1362
+ return console_move(io, 0, -NUM2INT(val));
1363
+ }
1364
+
1365
+ static VALUE
1366
+ console_cursor_right(VALUE io, VALUE val)
1367
+ {
1368
+ return console_move(io, 0, +NUM2INT(val));
1369
+ }
1370
+
1371
+ static VALUE
1372
+ console_scroll_forward(VALUE io, VALUE val)
1373
+ {
1374
+ return console_scroll(io, +NUM2INT(val));
1375
+ }
1376
+
1377
+ static VALUE
1378
+ console_scroll_backward(VALUE io, VALUE val)
1379
+ {
1380
+ return console_scroll(io, -NUM2INT(val));
1381
+ }
1382
+
1383
+ static VALUE
1384
+ console_clear_screen(VALUE io)
1385
+ {
1386
+ console_erase_screen(io, INT2FIX(2));
1387
+ console_goto(io, INT2FIX(0), INT2FIX(0));
1388
+ return io;
1389
+ }
1390
+
766
1391
  /*
767
1392
  * call-seq:
768
1393
  * IO.console -> #<File:/dev/tty>
@@ -882,7 +1507,7 @@ puts_call(VALUE io)
882
1507
  static VALUE
883
1508
  getpass_call(VALUE io)
884
1509
  {
885
- return ttymode(io, rb_io_gets, set_noecho, NULL);
1510
+ return ttymode(io, rb_io_gets, io, set_noecho, NULL);
886
1511
  }
887
1512
 
888
1513
  static void
@@ -891,7 +1516,6 @@ prompt(int argc, VALUE *argv, VALUE io)
891
1516
  if (argc > 0 && !NIL_P(argv[0])) {
892
1517
  VALUE str = argv[0];
893
1518
  StringValueCStr(str);
894
- rb_check_safe_obj(str);
895
1519
  rb_io_write(io, str);
896
1520
  }
897
1521
  }
@@ -961,6 +1585,7 @@ Init_console(void)
961
1585
  id_close = rb_intern("close");
962
1586
  id_min = rb_intern("min");
963
1587
  id_time = rb_intern("time");
1588
+ id_intr = rb_intern("intr");
964
1589
  #ifndef HAVE_RB_F_SEND
965
1590
  id___send__ = rb_intern("__send__");
966
1591
  #endif
@@ -977,6 +1602,8 @@ InitVM_console(void)
977
1602
  rb_define_method(rb_cIO, "getch", console_getch, -1);
978
1603
  rb_define_method(rb_cIO, "echo=", console_set_echo, 1);
979
1604
  rb_define_method(rb_cIO, "echo?", console_echo_p, 0);
1605
+ rb_define_method(rb_cIO, "console_mode", console_conmode_get, 0);
1606
+ rb_define_method(rb_cIO, "console_mode=", console_conmode_set, 1);
980
1607
  rb_define_method(rb_cIO, "noecho", console_noecho, 0);
981
1608
  rb_define_method(rb_cIO, "winsize", console_winsize, 0);
982
1609
  rb_define_method(rb_cIO, "winsize=", console_set_winsize, 1);
@@ -987,7 +1614,18 @@ InitVM_console(void)
987
1614
  rb_define_method(rb_cIO, "goto", console_goto, 2);
988
1615
  rb_define_method(rb_cIO, "cursor", console_cursor_pos, 0);
989
1616
  rb_define_method(rb_cIO, "cursor=", console_cursor_set, 1);
1617
+ rb_define_method(rb_cIO, "cursor_up", console_cursor_up, 1);
1618
+ rb_define_method(rb_cIO, "cursor_down", console_cursor_down, 1);
1619
+ rb_define_method(rb_cIO, "cursor_left", console_cursor_left, 1);
1620
+ rb_define_method(rb_cIO, "cursor_right", console_cursor_right, 1);
1621
+ rb_define_method(rb_cIO, "goto_column", console_goto_column, 1);
1622
+ rb_define_method(rb_cIO, "erase_line", console_erase_line, 1);
1623
+ rb_define_method(rb_cIO, "erase_screen", console_erase_screen, 1);
1624
+ rb_define_method(rb_cIO, "scroll_forward", console_scroll_forward, 1);
1625
+ rb_define_method(rb_cIO, "scroll_backward", console_scroll_backward, 1);
1626
+ rb_define_method(rb_cIO, "clear_screen", console_clear_screen, 0);
990
1627
  rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1);
1628
+ rb_define_method(rb_cIO, "check_winsize_changed", console_check_winsize_changed, 0);
991
1629
  #if ENABLE_IO_GETPASS
992
1630
  rb_define_method(rb_cIO, "getpass", console_getpass, -1);
993
1631
  #endif
@@ -999,4 +1637,15 @@ InitVM_console(void)
999
1637
  rb_define_method(mReadable, "getpass", io_getpass, -1);
1000
1638
  #endif
1001
1639
  }
1640
+ {
1641
+ /* :stopdoc: */
1642
+ cConmode = rb_define_class_under(rb_cIO, "ConsoleMode", rb_cObject);
1643
+ rb_define_alloc_func(cConmode, conmode_alloc);
1644
+ rb_undef_method(cConmode, "initialize");
1645
+ rb_define_method(cConmode, "initialize_copy", conmode_init_copy, 1);
1646
+ rb_define_method(cConmode, "echo=", conmode_set_echo, 1);
1647
+ rb_define_method(cConmode, "raw!", conmode_set_raw, -1);
1648
+ rb_define_method(cConmode, "raw", conmode_raw_new, -1);
1649
+ /* :startdoc: */
1650
+ }
1002
1651
  }
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-14 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake-compiler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake-compiler-dock
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 0.6.1
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 0.6.1
11
+ date: 2019-12-10 00:00:00.000000000 Z
12
+ dependencies: []
41
13
  description: add console capabilities to IO instances.
42
14
  email: nobu@ruby-lang.org
43
15
  executables: []
@@ -51,7 +23,6 @@ files:
51
23
  - ext/io/console/extconf.rb
52
24
  - ext/io/console/win32_vk.inc
53
25
  - lib/io/console/size.rb
54
- - stub/io/console.rb
55
26
  homepage: https://github.com/ruby/io-console
56
27
  licenses:
57
28
  - BSD-2-Clause
@@ -72,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
43
  - !ruby/object:Gem::Version
73
44
  version: '0'
74
45
  requirements: []
75
- rubygems_version: 3.0.3
46
+ rubygems_version: 3.1.0.pre3
76
47
  signing_key:
77
48
  specification_version: 4
78
49
  summary: Console interface
@@ -1 +0,0 @@
1
- require "#{RUBY_VERSION[/\d+\.\d+/]}/io/console.so"