Class: IO

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Defined Under Namespace

Modules: generic_readable Classes: ConsoleMode

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.console#<File:/dev/tty .console(sym, *args) ⇒ Object

Returns an File instance opened console.

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.

You must require ‘io/console’ to use this method.

Overloads:

  • .console#<File:/dev/tty

    Returns ].

    Returns:

    • (#<File:/dev/tty)

      ]



1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'console/console.c', line 1442

static VALUE
console_dev(int argc, VALUE *argv, VALUE klass)
{
    VALUE con = 0;
    rb_io_t *fptr;
    VALUE sym = 0;

    rb_check_arity(argc, 0, UNLIMITED_ARGUMENTS);
    if (argc) {
	Check_Type(sym = argv[0], T_SYMBOL);
    }
    if (klass == rb_cIO) klass = rb_cFile;
    if (rb_const_defined(klass, id_console)) {
	con = rb_const_get(klass, id_console);
	if (!RB_TYPE_P(con, T_FILE) ||
	    (!(fptr = RFILE(con)->fptr) || GetReadFD(fptr) == -1)) {
	    rb_const_remove(klass, id_console);
	    con = 0;
	}
    }
    if (sym) {
	if (sym == ID2SYM(id_close) && argc == 1) {
	    if (con) {
		rb_io_close(con);
		rb_const_remove(klass, id_console);
		con = 0;
	    }
	    return Qnil;
	}
    }
    if (!con) {
	VALUE args[2];
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H
# define CONSOLE_DEVICE "/dev/tty"
#elif defined _WIN32
# define CONSOLE_DEVICE "con$"
# define CONSOLE_DEVICE_FOR_READING "conin$"
# define CONSOLE_DEVICE_FOR_WRITING "conout$"
#endif
#ifndef CONSOLE_DEVICE_FOR_READING
# define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE
#endif
#ifdef CONSOLE_DEVICE_FOR_WRITING
	VALUE out;
	rb_io_t *ofptr;
#endif
	int fd;

#ifdef CONSOLE_DEVICE_FOR_WRITING
	fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0);
	if (fd < 0) return Qnil;
        rb_update_max_fd(fd);
	args[1] = INT2FIX(O_WRONLY);
	args[0] = INT2NUM(fd);
	out = rb_class_new_instance(2, args, klass);
#endif
	fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0);
	if (fd < 0) {
#ifdef CONSOLE_DEVICE_FOR_WRITING
	    rb_io_close(out);
#endif
	    return Qnil;
	}
        rb_update_max_fd(fd);
	args[1] = INT2FIX(O_RDWR);
	args[0] = INT2NUM(fd);
	con = rb_class_new_instance(2, args, klass);
	GetOpenFile(con, fptr);
	fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE));
#ifdef CONSOLE_DEVICE_FOR_WRITING
	GetOpenFile(out, ofptr);
	ofptr->pathv = fptr->pathv;
	fptr->tied_io_for_writing = out;
	ofptr->mode |= FMODE_SYNC;
#endif
	fptr->mode |= FMODE_SYNC;
	rb_const_set(klass, id_console, con);
    }
    if (sym) {
	return rb_f_send(argc, argv, con);
    }
    return con;
}

.default_console_sizeObject

fallback to console window size



3
4
5
6
7
8
# File 'console/lib/console/size.rb', line 3

def IO.default_console_size
  [
    ENV["LINES"].to_i.nonzero? || 25,
    ENV["COLUMNS"].to_i.nonzero? || 80,
  ]
end

Instance Method Details

#beepObject



979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'console/console.c', line 979

static VALUE
console_beep(VALUE io)
{
    rb_io_t *fptr;
    int fd;

    GetOpenFile(io, fptr);
    fd = GetWriteFD(fptr);
#ifdef _WIN32
    (void)fd;
    MessageBeep(0);
#else
    if (write(fd, "\a", 1) < 0)
	sys_fail_fptr(fptr);
#endif
    return io;
}

#check_winsize_changedObject



879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'console/console.c', line 879

static VALUE
console_check_winsize_changed(VALUE io)
{
    rb_io_t *fptr;
    HANDLE h;
    DWORD num;

    GetOpenFile(io, fptr);
    h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(fptr));
    while (GetNumberOfConsoleInputEvents(h, &num) && num > 0) {
	INPUT_RECORD rec;
	if (ReadConsoleInput(h, &rec, 1, &num)) {
	    if (rec.EventType == WINDOW_BUFFER_SIZE_EVENT) {
		rb_yield(Qnil);
	    }
	}
    }
    return io;
}

