53
53
import org .jruby .anno .JRubyMethod ;
54
54
import org .jruby .anno .JRubyModule ;
55
55
import org .jruby .runtime .Block ;
56
+ import org .jruby .runtime .Helpers ;
56
57
import org .jruby .runtime .ThreadContext ;
57
58
import static org .jruby .runtime .Visibility .*;
58
59
import org .jruby .runtime .builtin .IRubyObject ;
@@ -215,16 +216,12 @@ private static IRubyObject readlineImpl(ThreadContext context, String prompt, fi
215
216
holder .readline .setExpandEvents (false );
216
217
217
218
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
228
225
}
229
226
230
227
if (line == null ) return context .nil ;
@@ -242,6 +239,17 @@ private static IRubyObject readlineImpl(ThreadContext context, String prompt, fi
242
239
return RubyString .newString (runtime , bytes );
243
240
}
244
241
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
+
245
253
@ JRubyMethod (name = "input=" , module = true , visibility = PRIVATE )
246
254
public static IRubyObject setInput (ThreadContext context , IRubyObject recv , IRubyObject input ) {
247
255
// FIXME: JRUBY-3604
0 commit comments