Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ group :development do
gem "test-unit"
gem "test-unit-ruby-core"
gem 'rake-compiler'
gem "async"
end
28 changes: 25 additions & 3 deletions ext/io/console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ extern VALUE rb_scheduler_timeout(struct timeval *timeout);
# define rb_fiber_scheduler_make_timeout rb_scheduler_timeout
#endif

#ifdef HAVE_SYS_EVENT_H
extern VALUE rb_fiber_scheduler_current(void);
#endif

#ifndef HAVE_RB_IO_DESCRIPTOR
static int
io_descriptor_fallback(VALUE io)
Expand Down Expand Up @@ -1695,12 +1699,30 @@ console_dev(int argc, VALUE *argv, VALUE klass)
int fd;
VALUE path = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE));

fd = -1;
#ifdef HAVE_SYS_EVENT_H
VALUE scheduler = rb_fiber_scheduler_current();
if (!NIL_P(scheduler)) {
if (isatty(0)) {
fd = dup(0);
path = rb_obj_freeze(rb_str_new2("<STDIN>"));
} else if (isatty(1)) {
fd = dup(1);
path = rb_obj_freeze(rb_str_new2("<STDOUT>"));
} else if (isatty(2)) {
fd = dup(2);
path = rb_obj_freeze(rb_str_new2("<STDERR>"));
}
}
#endif
if (fd == -1) {
#ifdef CONSOLE_DEVICE_FOR_WRITING
fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0);
if (fd < 0) return Qnil;
out = rb_io_open_descriptor(klass, fd, FMODE_WRITABLE | FMODE_SYNC, path, Qnil, NULL);
int writing_fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0);
if (writing_fd < 0) return Qnil;
out = rb_io_open_descriptor(klass, writing_fd, FMODE_WRITABLE | FMODE_SYNC, path, Qnil, NULL);
#endif
fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0);
}
if (fd < 0) {
#ifdef CONSOLE_DEVICE_FOR_WRITING
rb_io_close(out);
Expand Down
1 change: 1 addition & 0 deletions ext/io/console/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
case ok
when true
have_header("sys/ioctl.h") if hdr
have_header("sys/event.h")
# rb_check_hash_type: 1.9.3
# rb_io_get_write_io: 1.9.1
# rb_cloexec_open: 2.0.0
Expand Down
21 changes: 18 additions & 3 deletions test/io/console/test_io_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@
def test_noecho
helper {|m, s|
s.noecho {
assert_not_send([s, :echo?])
m.print "a"
sleep 0.1
assert_not_send([s, :echo?])
m.print "a"
sleep 0.1
}
m.print "b"
assert_equal("b", m.readpartial(10))
Expand Down Expand Up @@ -367,6 +367,21 @@
end
end

def test_cursor_position_kqueue_regression
run_pty("#{<<~"begin;"}\n#{<<~'end;'}") do |r, w, _|
begin;
require "async"
require "io/console"

coords = Async { IO.console.cursor }.wait
p coords
end;
assert_equal("\e[6n", r.readpartial(5))

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.0, ubuntu-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.1, macos-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.4, ubuntu-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.2, macos-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.3, macos-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.2, ubuntu-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.3, ubuntu-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.4, macos-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").

Check failure on line 379 in test/io/console/test_io_console.rb

View workflow job for this annotation

GitHub Actions / test (3.1, ubuntu-latest)

Failure

<"\e[6n">("UTF-8") expected but was <"<inte">("ASCII-8BIT").
w.print("\e[12;34R"); w.flush
assert_equal([11, 33], eval(r.gets))
end
end

def assert_ctrl(expect, cc, r, w)
sleep 0.1
w.print cc
Expand Down
Loading