#clear_screenObject



1421
1422
1423
1424
1425
1426
1427
# File 'console/console.c', line 1421

static VALUE
console_clear_screen(VALUE io)
{
    console_erase_screen(io, INT2FIX(2));
    console_goto(io, INT2FIX(0), INT2FIX(0));
    return io;
}

#console_modeObject

Returns a data represents the current console mode.

You must require ‘io/console’ to use this method.



722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'console/console.c', line 722

static VALUE
console_conmode_get(VALUE io)
{
    conmode t;
    rb_io_t *fptr;
    int fd;

    GetOpenFile(io, fptr);
    fd = GetReadFD(fptr);
    if (!getattr(fd, &t)) sys_fail_fptr(fptr);

    return conmode_new(cConmode, &t);
}

#console_mode=(mode) ⇒ Object

Sets the console mode to mode.

You must require ‘io/console’ to use this method.



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'console/console.c', line 744

static VALUE
console_conmode_set(VALUE io, VALUE mode)
{
    conmode *t, r;
    rb_io_t *fptr;
    int fd;

    TypedData_Get_Struct(mode, conmode, &conmode_type, t);
    r = *t;
    GetOpenFile(io, fptr);
    fd = GetReadFD(fptr);
    if (!setattr(fd, &r)) sys_fail_fptr(fptr);

    return mode;
}

#cooked {|io| ... } ⇒ Object

Yields self within cooked mode.

STDIN.cooked(&:gets)

will read and return a line with echo back and line editing.

You must require ‘io/console’ to use this method.

Yields:

  • (io)


437
438
439
440
441
# File 'console/console.c', line 437

static VALUE
console_cooked(VALUE io)
{
    return ttymode(io, rb_yield, io, set_cookedmode, NULL);
}

#cooked!Object

Enables cooked mode.

If the terminal mode needs to be back, use io.cooked { … }.

You must require ‘io/console’ to use this method.



453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'console/console.c', line 453

static VALUE
console_set_cooked(VALUE io)
{
    conmode t;
    rb_io_t *fptr;
    int fd;

    GetOpenFile(io, fptr);
    fd = GetReadFD(fptr);
    if (!getattr(fd, &t)) sys_fail_fptr(fptr);
    set_cookedmode(&t, NULL);
    if (!setattr(fd, &t)) sys_fail_fptr(fptr);
    return io;
}

#cursorObject



1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
# File 'console/console.c', line 1300

static VALUE
console_cursor_pos(VALUE io)
{
    static const struct query_args query = {"\033[6n", 0};
    VALUE resp = console_vt_response(0, 0, io, &query);
    VALUE row, column, term;
    unsigned int r, c;
    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);
    r = NUM2UINT(row) - 1;
    c = NUM2UINT(column) - 1;
    RARRAY_ASET(resp, 0, INT2NUM(r));
    RARRAY_ASET(resp, 1, INT2NUM(c));
    return resp;
}

#cursor=(cpos) ⇒ Object



1377
1378
1379
1380
1381
1382
1383
# File 'console/console.c', line 1377

static VALUE
console_cursor_set(VALUE io, VALUE cpos)
{
    cpos = rb_convert_type(cpos, T_ARRAY, "Array", "to_ary");
    if (RARRAY_LEN(cpos) != 2) rb_raise(rb_eArgError, "expected 2D coordinate");
    return console_goto(io, RARRAY_AREF(cpos, 0), RARRAY_AREF(cpos, 1));
}

#cursor_down(val) ⇒ Object



1391
1392
1393
1394
1395
# File 'console/console.c', line 1391

static VALUE
console_cursor_down(VALUE io, VALUE val)
{
    return console_move(io, +NUM2INT(val), 0);
}

#cursor_left(val) ⇒ Object



1397
1398
1399
1400
1401
# File 'console/console.c', line 1397

static VALUE
console_cursor_left(VALUE io, VALUE val)
{
    return console_move(io, 0, -NUM2INT(val));
}

#cursor_right(val) ⇒ Object



1403
1404
1405
1406
1407
# File 'console/console.c', line 1403

