/**
   * Read a character from the console.
   *
   * @return the character, or -1 if an EOF is received.
   */
  public final int readVirtualKey() throws IOException {
    int c = terminal.readVirtualKey(in);

    if (debugger != null) {
      debug("keystroke: " + c + "");
    }

    // clear any echo characters
    clearEcho(c);

    return c;
  }
  /** Clear the echoed characters for the specified character code. */
  int clearEcho(int c) throws IOException {
    // if the terminal is not echoing, then just return...
    if (!terminal.getEcho()) {
      return 0;
    }

    // otherwise, clear
    int num = countEchoCharacters((char) c);
    back(num);
    drawBuffer(num);

    return num;
  }
  /** Clear the screen by issuing the ANSI "clear screen" code. */
  public boolean clearScreen() throws IOException {
    if (!terminal.isANSISupported()) {
      return false;
    }

    // send the ANSI code to clear the screen
    printString(((char) 27) + "[2J");
    flushConsole();

    // then send the ANSI code to go to position 1,1
    printString(((char) 27) + "[1;1H");
    flushConsole();

    redrawLine();

    return true;
  }
  /**
   * Read a line from the <i>in</i> {@link InputStream}, and return the line (without any trailing
   * newlines).
   *
   * @param prompt the prompt to issue to the console, may be null.
   * @return a line that is read from the terminal, or null if there was null input (e.g.,
   *     <i>CTRL-D</i> was pressed).
   */
  public String readLine(final String prompt, final Character mask) throws IOException {
    this.mask = mask;
    if (prompt != null) this.prompt = prompt;

    try {
      terminal.beforeReadLine(this, this.prompt, mask);

      if ((this.prompt != null) && (this.prompt.length() > 0)) {
        out.write(this.prompt);
        out.flush();
      }

      // if the terminal is unsupported, just use plain-java reading
      if (!terminal.isSupported()) {
        return readLine(in);
      }

      while (true) {
        int[] next = readBinding();

        if (next == null) {
          return null;
        }

        int c = next[0];
        int code = next[1];

        if (c == -1) {
          return null;
        }

        boolean success = true;

        switch (code) {
          case EXIT: // ctrl-d
            if (buf.buffer.length() == 0) {
              return null;
            }

          case COMPLETE: // tab
            success = complete();
            break;

          case MOVE_TO_BEG:
            success = setCursorPosition(0);
            break;

          case KILL_LINE: // CTRL-K
            success = killLine();
            break;

          case CLEAR_SCREEN: // CTRL-L
            success = clearScreen();
            break;

          case KILL_LINE_PREV: // CTRL-U
            success = resetLine();
            break;

          case NEWLINE: // enter
            moveToEnd();
            printNewline(); // output newline
            return finishBuffer();

          case DELETE_PREV_CHAR: // backspace
            success = backspace();
            break;

          case DELETE_NEXT_CHAR: // delete
            success = deleteCurrentCharacter();
            break;

          case MOVE_TO_END:
            success = moveToEnd();
            break;

          case PREV_CHAR:
            success = moveCursor(-1) != 0;
            break;

          case NEXT_CHAR:
            success = moveCursor(1) != 0;
            break;

          case NEXT_HISTORY:
            success = moveHistory(true);
            break;

          case PREV_HISTORY:
            success = moveHistory(false);
            break;

          case REDISPLAY:
            break;

          case PASTE:
            success = paste();
            break;

          case DELETE_PREV_WORD:
            success = deletePreviousWord();
            break;

          case PREV_WORD:
            success = previousWord();
            break;

          case NEXT_WORD:
            success = nextWord();
            break;

          case START_OF_HISTORY:
            success = history.moveToFirstEntry();
            if (success) setBuffer(history.current());
            break;

          case END_OF_HISTORY:
            success = history.moveToLastEntry();
            if (success) setBuffer(history.current());
            break;

          case CLEAR_LINE:
            moveInternal(-(buf.buffer.length()));
            killLine();
            break;

          case INSERT:
            buf.setOvertyping(!buf.isOvertyping());
            break;

          case UNKNOWN:
          default:
            if (c != 0) { // ignore null chars
              ActionListener action =
                  (ActionListener) triggeredActions.get(new Character((char) c));
              if (action != null) action.actionPerformed(null);
              else putChar(c, true);
            } else success = false;
        }

        if (!(success)) {
          beep();
        }

        flushConsole();
      }
    } finally {
      terminal.afterReadLine(this, this.prompt, mask);
    }
  }
 /**
  * Query the terminal to find the current width;
  *
  * @see Terminal#getTerminalHeight
  * @return the height of the current terminal.
  */
 public int getTermheight() {
   return Terminal.setupTerminal().getTerminalHeight();
 }
 /**
  * Query the terminal to find the current width;
  *
  * @see Terminal#getTerminalWidth
  * @return the width of the current terminal.
  */
 public int getTermwidth() {
   return Terminal.setupTerminal().getTerminalWidth();
 }
  /**
   * Create a new reader.
   *
   * @param in the input
   * @param out the output
   * @param bindings the key bindings to use
   * @param term the terminal to use
   */
  public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
      throws IOException {
    this.terminal = term;
    setInput(in);
    this.out = out;

    if (bindings == null) {
      try {
        String bindingFile =
            System.getProperty(
                "jline.keybindings",
                new File(System.getProperty("user.home", ".jlinebindings.properties"))
                    .getAbsolutePath());

        if (new File(bindingFile).isFile()) {
          bindings = new FileInputStream(new File(bindingFile));
        }
      } catch (Exception e) {
        // swallow exceptions with option debugging
        if (debugger != null) {
          e.printStackTrace(debugger);
        }
      }
    }

    if (bindings == null) {
      bindings = terminal.getDefaultBindings();
    }

    this.keybindings = new short[Character.MAX_VALUE * 2];

    Arrays.fill(this.keybindings, UNKNOWN);

    /**
     * Loads the key bindings. Bindings file is in the format:
     *
     * <p>keycode: operation name
     */
    if (bindings != null) {
      Properties p = new Properties();
      p.load(bindings);
      bindings.close();

      for (Iterator i = p.keySet().iterator(); i.hasNext(); ) {
        String val = (String) i.next();

        try {
          Short code = new Short(val);
          String op = (String) p.getProperty(val);

          Short opval = (Short) KEYMAP_NAMES.get(op);

          if (opval != null) {
            keybindings[code.shortValue()] = opval.shortValue();
          }
        } catch (NumberFormatException nfe) {
          consumeException(nfe);
        }
      }

      // hardwired arrow key bindings
      // keybindings[VK_UP] = PREV_HISTORY;
      // keybindings[VK_DOWN] = NEXT_HISTORY;
      // keybindings[VK_LEFT] = PREV_CHAR;
      // keybindings[VK_RIGHT] = NEXT_CHAR;
    }
  }
 public ConsoleReader(final InputStream in, final Writer out, final InputStream bindings)
     throws IOException {
   this(in, out, bindings, Terminal.getTerminal());
 }
 public static void main(String[] arg) {
   // startupの配列から、ハッシュテーブルを作る
   Hashtable<String, String> dict = new Hashtable<String, String>();
   for (int i = 0; i < startup.length; i++) {
     dict.put(startup[i][0], startup[i][1]);
   }
   // コマンドを打ち込んでもらって、追加・削除・一覧表示
   String command;
   while (true) {
     command = Terminal.inputString("コマンド入力:");
     if (command.equals("quit")) {
       break;
     }
     // 一覧表示
     if (command.equals("list")) {
       Enumeration<String> keys = dict.keys();
       while (keys.hasMoreElements()) {
         String key = keys.nextElement();
         System.out.printf("%s: %s\n", key, dict.get(key));
       }
     }
     // 追加
     if (command.equals("append")) {
       String english = Terminal.inputString("英単語入力:");
       String japanese = Terminal.inputString("和訳入力:");
       String kind = Terminal.inputString("品詞入力:");
       if (dict.get(english) != null) {
         System.out.println("その英単語「" + english + "」は既に入力済みです。");
       } else {
         dict.put(english, kind + ":" + japanese);
       }
     }
     // 削除
     if (command.equals("remove")) {
       String english = Terminal.inputString("削除する英単語入力:");
       if (dict.get(english) == null) {
         System.out.println("その英単語「" + english + "」はありません");
       } else {
         dict.remove(english);
       }
     }
     // 逐語変換
     if (command.equals("translate")) {
       String text = Terminal.readLine("英文入力:");
       if (text.equals("")) {
         break;
       }
       // 単語に分解
       String words[] = text.split(" ");
       String result = "";
       String verbresult = ""; // 動詞を最後に持ってくる
       String prepresult = ""; // 前置詞を後ろに持ってくるため
       for (int i = 0; i < words.length; i++) {
         // 該当する英単語があるかどうか
         String trans = dict.get(words[i]);
         // ない場合は、そのまま英単語を使う
         if (trans == null) {
           result += words[i];
         } else {
           String wayaku = trans.substring(trans.indexOf(":") + 1); // コロン以降を表示
           String hinshi = trans.substring(0, trans.indexOf(":"));
           // もし品詞が動詞だった場合
           if (hinshi.equals("verb")) {
             result += "が";
             verbresult += wayaku;
           }
           // もし品詞が前置詞だった場合
           else if (hinshi.equals("prep")) {
             result += prepresult;
             prepresult = wayaku;
           } else {
             result += wayaku;
           }
         }
       }
       // 最後に動詞と前置詞の訳を加えて表示する
       System.out.println(result + prepresult + verbresult);
     }
   }
 }