public String readLine()

in core/src/main/java/com/jetbrains/youtrackdb/internal/common/console/TTYConsoleReader.java [137:297]


  public String readLine() throws IOException {
    var consoleInput = "";

    var buffer = new StringBuffer();
    cursorPosition = 0;
    historyBuffer = null;
    var historyNum = history.size();
    var hintedHistory = false;
    while (true) {
      var escape = false;
      var ctrl = false;
      var next = inStream.read();
      if (next == ESC) {
        escape = true;
        inStream.read();
        next = inStream.read();
      }
      if (escape) {
        if (next == 49) {
          inStream.read();
          next = inStream.read();
        }
        if (next == CTRL) {
          ctrl = true;
          next = inStream.read();
        }
        if (ctrl) {
          if (next == RIGHT_CHAR) {
            cursorPosition = buffer.indexOf(" ", cursorPosition) + 1;
            if (cursorPosition == 0) {
              cursorPosition = buffer.length();
            }
            updatePrompt(buffer);
          } else if (next == LEFT_CHAR) {
            if (cursorPosition > 1
                && cursorPosition < buffer.length()
                && buffer.charAt(cursorPosition - 1) == ' ') {
              cursorPosition = buffer.lastIndexOf(" ", (cursorPosition - 2)) + 1;
            } else {
              cursorPosition = buffer.lastIndexOf(" ", cursorPosition) + 1;
            }
            if (cursorPosition < 0) {
              cursorPosition = 0;
            }
            updatePrompt(buffer);
          }
        } else {
          if (next == UP_CHAR && !history.isEmpty()) {
            if (history.size() > 0) { // UP
              if (!hintedHistory
                  && (historyNum == history.size()
                  || !buffer.toString().equals(history.get(historyNum)))) {
                if (buffer.length() > 0) {
                  hintedHistory = true;
                  historyBuffer = buffer.toString();
                } else {
                  historyBuffer = null;
                }
              }
              historyNum = getHintedHistoryIndexUp(historyNum);
              if (historyNum > -1) {
                buffer = new StringBuffer(history.get(historyNum));
              } else {
                buffer = new StringBuffer(historyBuffer);
              }
              cursorPosition = buffer.length();
              updatePrompt(buffer);
            }
          } else if (next == DOWN_CHAR && !history.isEmpty()) { // DOWN
            if (history.size() > 0) {
              historyNum = getHintedHistoryIndexDown(historyNum);
              if (historyNum == history.size()) {
                if (historyBuffer != null) {
                  buffer = new StringBuffer(historyBuffer);
                } else {
                  buffer = new StringBuffer();
                }
              } else {
                buffer = new StringBuffer(history.get(historyNum));
              }
              cursorPosition = buffer.length();
              updatePrompt(buffer);
            }
          } else if (next == RIGHT_CHAR) {
            if (cursorPosition < buffer.length()) {
              cursorPosition++;
              updatePrompt(buffer);
            }
          } else if (next == LEFT_CHAR) {
            if (cursorPosition > 0) {
              cursorPosition--;
              updatePrompt(buffer);
            }
          } else if (next == END_CHAR) {
            cursorPosition = buffer.length();
            updatePrompt(buffer);
          } else if (next == BEGIN_CHAR) {
            cursorPosition = 0;
            updatePrompt(buffer);
          }
        }
      } else {
        if (next == NEW_LINE_CHAR) {
          outStream.println();
          oldPromptLength = 0;
          oldTextLength = 0;
          oldCursorPosition = 0;
          maxTotalLength = 0;
          break;

        } else if (next == BACKSPACE_CHAR) {
          if (buffer.length() > 0 && cursorPosition > 0) {
            buffer.deleteCharAt(cursorPosition - 1);
            cursorPosition--;
            updatePrompt(buffer);
          }
        } else if (next == DEL_CHAR) {
          if (buffer.length() > 0 && cursorPosition >= 0 && cursorPosition < buffer.length()) {
            buffer.deleteCharAt(cursorPosition);
            updatePrompt(buffer);
          }
        } else if (next == HORIZONTAL_TAB_CHAR) {
          buffer = writeHint(buffer);
          cursorPosition = buffer.length();
          updatePrompt(buffer);
        } else {
          if ((next > UNIT_SEPARATOR_CHAR && next < BACKSPACE_CHAR) || next > BACKSPACE_CHAR) {
            if (cursorPosition == buffer.length()) {
              buffer.append((char) next);
            } else {
              buffer.insert(cursorPosition, (char) next);
            }
            cursorPosition++;
            updatePrompt(buffer);
          } else {
            outStream.println();
            outStream.print(buffer);
          }
        }
        historyNum = history.size();
        hintedHistory = false;
      }
    }
    consoleInput = buffer.toString();
    history.remove(consoleInput);
    history.add(consoleInput);
    historyNum = history.size();
    writeHistory(historyNum);

    if (consoleInput.equals("clear")) {
      outStream.flush();
      for (var i = 0; i < 150; i++) {
        outStream.println();
      }
      outStream.print("\r");
      outStream.print(console.getPrompt());
      return readLine();
    } else {
      return consoleInput;
    }
  }