static VALUE
console_cursor_right(VALUE io, VALUE val)
{
    return console_move(io, 0, +NUM2INT(val));
}

#cursor_up(val) ⇒ Object



1385
1386
1387
1388
1389
# File 'console/console.c', line 1385

static VALUE
console_cursor_up(VALUE io, VALUE val)
{
    return console_move(io, -NUM2INT(val), 0);
}

#echo=(flag) ⇒ Object

Enables/disables echo back. On some platforms, all combinations of this flags and raw/cooked mode may not be valid.

You must require ‘io/console’ to use this method.



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'console/console.c', line 612

static VALUE
console_set_echo(VALUE io, VALUE f)
{
    conmode t;
    rb_io_t *fptr;
    int fd;

    GetOpenFile(io, fptr);
    fd = GetReadFD(fptr);
    if (!getattr(fd, &t)) sys_fail_fptr(fptr);
    if (RTEST(f))
	set_echo(&t, NULL);
    else
	set_noecho(&t, NULL);
    if (!setattr(fd, &t)) sys_fail_fptr(fptr);
    return io;
}

#echo?Boolean

Returns true if echo back is enabled.

You must require ‘io/console’ to use this method.

Returns:

  • (Boolean)


638
639
640
641
642
643
644
645
646
647
648
649
# File 'console/console.c', line 638

static VALUE
console_echo_p(VALUE io)
{
    conmode t;
    rb_io_t *fptr;
    int fd;

    GetOpenFile(io, fptr);
    fd = GetReadFD(fptr);
    if (!getattr(fd, &t)) sys_fail_fptr(fptr);
    return echo_p(&t) ? Qtrue : Qfalse;
}

#erase_line(val) ⇒ Object



1348
1349
1350
1351
1352
1353
1354
# File 'console/console.c', line 1348

static VALUE
console_erase_line(VALUE io, VALUE val)
{
    int mode = mode_in_range(val, 2, "line erase");
    rb_io_write(io, rb_sprintf("\x1b[%dK", mode));
    return io;
}

#erase_screen(val) ⇒ Object



1356
1357
1358
1359
1360
1361
1362
# File 'console/console.c', line 1356

static VALUE
console_erase_screen(VALUE io, VALUE val)
{
    int mode = mode_in_range(val, 3, "screen erase");
    rb_io_write(io, rb_sprintf("\x1b[%dJ", mode));
    return io;
}

#getch(min: nil, time: nil, intr: nil) ⇒ String

Reads and returns a character in raw mode.

See IO#raw for details on the parameters.

You must require ‘io/console’ to use this method.

Returns:

  • (String)


507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'console/console.c', line 507

static VALUE
console_getch(int argc, VALUE *argv, VALUE io)
{
    rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
#ifndef _WIN32
    return ttymode(io, getc_call, io, set_rawmode, optp);
#else
    rb_io_t *fptr;
    VALUE str;
    wint_t c;
    int len;
    char buf[8];
    wint_t wbuf[2];
# ifndef HAVE_RB_IO_WAIT
    struct timeval *to = NULL, tv;
# else
    VALUE timeout = Qnil;
# endif

    GetOpenFile(io, fptr);
    if (optp) {
	if (optp->vtime) {
# ifndef HAVE_RB_IO_WAIT
	    to = &tv;
# else
	    struct timeval tv;
# endif
	    tv.tv_sec = optp->vtime / 10;
	    tv.tv_usec = (optp->vtime % 10) * 100000;
# ifdef HAVE_RB_IO_WAIT
	    timeout = rb_scheduler_timeout(&tv);
# endif
	}
	switch (optp->vmin) {
	  case 1: /* default */
	    break;
	  case 0: /* return nil when timed out */
	    if (optp->vtime) break;
	    /* fallthru */
	  default:
	    rb_warning("min option larger than 1 ignored");
	}
	if (optp->intr) {
# ifndef HAVE_RB_IO_WAIT
	    int w = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, to);
	    if (w < 0) rb_eof_error();
	    if (!(w & RB_WAITFD_IN)) return Qnil;
# else
	    VALUE result = rb_io_wait(io, RUBY_IO_READABLE, timeout);
	    if (result == Qfalse) return Qnil;
# endif
	}
	else if (optp->vtime) {
	    rb_warning("Non-zero vtime option ignored if intr flag is unset");
	}
    }
    len = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getch, wbuf, RUBY_UBF_IO, 0);
    switch (len) {
      case 0:
	return Qnil;
      case 2:
	buf[0] = (char)wbuf[0];
	c = wbuf[1];
	len = 1;
	do {
	    buf[len++] = (unsigned char)c;
	} while ((c >>= CHAR_BIT) && len < (int)sizeof(buf));
	return rb_str_new(buf, len);
      default:
	c = wbuf[0];
	len = rb_uv_to_utf8(buf, c);
	str = rb_utf8_str_new(buf, len);
	return rb_str_conv_enc(str, NULL, rb_default_external_encoding());
    }
