Skip to content

Commit 598a374

Browse files
committed
[fix] another try at restoring terminal settings
since the previous attempt (on runtime tear-down) does not always work towards getting updates back: jruby/jruby#5387 actually also resolves: jruby/jruby#3181
1 parent 72790ff commit 598a374

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/main/java/org/jruby/ext/readline/Readline.java

+18-10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.jruby.anno.JRubyMethod;
5454
import org.jruby.anno.JRubyModule;
5555
import org.jruby.runtime.Block;
56+
import org.jruby.runtime.Helpers;
5657
import org.jruby.runtime.ThreadContext;
5758
import static org.jruby.runtime.Visibility.*;
5859
import org.jruby.runtime.builtin.IRubyObject;
@@ -215,16 +216,12 @@ private static IRubyObject readlineImpl(ThreadContext context, String prompt, fi
215216
holder.readline.setExpandEvents(false);
216217

217218
String line;
218-
while (true) {
219-
try {
220-
holder.readline.getTerminal().setEchoEnabled(false);
221-
line = holder.readline.readLine(prompt);
222-
break;
223-
} catch (IOException ioe) {
224-
throw runtime.newIOErrorFromException(ioe);
225-
} finally {
226-
holder.readline.getTerminal().setEchoEnabled(true);
227-
}
219+
try {
220+
line = readlineLoop(holder.readline, prompt);
221+
} catch (IOException ex) {
222+
throw runtime.newIOErrorFromException(ex);
223+
} catch (Exception ex) {
224+
Helpers.throwException(ex); return null; // likely init/restore failure
228225
}
229226

230227
if (line == null) return context.nil;
@@ -242,6 +239,17 @@ private static IRubyObject readlineImpl(ThreadContext context, String prompt, fi
242239
return RubyString.newString(runtime, bytes);
243240
}
244241

242+
private static String readlineLoop(final ConsoleReader reader, final String prompt) throws Exception {
243+
while (true) {
244+
try {
245+
reader.getTerminal().init();
246+
return reader.readLine(prompt);
247+
} finally {
248+
reader.getTerminal().restore();
249+
}
250+
}
251+
}
252+
245253
@JRubyMethod(name = "input=", module = true, visibility = PRIVATE)
246254
public static IRubyObject setInput(ThreadContext context, IRubyObject recv, IRubyObject input) {
247255
// FIXME: JRUBY-3604

0 commit comments

Comments
 (0)