/** * Calling this method will put the underlying terminal in private mode, clear the screen, move * the cursor and refresh. * * @throws LanternaException */ public void startScreen() { if (hasBeenActivated) return; hasBeenActivated = true; terminal.enterPrivateMode(); terminal.clearScreen(); terminal.moveCursor(cursorPosition.getColumn(), cursorPosition.getRow()); refresh(); }
/** * Call this method to make changes done through {@code putCharacter(...)}, {@code putString(...)} * visible on the terminal. The screen will calculate the changes that are required and send the * necessary characters and control sequences to make it so. */ public void refresh() { if (!hasBeenActivated) return; synchronized (mutex) { // If any resize operations are in the queue, execute them resizeScreenIfNeeded(); Map<TerminalPosition, ScreenCharacter> updateMap = new TreeMap<TerminalPosition, ScreenCharacter>(new ScreenPointComparator()); for (int y = 0; y < terminalSize.getRows(); y++) { for (int x = 0; x < terminalSize.getColumns(); x++) { ScreenCharacter c = backbuffer[y][x]; if (!c.equals(visibleScreen[y][x]) || wholeScreenInvalid) { visibleScreen[y][x] = c; // Remember, ScreenCharacter is immutable, we don't need to worry about it being // modified updateMap.put(new TerminalPosition(x, y), c); } } } Writer terminalWriter = new Writer(); terminalWriter.reset(); TerminalPosition previousPoint = null; for (TerminalPosition nextUpdate : updateMap.keySet()) { if (previousPoint == null || previousPoint.getRow() != nextUpdate.getRow() || previousPoint.getColumn() + 1 != nextUpdate.getColumn()) { terminalWriter.setCursorPosition(nextUpdate.getColumn(), nextUpdate.getRow()); } terminalWriter.writeCharacter(updateMap.get(nextUpdate)); previousPoint = nextUpdate; } terminalWriter.setCursorPosition( getCursorPosition().getColumn(), getCursorPosition().getRow()); wholeScreenInvalid = false; } terminal.flush(); }
public int compare(TerminalPosition o1, TerminalPosition o2) { if (o1.getRow() == o2.getRow()) if (o1.getColumn() == o2.getColumn()) return 0; else return new Integer(o1.getColumn()).compareTo(o2.getColumn()); else return new Integer(o1.getRow()).compareTo(o2.getRow()); }