io-console 0.5.4 → 0.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31d79d8c0729bc693338a79041f2a32b412b556ee9c53b27a8f64187f1d47202
4
- data.tar.gz: b0a73452d3fb90e791ce1b5fa8e3ad3f7656b3826bc39a0260632c4ff8db150c
3
+ metadata.gz: d4124dc490e2ac47dda9ad9283023351e95d781ac40af9a22f012a25c3b3eb15
4
+ data.tar.gz: 9ff304d0831ef2d5486f02c5da57fbe91ae960c4d3f1f28057e8e55a7fc7507a
5
5
  SHA512:
6
- metadata.gz: b4b97beb4cb0b4f3692a26d0ca06eb78f21672426a01848a3a73182d01ea037d7d7ff2bb9b792eacc2e436620a18ce9b7d4c87709b4449cd5293ae96ffdb8281
7
- data.tar.gz: 26330a319d0b643d991bbcbacbef03456000133bcf4adb1ad74500b792dbd84aa512f78e273876bcface1114884b2decaf9ecafbb5e24dbb739cab54b5b36209
6
+ metadata.gz: c8f277a43454e22b0c43beee7eba4069eac9db3b369a4ce0799259d482606a212fbb1d3deae07031703840add83fd5561dfe7170bc62f60675aabf695613e493
7
+ data.tar.gz: dfe52d8ca7becd7cf2aa8a27a56702380c1723bfd12393583cff7ad04c2996671b669476d8fd506c3e703d30b75c4084467ffcb8b0fcd7fe6e2be424f9d57c11
data/README.md CHANGED
@@ -27,7 +27,7 @@ IO.console -> #<File:/dev/tty>
27
27
  IO.console(sym, *args)