#endif
}

#getpass(prompt = nil) ⇒ String

Reads and returns a line without echo back. Prints prompt unless it is nil.

The newline character that terminates the read line is removed from the returned string, see String#chomp!.

You must require ‘io/console’ to use this method.

Returns:

  • (String)


1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
# File 'console/console.c', line 1583

static VALUE
console_getpass(int argc, VALUE *argv, VALUE io)
{
    VALUE str, wio;

    rb_check_arity(argc, 0, 1);
    wio = rb_io_get_write_io(io);
    if (wio == io && io == rb_stdin) wio = rb_stderr;
    prompt(argc, argv, wio);
    str = rb_ensure(getpass_call, io, puts_call, wio);
    return str_chomp(str);
}

#goto(y, x) ⇒ Object



1321
1322
1323
1324
1325
1326
# File 'console/console.c', line 1321

static VALUE
console_goto(VALUE io, VALUE y, VALUE x)
{
    rb_io_write(io, rb_sprintf("\x1b[%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1));
    return io;
}

#goto_column(val) ⇒ Object



1341
1342
1343
1344
1345
1346
# File 'console/console.c', line 1341

static VALUE
console_goto_column(VALUE io, VALUE val)
{
    rb_io_write(io, rb_sprintf("\x1b[%dG", NUM2UINT(val)+1));
    return io;
}

#iflushObject

Flushes input buffer in kernel.

You must require ‘io/console’ to use this method.



910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'console/console.c', line 910

static VALUE
console_iflush(VALUE io)
{
    rb_io_t *fptr;
    int fd;

    GetOpenFile(io, fptr);
    fd = GetReadFD(fptr);
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
    if (tcflush(fd, TCIFLUSH)) sys_fail_fptr(fptr);
#endif
    (void)fd;
    return io;
}

#ioflushObject

Flushes input and output buffers in kernel.

You must require ‘io/console’ to use this method.



956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# File 'console/console.c', line 956

static VALUE
console_ioflush(VALUE io)
{
    rb_io_t *fptr;
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
    int fd1, fd2;
#endif

    GetOpenFile(io, fptr);
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
    fd1 = GetReadFD(fptr);
    fd2 = GetWriteFD(fptr);
    if (fd2 != -1 && fd1 != fd2) {
	if (tcflush(fd1, TCIFLUSH)) sys_fail_fptr(fptr);
	if (tcflush(fd2, TCOFLUSH)) sys_fail_fptr(fptr);
    }
    else {
	if (tcflush(fd1, TCIOFLUSH)) sys_fail_fptr(fptr);
    }
#endif
    return io;
}

#noecho {|io| ... } ⇒ Object

Yields self with disabling echo back.

STDIN.noecho(&:gets)

will read and return a line without echo back.

You must require ‘io/console’ to use this method.

Yields:

  • (io)


596
597
598
599
600
# File 'console/console.c', line 596

static VALUE
console_noecho(VALUE io)
{
    return ttymode(io, rb_yield, io, set_noecho, NULL);
}

#nonblock {|io| ... } ⇒ IO #nonblock(boolean) {|io| ... } ⇒ IO

Yields self in non-blocking mode.

When false is given as an argument, self is yielded in blocking mode. The original mode is restored after the block is executed.

Overloads:

  • #nonblock {|io| ... } ⇒ IO

    Yields:

    • (io)

    Returns:

  • #nonblock(boolean) {|io| ... } ⇒ IO

    Yields:

    • (io)

    Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'nonblock/nonblock.c', line 109

static VALUE
rb_io_nonblock_block(int argc, VALUE *argv, VALUE io)
{
    int nb = 1;
    rb_io_t *fptr;
    int f, restore[2];

    GetOpenFile(io, fptr);
    if (argc > 0) {
	VALUE v;
	rb_scan_args(argc, argv, "01", &v);
	nb = RTEST(v);
    }
    f = io_nonblock_mode(fptr->fd);
    restore[0] = fptr->fd;
    restore[1] = f;
    if (!io_nonblock_set(fptr->fd, f, nb))
	return rb_yield(io);
    return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore);
}

#nonblock=(boolean) ⇒ Boolean

Enables non-blocking mode on a stream when set to true, and blocking mode when set to false.

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
# File 'nonblock/nonblock.c', line 78

static VALUE
rb_io_nonblock_set(VALUE io, VALUE nb)
{
    rb_io_t *fptr;
    GetOpenFile(io, fptr);
    if (RTEST(nb))
	rb_io_set_nonblock(fptr);
    else
	io_nonblock_set(fptr->fd, io_nonblock_mode(fptr->fd), RTEST(nb));
    return io;
}

#nonblock?Boolean

Returns true if an IO object is in non-blocking mode.

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'nonblock/nonblock.c', line 39

static VALUE
rb_io_nonblock_p(VALUE io)
{
    rb_io_t *fptr;
    GetOpenFile(io, fptr);
    if (io_nonblock_mode(fptr->fd) & O_NONBLOCK)
	return Qtrue;
    return Qfalse;
}

#nreadInteger

Returns number of bytes that can be read without blocking. Returns zero if no information available.

Returns:

  • (Integer)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'wait/wait.c', line 51

static VALUE
io_nread(VALUE io)
{
    rb_io_t *fptr = NULL;
    ioctl_arg n;

    GetOpenFile(io, fptr);
    rb_io_check_readable(fptr);
    int len = rb_io_read_pending(fptr);
    if (len > 0) return INT2FIX(len);
    if (!FIONREAD_POSSIBLE_P(fptr->fd)) return INT2FIX(0);
    if (ioctl(fptr->fd, FIONREAD, &n)) return INT2FIX(0);
    if (n > 0) return ioctl_arg2num(n);
    return INT2FIX(0);
}

#oflushObject

Flushes output buffer in kernel.

You must require ‘io/console’ to use this method.



933
934
935
936
937
938
939
940
941
942
943
944
945
946
# File 'console/console.c', line 933

static VALUE
console_oflush(VALUE io)
{
    rb_io_t *fptr;
    int fd;

    GetOpenFile(io, fptr);
    fd = GetWriteFD(fptr);
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
    if (tcflush(fd, TCOFLUSH)) sys_fail_fptr(fptr);
#endif
    (void)fd;
    return io;
}

#pressed?(k) ⇒ Boolean

Returns:

  • (Boolean)


1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'console/console.c', line 1199

static VALUE
console_key_pressed_p(VALUE io, VALUE k)
{
    int vk = -1;

    if (FIXNUM_P(k)) {
	vk = NUM2UINT(k);
    }
    else {
	const struct vktable *t;
	const char *kn;
	if (SYMBOL_P(k)) {
	    k = rb_sym2str(k);
	    kn = RSTRING_PTR(k);
	}
	else {
	    kn = StringValuePtr(k);
	}
	t = console_win32_vk(kn, RSTRING_LEN(k));
	if (!t || (vk = (short)t->vk) == -1) {
	    rb_raise(rb_eArgError, "unknown virtual key code: % "PRIsVALUE, k);
	}
    }
    return GetKeyState(vk) & 0x80 ? Qtrue : Qfalse;
}

#raw(min: nil, time: nil, intr: nil) {|io| ... } ⇒ Object

Yields self within raw mode, and returns the result of the block.

STDIN.raw(&:gets)

will read and return a line without echo back and line editing.

The parameter min specifies the minimum number of bytes that should be received when a read operation is performed. (default: 1)

The parameter time specifies the timeout in seconds with a precision of 1/10 of a second. (default: 0)

If the parameter intr is true, enables break, interrupt, quit, and suspend special characters.

Refer to the manual page of termios for further details.

You must require ‘io/console’ to use this method.

Yields:

  • (io)


390
391
392
393
394
395
# File 'console/console.c', line 390

static VALUE
console_raw(int argc, VALUE *argv, VALUE io)
{
    rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
    return ttymode(io, rb_yield, io, set_rawmode, optp);
}

#raw!(min: nil, time: nil, intr: nil) ⇒ IO

Enables raw mode, and returns io.

If the terminal mode needs to be back, use io.raw { ... }.

See IO#raw for details on the parameters.

You must require ‘io/console’ to use this method.

Returns:



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'console/console.c', line 409

static VALUE
console_set_raw(int argc, VALUE *argv, VALUE io)
{
    conmode t;
    rb_io_t *fptr;
    int fd;
    rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);

    GetOpenFile(io, fptr);
    fd = GetReadFD(fptr);
    if (!getattr(fd, &t)) sys_fail_fptr(fptr);
    set_rawmode(&t, optp);
    if (!setattr(fd, &t)) sys_fail_fptr(fptr);
    return io;
}

