Exemplo n.º 1
0
  @JRubyMethod(name = "readline", module = true, visibility = Visibility.PRIVATE)
  public static IRubyObject s_readline(
      ThreadContext context, IRubyObject recv, IRubyObject prompt, IRubyObject add_to_hist)
      throws IOException {
    Ruby runtime = context.getRuntime();
    ConsoleHolder holder = getHolder(runtime);
    if (holder.readline == null) {
      initReadline(runtime, holder); // not overridden, let's go
    }

    IRubyObject line = runtime.getNil();
    String v = null;
    while (true) {
      try {
        holder.readline.getTerminal().disableEcho();
        v = holder.readline.readLine(prompt.toString());
        break;
      } catch (IOException ioe) {
        if (RubyIO.restartSystemCall(ioe)) {
          // This is for JRUBY-2988, since after a suspend the terminal seems
          // to need to be reinitialized. Since we can't easily detect suspension,
          // initialize after every readline. Probably not fast, but this is for
          // interactive terminals anyway...so who cares?
          try {
            holder.readline.getTerminal().initializeTerminal();
          } catch (Exception e) {
          }
          continue;
        }
        throw runtime.newIOErrorFromException(ioe);
      } finally {
        holder.readline.getTerminal().enableEcho();
      }
    }

    if (null != v) {
      if (add_to_hist.isTrue()) {
        holder.readline.getHistory().addToHistory(v);
      }

      /* Explicitly use UTF-8 here. c.f. history.addToHistory using line.asUTF8() */
      line = RubyString.newUnicodeString(recv.getRuntime(), v);
    }
    return line;
  }