Skip to content

Commit 18c67a7

Browse files
committed
Do not use an extra Thread to read the input with JLine
PullRequest: truffleruby/664
2 parents a4c8b44 + 2470d86 commit 18c67a7

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Bug fixes:
44

55
* Implement `rb_io_wait_writable` (#1586).
6+
* Fixed error when using arrows keys first within `irb` or `pry` (#1478, #1486).
67

78
Changes:
89

spec/tags/truffle/irb/arrows_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
slow:IRB supports using arrow keys first

spec/truffle/irb/arrows_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "IRB" do
12+
it "supports using arrow keys first" do
13+
# --readline is needed otherwise Readline is not used when stdin is not a TTY.
14+
IO.popen([*ruby_exe, "-S", "irb", "-f", "--prompt=simple", "--readline"], "r+") do |io|
15+
io.gets.should == "Switch to inspect mode.\n"
16+
17+
io.puts "\e[A" # up arrow
18+
# JLine seems to add a bell character
19+
[">> \n", ">> \a\n"].should include(io.gets)
20+
21+
io.puts "22 + 33"
22+
# The extra bell character causes a continuation line
23+
[">> 22 + 33\n", "?> 22 + 33\n"].should include(io.gets)
24+
io.gets.should == "=> 55\n"
25+
26+
io.puts "exit"
27+
io.gets.should == ">> exit\n"
28+
end
29+
end
30+
end

src/main/java/org/truffleruby/stdlib/readline/ConsoleHolder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,20 @@ private ConsoleHolder(RubyContext context,
7676
this.in = new IoStream(context, inFd, inIo);
7777
this.out = new IoStream(context, outFd, outIo);
7878

79+
// Disable the creation of a Thread to read the input. We already handle interrupts
80+
// natively. See ConsoleReader.setInput() for the logic to determine "nonBlockingEnabled".
81+
final String timeout = System.getProperty(ConsoleReader.JLINE_ESC_TIMEOUT);
82+
System.setProperty(ConsoleReader.JLINE_ESC_TIMEOUT, "0");
7983
try {
8084
readline = new ConsoleReader(in.getIn(), out.getOut());
8185
} catch (IOException e) {
8286
throw new UnsupportedOperationException("Couldn't initialize readline", e);
87+
} finally {
88+
if (timeout == null) {
89+
System.clearProperty(ConsoleReader.JLINE_ESC_TIMEOUT);
90+
} else {
91+
System.setProperty(ConsoleReader.JLINE_ESC_TIMEOUT, timeout);
92+
}
8393
}
8494

8595
readline.setExpandEvents(false);

0 commit comments

Comments
 (0)