#ready?Boolean

Returns true if input available without blocking, or false.

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
# File 'wait/wait.c', line 93

static VALUE
io_ready_p(VALUE io)
{
    rb_io_t *fptr;

    GetOpenFile(io, fptr);
    rb_io_check_readable(fptr);
    if (rb_io_read_pending(fptr)) return Qtrue;

    return io_wait_event(io, RUBY_IO_READABLE, RB_INT2NUM(0));
}

#scroll_backward(val) ⇒ Object



1415
1416
1417
1418
1419
# File 'console/console.c', line 1415

static VALUE
console_scroll_backward(VALUE io, VALUE val)
{
    return console_scroll(io, -NUM2INT(val));
}

#scroll_forward(val) ⇒ Object



1409
1410
1411
1412
1413
# File 'console/console.c', line 1409

static VALUE
console_scroll_forward(VALUE io, VALUE val)
{
    return console_scroll(io, +NUM2INT(val));
}

#wait(events, timeout) ⇒ Object #wait(timeout = nil, mode = :read) ⇒ Object

Waits until the IO becomes ready for the specified events and returns the subset of events that become ready, or false when times out.

The events can be a bit mask of IO::READABLE, IO::WRITABLE or IO::PRIORITY.

Returns true immediately when buffered data is available.

Optional parameter mode is one of :read, :write, or :read_write (deprecated).



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'wait/wait.c', line 228

