Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- (unknown)
Defined Under Namespace
Modules: generic_readable
Class Method Summary collapse
-
.console(*args) ⇒ Object
Returns an File instance opened console.
-
.default_console_size ⇒ Object
fallback to console window size.
Instance Method Summary collapse
- #beep ⇒ Object
-
#cooked {|io| ... } ⇒ Object
Yields
selfwithin cooked mode. -
#cooked! ⇒ Object
Enables cooked mode.
- #cursor ⇒ Object
- #cursor=(cpos) ⇒ Object
-
#echo=(flag) ⇒ Object
Enables/disables echo back.
-
#echo? ⇒ Boolean
Returns
trueif echo back is enabled. -
#getch(min: nil, time: nil) ⇒ String
Reads and returns a character in raw mode.
-
#getpass(prompt = nil) ⇒ String
Reads and returns a line without echo back.
- #goto(x, y) ⇒ Object
-
#iflush ⇒ Object
Flushes input buffer in kernel.
-
#ioflush ⇒ Object
Flushes input and output buffers in kernel.
-
#noecho {|io| ... } ⇒ Object
Yields
selfwith disabling echo back. -
#oflush ⇒ Object
Flushes output buffer in kernel.
- #pressed?(k) ⇒ Boolean
-
#raw(min: nil, time: nil) {|io| ... } ⇒ Object
Yields
selfwithin raw mode. -
#raw!(min: nil, time: nil) ⇒ Object
Enables raw mode.
-
#winsize ⇒ Array
Returns console size.
-
#winsize=(size) ⇒ Object
Tries to set console size.
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.
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 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 |
# File 'ext/io/console/console.c', line 779
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_size ⇒ Object
fallback to console window size
3 4 5 6 7 8 |
# File 'lib/io/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
#beep ⇒ Object
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'ext/io/console/console.c', line 673
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)
rb_sys_fail(0);
#endif
return io;
}
|
#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.
357 358 359 360 361 |
# File 'ext/io/console/console.c', line 357
static VALUE
console_cooked(VALUE io)
{
return ttymode(io, rb_yield, 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.
373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'ext/io/console/console.c', line 373
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)) rb_sys_fail(0);
set_cookedmode(&t, NULL);
if (!setattr(fd, &t)) rb_sys_fail(0);
return io;
}
|
#cursor ⇒ Object
709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
# File 'ext/io/console/console.c', line 709
static VALUE
console_cursor_pos(VALUE io)
{
rb_io_t *fptr;
int fd;
rb_console_size_t ws;
GetOpenFile(io, fptr);
fd = GetWriteFD(fptr);
if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) {
rb_syserr_fail(LAST_ERROR, 0);
}
return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.X), UINT2NUM(ws.dwCursorPosition.Y));
}
|
#cursor=(cpos) ⇒ Object
724 725 726 727 728 729 730 |
# File 'ext/io/console/console.c', line 724
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));
}
|
#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.
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'ext/io/console/console.c', line 439
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)) rb_sys_fail(0);
if (RTEST(f))
set_echo(&t, NULL);
else
set_noecho(&t, NULL);
if (!setattr(fd, &t)) rb_sys_fail(0);
return io;
}
|
#echo? ⇒ Boolean
Returns true if echo back is enabled.
You must require ‘io/console’ to use this method.
465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'ext/io/console/console.c', line 465
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)) rb_sys_fail(0);
return echo_p(&t) ? Qtrue : Qfalse;
}
|
#getch(min: nil, time: 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.
404 405 406 407 408 409 |
# File 'ext/io/console/console.c', line 404
static VALUE
console_getch(int argc, VALUE *argv, VALUE io)
{
rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
return ttymode(io, getc_call, set_rawmode, optp);
}
|
#getpass(prompt = nil) ⇒ String
Reads and returns a line without echo back. Prints prompt unless it is nil.
You must require ‘io/console’ to use this method.
917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'ext/io/console/console.c', line 917
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(x, y) ⇒ Object
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'ext/io/console/console.c', line 692
static VALUE
console_goto(VALUE io, VALUE x, VALUE y)
{
rb_io_t *fptr;
int fd;
COORD pos;
GetOpenFile(io, fptr);
fd = GetWriteFD(fptr);
pos.X = NUM2UINT(x);
pos.Y = NUM2UINT(y);
if (!SetConsoleCursorPosition((HANDLE)rb_w32_get_osfhandle(fd), pos)) {
rb_syserr_fail(LAST_ERROR, 0);
}
return io;
}
|
#iflush ⇒ Object
Flushes input buffer in kernel.
You must require ‘io/console’ to use this method.
604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'ext/io/console/console.c', line 604
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)) rb_sys_fail(0);
#endif
(void)fd;
return io;
}
|
#ioflush ⇒ Object
Flushes input and output buffers in kernel.
You must require ‘io/console’ to use this method.
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
# File 'ext/io/console/console.c', line 650
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)) rb_sys_fail(0);
if (tcflush(fd2, TCOFLUSH)) rb_sys_fail(0);
}
else {
if (tcflush(fd1, TCIOFLUSH)) rb_sys_fail(0);
}
#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.
423 424 425 426 427 |
# File 'ext/io/console/console.c', line 423
static VALUE
console_noecho(VALUE io)
{
return ttymode(io, rb_yield, set_noecho, NULL);
}
|
#oflush ⇒ Object
Flushes output buffer in kernel.
You must require ‘io/console’ to use this method.
627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'ext/io/console/console.c', line 627
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)) rb_sys_fail(0);
#endif
(void)fd;
return io;
}
|
#pressed?(k) ⇒ Boolean
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
# File 'ext/io/console/console.c', line 734
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) {|io| ... } ⇒ Object
Yields self within raw mode.
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)
Refer to the manual page of termios for further details.
You must require ‘io/console’ to use this method.
310 311 312 313 314 315 |
# File 'ext/io/console/console.c', line 310
static VALUE
console_raw(int argc, VALUE *argv, VALUE io)
{
rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
return ttymode(io, rb_yield, set_rawmode, optp);
}
|
#raw!(min: nil, time: nil) ⇒ Object
Enables raw mode.
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.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'ext/io/console/console.c', line 329
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, &opts);
GetOpenFile(io, fptr);
fd = GetReadFD(fptr);
if (!getattr(fd, &t)) rb_sys_fail(0);
set_rawmode(&t, optp);
if (!setattr(fd, &t)) rb_sys_fail(0);
return io;
}
|
#winsize ⇒ Array
Returns console size.
You must require ‘io/console’ to use this method.
506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'ext/io/console/console.c', line 506
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)) rb_sys_fail(0);
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.
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 583 584 585 586 587 588 589 590 591 592 593 |
# File 'ext/io/console/console.c', line 528
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)) rb_sys_fail(0);
#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;
}
|