예제 #1
0
  /**
   * Set the current buffer's content to the specified {@link String}. The visual console will be
   * modified to show the current buffer.
   *
   * @param buffer the new contents of the buffer.
   */
  private final void setBuffer(final String buffer) throws IOException {
    // don't bother modifying it if it is unchanged
    if (buffer.equals(buf.buffer.toString())) {
      return;
    }

    // obtain the difference between the current buffer and the new one
    int sameIndex = 0;

    for (int i = 0, l1 = buffer.length(), l2 = buf.buffer.length(); (i < l1) && (i < l2); i++) {
      if (buffer.charAt(i) == buf.buffer.charAt(i)) {
        sameIndex++;
      } else {
        break;
      }
    }

    int diff = buf.buffer.length() - sameIndex;

    backspace(diff); // go back for the differences
    killLine(); // clear to the end of the line
    buf.buffer.setLength(sameIndex); // the new length
    putString(buffer.substring(sameIndex)); // append the differences
  }