static VALUE
io_wait(int argc, VALUE *argv, VALUE io)
{
    VALUE timeout = Qnil;
    rb_io_event_t events = 0;

    if (argc < 2 || (argc >= 2 && RB_SYMBOL_P(argv[1]))) {
	if (argc > 0) {
	    timeout = argv[0];
	}

	for (int i = 1; i < argc; i += 1) {
	    events |= wait_mode_sym(argv[i]);
	}
    }
    else if (argc == 2) {
	events = RB_NUM2UINT(argv[0]);

	if (argv[1] != Qnil) {
	    timeout = argv[1];
	}
    }
    else {
	// TODO error
	return Qnil;
    }

    if (events == 0) {
	events = RUBY_IO_READABLE;
    }

    if (events & RUBY_IO_READABLE) {
	rb_io_t *fptr = NULL;
	RB_IO_POINTER(io, fptr);

	if (rb_io_read_pending(fptr)) {
	    return Qtrue;
	}
    }

    return io_wait_event(io, events, timeout);
}

#wait_priorityBoolean #wait_priority(timeout) ⇒ Boolean

Waits until IO is priority and returns true or false when times out.

Overloads:

  • #wait_priorityBoolean

    Returns:

    • (Boolean)
  • #wait_priority(timeout) ⇒ Boolean

    Returns:

    • (Boolean)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'wait/wait.c', line 161

static VALUE
io_wait_priority(int argc, VALUE *argv, VALUE io)
{
    rb_io_t *fptr = NULL;

    RB_IO_POINTER(io, fptr);
    rb_io_check_readable(fptr);

    if (rb_io_read_pending(fptr)) return Qtrue;

    rb_check_arity(argc, 0, 1);
    VALUE timeout = argc == 1 ? argv[0] : Qnil;

    return io_wait_event(io, RUBY_IO_PRIORITY, timeout);
}

#wait_readableBoolean #wait_readable(timeout) ⇒ Boolean

Waits until IO is readable and returns true, or false when times out. Returns true immediately when buffered data is available.

Overloads:

  • #wait_readableBoolean

    Returns:

    • (Boolean)
  • #wait_readable(timeout) ⇒ Boolean

    Returns:

    • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'wait/wait.c', line 115