28
28
  ```
29
29
 
30
- Returns an File instance opened console.
30
+ Returns a File instance opened console.
31
31
 
32
32
  If `sym` is given, it will be sent to the opened console with `args` and the result will be returned instead of the console IO itself.
33
33
 
@@ -1,4 +1,4 @@
1
- /* -*- c-file-style: "ruby" -*- */
1
+ /* -*- c-file-style: "ruby"; indent-tabs-mode: t -*- */
2
2
  /*
3
3
  * console IO module
4
4
  */
@@ -77,9 +77,18 @@ getattr(int fd, conmode *t)
77
77
 
78
78
  static ID id_getc, id_console, id_close, id_min, id_time, id_intr;
79
79
  #if ENABLE_IO_GETPASS
80
- static ID id_gets;
80
+ static ID id_gets, id_chomp_bang;
81
81
  #endif
82
82
 
83
+ #if defined HAVE_RUBY_FIBER_SCHEDULER_H
84
+ # include "ruby/fiber/scheduler.h"
85
+ #elif defined HAVE_RB_SCHEDULER_TIMEOUT
86
+ extern VALUE rb_scheduler_timeout(struct timeval *timeout);
87
+ # define rb_fiber_scheduler_make_timeout rb_scheduler_timeout
88
+ #endif
89
+
90
+ #define sys_fail_fptr(fptr) rb_sys_fail_str((fptr)->pathv)
91
+
83
92
  #ifndef HAVE_RB_F_SEND
84
93
  static ID id___send__;
85
94
 
@@ -111,6 +120,9 @@ rawmode_opt(int *argcp, VALUE *argv, int min_argc, int max_argc, rawmode_arg_t *
111
120
  int argc = *argcp;
112
121
  rawmode_arg_t *optp = NULL;
113
122
  VALUE vopts = Qnil;
123
+ #ifdef RB_SCAN_ARGS_PASS_CALLED_KEYWORDS
124
+ argc = rb_scan_args(argc, argv, "*:", NULL, &vopts);
125
+ #else
114
126
  if (argc > min_argc) {
115
127
  vopts = rb_check_hash_type(argv[argc-1]);
116
128
  if (!NIL_P(vopts)) {
@@ -120,6 +132,7 @@ rawmode_opt(int *argcp, VALUE *argv, int min_argc, int max_argc, rawmode_arg_t *
120
132
  if (!vopts) vopts = Qnil;
121
133
  }
122
134
  }
135
+ #endif
123
136
  rb_check_arity(argc, min_argc, max_argc);
124
137
  if (!NIL_P(vopts)) {
125
138
  VALUE vmin = rb_hash_aref(vopts, ID2SYM(id_min));
@@ -190,6 +203,7 @@ set_rawmode(conmode *t, void *arg)
190
203
  if (r->intr) {
191
204
  t->c_iflag |= BRKINT;
192
205
  t->c_lflag |= ISIG;
206
+ t->c_oflag |= OPOST;
193
207
  }
194
208
  #endif
195
209
  (void)r;
@@ -355,9 +369,9 @@ ttymode_with_io(VALUE io, VALUE (*func)(VALUE, VALUE), VALUE farg, void (*setter
355
369
 
356
370
  /*
357
371
  * call-seq:
358
- * io.raw(min: nil, time: nil) {|io| }
372
+ * io.raw(min: nil, time: nil, intr: nil) {|io| }
359
373
  *
360
- * Yields +self+ within raw mode.
374
+ * Yields +self+ within raw mode, and returns the result of the block.
361
375
  *
362
376
  * STDIN.raw(&:gets)
363
377
  *
@@ -369,6 +383,9 @@ ttymode_with_io(VALUE io, VALUE (*func)(VALUE, VALUE), VALUE farg, void (*setter
369
383
  * The parameter +time+ specifies the timeout in _seconds_ with a
370
384
  * precision of 1/10 of a second. (default: 0)
371
385
  *
386
+ * If the parameter +intr+ is +true+, enables break, interrupt, quit,
387
+ * and suspend special characters.
388
+ *
372
389
  * Refer to the manual page of termios for further details.
373
390
  *
374
391
  * You must require 'io/console' to use this method.
@@ -382,11 +399,11 @@ console_raw(int argc, VALUE *argv, VALUE io)
382
399
 
383
400
  /*
384
401
  * call-seq:
385
- * io.raw!(min: nil, time: nil)
402
+ * io.raw!(min: nil, time: nil, intr: nil) -> io
386
403
  *
387
- * Enables raw mode.
404
+ * Enables raw mode, and returns +io+.
388
405
  *
389
- * If the terminal mode needs to be back, use io.raw { ... }.
406
+ * If the terminal mode needs to be back, use <code>io.raw { ... }</code>.
390
407
  *
391
408
  * See IO#raw for details on the parameters.
392
409
  *
@@ -402,9 +419,9 @@ console_set_raw(int argc, VALUE *argv, VALUE io)
402
419
 
403
420
  GetOpenFile(io, fptr);
404
421
  fd = GetReadFD(fptr);
405
- if (!getattr(fd, &t)) rb_sys_fail(0);
422
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
406
423
  set_rawmode(&t, optp);
407
- if (!setattr(fd, &t)) rb_sys_fail(0);
424
+ if (!setattr(fd, &t)) sys_fail_fptr(fptr);
408
425
  return io;
409
426
  }
410
427
 
@@ -445,9 +462,9 @@ console_set_cooked(VALUE io)
445
462
 
446
463
  GetOpenFile(io, fptr);
447
464
  fd = GetReadFD(fptr);
448
- if (!getattr(fd, &t)) rb_sys_fail(0);
465
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
449
466
  set_cookedmode(&t, NULL);
450
- if (!setattr(fd, &t)) rb_sys_fail(0);
467
+ if (!setattr(fd, &t)) sys_fail_fptr(fptr);
451
468
  return io;
452
469
  }
453
470
 
@@ -482,7 +499,7 @@ nogvl_getch(void *p)
482
499
 
483
500
  /*
484
501
  * call-seq:
485
- * io.getch(min: nil, time: nil) -> char
502
+ * io.getch(min: nil, time: nil, intr: nil) -> char
486
503
  *
487
504
  * Reads and returns a character in raw mode.
488
505
  *
@@ -500,28 +517,50 @@ console_getch(int argc, VALUE *argv, VALUE io)
500
517
  rb_io_t *fptr;
501
518
  VALUE str;
502
519
  wint_t c;
503
- int w, len;
520
+ int len;
504
521
  char buf[8];
505
522
  wint_t wbuf[2];
523
+ # ifndef HAVE_RB_IO_WAIT
506
524
  struct timeval *to = NULL, tv;
525
+ # else
526
+ VALUE timeout = Qnil;
527
+ # endif
507
528
 
508
529
  GetOpenFile(io, fptr);
509
530
  if (optp) {
510
531
  if (optp->vtime) {
532
+ # ifndef HAVE_RB_IO_WAIT
511
533
  to = &tv;
534
+ # else
535
+ struct timeval tv;
536
+ # endif
512
537
  tv.tv_sec = optp->vtime / 10;
513
538
  tv.tv_usec = (optp->vtime % 10) * 100000;
539
+ # ifdef HAVE_RB_IO_WAIT
540
+ timeout = rb_fiber_scheduler_make_timeout(&tv);
541
+ # endif
514
542
  }
515
- if (optp->vmin != 1) {
516
- rb_warning("min option ignored");
543
+ switch (optp->vmin) {
544
+ case 1: /* default */
545
+ break;
546
+ case 0: /* return nil when timed out */
547
+ if (optp->vtime) break;
548
+ /* fallthru */
549
+ default:
550
+ rb_warning("min option larger than 1 ignored");
517
551
  }
518
552
  if (optp->intr) {
519
- w = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, to);
553
+ # ifndef HAVE_RB_IO_WAIT
554
+ int w = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, to);
520
555
  if (w < 0) rb_eof_error();
521
556
  if (!(w & RB_WAITFD_IN)) return Qnil;
557
+ # else
558
+ VALUE result = rb_io_wait(io, RUBY_IO_READABLE, timeout);
559
+ if (result == Qfalse) return Qnil;
560
+ # endif
522
561
  }
523
- else {
524
- rb_warning("vtime option ignored if intr flag is unset");
562
+ else if (optp->vtime) {
563
+ rb_warning("Non-zero vtime option ignored if intr flag is unset");
525
564
  }
526
565
  }
527
566
  len = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getch, wbuf, RUBY_UBF_IO, 0);
@@ -582,12 +621,12 @@ console_set_echo(VALUE io, VALUE f)
582
621
 
583
622
  GetOpenFile(io, fptr);
584
623
  fd = GetReadFD(fptr);
585
- if (!getattr(fd, &t)) rb_sys_fail(0);
624
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
586
625
  if (RTEST(f))
587
626
  set_echo(&t, NULL);
588
627
  else
589
628
  set_noecho(&t, NULL);
590
- if (!setattr(fd, &t)) rb_sys_fail(0);
629
+ if (!setattr(fd, &t)) sys_fail_fptr(fptr);
591
630
  return io;
592
631
  }
593
632
 
@@ -608,7 +647,7 @@ console_echo_p(VALUE io)
608
647
 
609
648
  GetOpenFile(io, fptr);
610
649
  fd = GetReadFD(fptr);
611
- if (!getattr(fd, &t)) rb_sys_fail(0);
650
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
612
651
  return echo_p(&t) ? Qtrue : Qfalse;
613
652
  }
614
653
 
@@ -692,7 +731,7 @@ console_conmode_get(VALUE io)
692
731
 
693
732
  GetOpenFile(io, fptr);
694
733
  fd = GetReadFD(fptr);
695
- if (!getattr(fd, &t)) rb_sys_fail(0);
734
+ if (!getattr(fd, &t)) sys_fail_fptr(fptr);
696
735
 
697
736
  return conmode_new(cConmode, &t);
698
737
  }
@@ -716,7 +755,7 @@ console_conmode_set(VALUE io, VALUE mode)
716
755
  r = *t;
717
756
  GetOpenFile(io, fptr);
718
757
  fd = GetReadFD(fptr);
719
- if (!setattr(fd, &r)) rb_sys_fail(0);
758
+ if (!setattr(fd, &r)) sys_fail_fptr(fptr);
720
759
 
721
760
  return mode;
722
761
  }
@@ -758,7 +797,7 @@ console_winsize(VALUE io)
758
797
 
759
798
  GetOpenFile(io, fptr);
760
799
  fd = GetWriteFD(fptr);
761
- if (!getwinsize(fd, &ws)) rb_sys_fail(0);
800
+ if (!getwinsize(fd, &ws)) sys_fail_fptr(fptr);
762
801
  return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws)));
763
802
  }
764
803
 
@@ -805,7 +844,7 @@ console_set_winsize(VALUE io, VALUE size)
805
844
  SET(xpixel);
806
845
  SET(ypixel);
807
846
  #undef SET
808
- if (!setwinsize(fd, &ws)) rb_sys_fail(0);
847
+ if (!setwinsize(fd, &ws)) sys_fail_fptr(fptr);
809
848
  #elif defined _WIN32
810
849
  wh = (HANDLE)rb_w32_get_osfhandle(fd);
811
850
  #define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
@@ -880,7 +919,7 @@ console_iflush(VALUE io)
880
919
  GetOpenFile(io, fptr);
881
920
  fd = GetReadFD(fptr);
882
921
  #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
883
- if (tcflush(fd, TCIFLUSH)) rb_sys_fail(0);
922
+ if (tcflush(fd, TCIFLUSH)) sys_fail_fptr(fptr);
884
923
  #endif
885
924
  (void)fd;
886
925
  return io;
@@ -903,7 +942,7 @@ console_oflush(VALUE io)
903
942
  GetOpenFile(io, fptr);
904
943
  fd = GetWriteFD(fptr);
905
944
  #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
906
- if (tcflush(fd, TCOFLUSH)) rb_sys_fail(0);
945
+ if (tcflush(fd, TCOFLUSH)) sys_fail_fptr(fptr);
907
946
  #endif
908
947
  (void)fd;
909
948
  return io;
@@ -930,11 +969,11 @@ console_ioflush(VALUE io)
930
969
  fd1 = GetReadFD(fptr);
931
970
  fd2 = GetWriteFD(fptr);
932
971
  if (fd2 != -1 && fd1 != fd2) {
933
- if (tcflush(fd1, TCIFLUSH)) rb_sys_fail(0);
934
- if (tcflush(fd2, TCOFLUSH)) rb_sys_fail(0);
972
+ if (tcflush(fd1, TCIFLUSH)) sys_fail_fptr(fptr);
973
+ if (tcflush(fd2, TCOFLUSH)) sys_fail_fptr(fptr);
935
974
  }
936
975
  else {
937
- if (tcflush(fd1, TCIOFLUSH)) rb_sys_fail(0);
976
+ if (tcflush(fd1, TCIOFLUSH)) sys_fail_fptr(fptr);
938
977
  }
939
978
  #endif
940
979
  return io;
@@ -953,7 +992,7 @@ console_beep(VALUE io)
953
992
  MessageBeep(0);
954
993
  #else
955
994
  if (write(fd, "\a", 1) < 0)
956
- rb_sys_fail(0);
995
+ sys_fail_fptr(fptr);
957
996
  #endif
958
997
  return io;
959
998
  }
@@ -1187,8 +1226,8 @@ console_key_pressed_p(VALUE io, VALUE k)
1187
1226
  }
1188
1227
  #else
1189
1228
  struct query_args {
1190
- const char *qstr;
1191
- int opt;
1229
+ char qstr[6];
1230
+ unsigned char opt;
1192
1231
  };
1193
1232
 
1194
1233
  static int
@@ -1489,7 +1528,7 @@ console_dev(int argc, VALUE *argv, VALUE klass)
1489
1528
 
1490
1529
  /*
1491
1530
  * call-seq:
1492
- * io.getch(min: nil, time: nil) -> char
1531
+ * io.getch(min: nil, time: nil, intr: nil) -> char
1493
1532
  *
1494
1533
  * See IO#getch.
1495
1534
  */
@@ -1526,7 +1565,7 @@ static VALUE
1526
1565
  str_chomp(VALUE str)
1527
1566
  {
1528
1567
  if (!NIL_P(str)) {
1529
- str = rb_funcallv(str, rb_intern("chomp!"), 0, 0);
1568
+ rb_funcallv(str, id_chomp_bang, 0, 0);
1530
1569
  }
1531
1570
  return str;
1532
1571
  }
@@ -1538,6 +1577,10 @@ str_chomp(VALUE str)
1538
1577
  * Reads and returns a line without echo back.
1539
1578
  * Prints +prompt+ unless it is +nil+.
1540
1579
  *
1580
+ * The newline character that terminates the
1581
+ * read line is removed from the returned string,
1582
+ * see String#chomp!.
1583
+ *
1541
1584
  * You must require 'io/console' to use this method.
1542
1585
  */
1543
1586
  static VALUE
@@ -1582,6 +1625,7 @@ Init_console(void)
1582
1625
  id_getc = rb_intern("getc");
1583
1626
  #if ENABLE_IO_GETPASS
1584
1627
  id_gets = rb_intern("gets");
1628
+ id_chomp_bang = rb_intern("chomp!");
1585
1629
  #endif
1586
1630
  id_console = rb_intern("console");
1587
1631
  id_close = rb_intern("close");
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: false
2
2
  require 'mkmf'
3
3
 
4
- ok = true
4
+ ok = true if RUBY_ENGINE == "ruby" || RUBY_ENGINE == "truffleruby"
5
5
  hdr = nil
6
6
  case
7
7
  when macro_defined?("_WIN32", "")
@@ -14,8 +14,9 @@ when have_header(hdr = "sgtty.h")
14
14
  %w"stty gtty".each {|f| have_func(f, hdr)}
15
15
  else
16
16
  ok = false
17
- end
18
- if ok
17
+ end if ok
18
+ case ok
19
+ when true
19
20
  have_header("sys/ioctl.h") if hdr
20
21
  # rb_check_hash_type: 1.9.3
21
22
  # rb_io_get_write_io: 1.9.1
@@ -23,8 +24,15 @@ if ok
23
24
  # rb_funcallv: 2.1.0
24
25
  # RARRAY_CONST_PTR: 2.1.0
25
26
  # rb_sym2str: 2.2.0
27
+ if have_macro("HAVE_RUBY_FIBER_SCHEDULER_H")
28
+ $defs << "-D""HAVE_RB_IO_WAIT=1"
29
+ elsif have_func("rb_scheduler_timeout") # 3.0
30
+ have_func("rb_io_wait")
31
+ end
26
32
  $defs << "-D""ENABLE_IO_GETPASS=1"
27
33
  create_makefile("io/console") {|conf|
28
34
  conf << "\n""VK_HEADER = #{vk_header}\n"
29
35
  }
36
+ when nil
37
+ File.write("Makefile", dummy_makefile($srcdir).join(""))
30
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-25 00:00:00.000000000 Z
11
+ date: 2021-03-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: add console capabilities to IO instances.
14
14
  email: nobu@ruby-lang.org
@@ -25,10 +25,11 @@ files:
25
25
  - lib/io/console/size.rb
26
26
  homepage: https://github.com/ruby/io-console
27
27
  licenses:
28
+ - Ruby
28
29
  - BSD-2-Clause
29
30
  metadata:
30
31
  source_code_url: https://github.com/ruby/io-console
31
- post_install_message:
32
+ post_install_message:
32
33
  rdoc_options: []
33
34
  require_paths:
34
35
  - lib
@@ -36,15 +37,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
38
  - - ">="
38
39
  - !ruby/object:Gem::Version
39
- version: 2.2.0
40
+ version: 2.4.0
40
41
  required_rubygems_version: !ruby/object:Gem::Requirement
41
42
  requirements:
42
43
  - - ">="
43
44
  - !ruby/object:Gem::Version
44
45
  version: '0'
45
46
  requirements: []
46
- rubygems_version: 3.1.2
47
- signing_key:
47
+ rubygems_version: 3.2.3
48
+ signing_key:
48
49
  specification_version: 4
49
50
  summary: Console interface
50
51
  test_files: []