static VALUE
io_wait_readable(int argc, VALUE *argv, VALUE io)
{
    rb_io_t *fptr = NULL;

    RB_IO_POINTER(io, fptr);
    rb_io_check_readable(fptr);

    if (rb_io_read_pending(fptr)) return Qtrue;

    rb_check_arity(argc, 0, 1);
    VALUE timeout = (argc == 1 ? argv[0] : Qnil);

    return io_wait_event(io, RUBY_IO_READABLE, timeout);
}

#wait_writableBoolean #wait_writable(timeout) ⇒ Boolean

Waits until IO is writable and returns true or false when times out.

Overloads:

  • #wait_writableBoolean

    Returns:

    • (Boolean)
  • #wait_writable(timeout) ⇒ Boolean

    Returns:

    • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'wait/wait.c', line 139

static VALUE
io_wait_writable(int argc, VALUE *argv, VALUE io)
{
    rb_io_t *fptr = NULL;

    RB_IO_POINTER(io, fptr);
    rb_io_check_writable(fptr);

    rb_check_arity(argc, 0, 1);
    VALUE timeout = (argc == 1 ? argv[0] : Qnil);

    return io_wait_event(io, RUBY_IO_WRITABLE, timeout);
}

#winsizeArray

Returns console size.

You must require ‘io/console’ to use this method.

Returns:

  • (Array)


788
789
790
791
792
793
794
795
796
797
798
799
# File 'console/console.c', line 788

static VALUE
console_winsize(VALUE io)
{
    rb_io_t *fptr;
    int fd;
    rb_console_size_t ws;

    GetOpenFile(io, fptr);
    fd = GetWriteFD(fptr);
    if (!getwinsize(fd, &ws)) sys_fail_fptr(fptr);
    return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws)));
}

#winsize=(size) ⇒ Object

Tries to set console size. The effect depends on the platform and the running environment.

You must require ‘io/console’ to use this method.



810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# File 'console/console.c', line 810

static VALUE
console_set_winsize(VALUE io, VALUE size)
{
    rb_io_t *fptr;
    rb_console_size_t ws;
#if defined _WIN32
    HANDLE wh;
    int newrow, newcol;
    BOOL ret;
#endif
    VALUE row, col, xpixel, ypixel;
    const VALUE *sz;
    int fd;
    long sizelen;

    GetOpenFile(io, fptr);
    size = rb_Array(size);
    if ((sizelen = RARRAY_LEN(size)) != 2 && sizelen != 4) {
	rb_raise(rb_eArgError,
		 "wrong number of arguments (given %ld, expected 2 or 4)",
		 sizelen);
    }
    sz = RARRAY_CONST_PTR(size);
    row = sz[0], col = sz[1], xpixel = ypixel = Qnil;
    if (sizelen == 4) xpixel = sz[2], ypixel = sz[3];
    fd = GetWriteFD(fptr);
#if defined TIOCSWINSZ
    ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0;
#define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
    SET(row);
    SET(col);
    SET(xpixel);
    SET(ypixel);
#undef SET
    if (!setwinsize(fd, &ws)) sys_fail_fptr(fptr);
#elif defined _WIN32
    wh = (HANDLE)rb_w32_get_osfhandle(fd);
#define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
    SET(row);
    SET(col);
#undef SET
    if (!NIL_P(xpixel)) (void)NUM2UINT(xpixel);
    if (!NIL_P(ypixel)) (void)NUM2UINT(ypixel);
    if (!GetConsoleScreenBufferInfo(wh, &ws)) {
	rb_syserr_fail(LAST_ERROR, "GetConsoleScreenBufferInfo");
    }
    ws.dwSize.X = newcol;
    ret = SetConsoleScreenBufferSize(wh, ws.dwSize);
    ws.srWindow.Left = 0;
    ws.srWindow.Top = 0;
    ws.srWindow.Right = newcol-1;
    ws.srWindow.Bottom = newrow-1;
    if (!SetConsoleWindowInfo(wh, TRUE, &ws.srWindow)) {
	rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo");
    }
    /* retry when shrinking buffer after shrunk window */
    if (!ret && !SetConsoleScreenBufferSize(wh, ws.dwSize)) {
	rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo");
    }
    /* remove scrollbar if possible */
    if (!SetConsoleWindowInfo(wh, TRUE, &ws.srWindow)) {
	rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo");
    }
#endif
    return io;
}