diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java index ffcfc404e7b..0eff75b6d99 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java @@ -376,12 +376,45 @@ public void eraseToCursor() { @Override public void eraseAll() { synchronized (fTerminal) { + // What is on the screen goes into the history before it is erased, as in + // Windows Terminal and VTE, so that clearing the screen does not destroy + // what the user was reading. It matters on Windows, where ConPTY clears + // the screen at the start of every connection. Blank lines at the bottom + // of the screen are left out, but not the row a line runs on to: the + // history ends where a line ends. The alternate screen keeps no history, + // so there the lines simply go. + int used = fLines; + while (used > 0 && isBlank(toAbsoluteLine(used - 1))) { + used--; + } + while (used > 0 && used < fLines && fTerminal.isWrappedLine(toAbsoluteLine(used - 1))) { + used++; + } + for (int i = 0; i < used; i++) { + fTerminal.addLine(); + } + if (used > 0) { + // the screen moved down the buffer: the cursor stays on its row of it + setCursorLine(fCursorLine); + } for (int line = toAbsoluteLine(0); line < toAbsoluteLine(fLines); line++) { fTerminal.cleanLine(line); } } } + private boolean isBlank(int line) { + char[] chars = fTerminal.getChars(line); + if (chars != null) { + for (char c : chars) { + if (c != '\000' && c != ' ') { + return false; + } + } + } + return true; + } + @Override public void eraseLine() { synchronized (fTerminal) { diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java index 95a6848efdd..bad8b38d178 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java @@ -320,7 +320,31 @@ public void testEraseAll() { vt100.setDimensions(3, 4); fill(term, "0000\n" + "1111\n" + "2222\n" + "3333\n" + "4444\n" + "5555"); vt100.eraseAll(); - assertEqualsTerm("0000\n" + "1111\n" + "2222\n" + " \n" + " \n" + " ", toMultiLineText(term)); + // the screen goes into the history; with no room to grow, the oldest lines make way + assertEqualsTerm("3333\n" + "4444\n" + "5555\n" + " \n" + " \n" + " ", toMultiLineText(term)); + } + + @Test + public void testEraseAllKeepsTheScreenInHistory() { + // also on the buffer the terminal actually uses + for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) { + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(100); + vt100.setDimensions(4, 4); + fill(term, "0000\n" + "1111\n" + "2222\n" + "3333\n" + "4444\n" + " "); + vt100.setCursor(2, 1); + vt100.eraseAll(); + // the screen was 2222, 3333, 4444 and a blank line, which is not kept + assertEqualsTerm("0000\n" + "1111\n" + "2222\n" + "3333\n" + "4444\n" + " \n" + " \n" + " \n" + " ", + toMultiLineText(term)); + // erasing does not move the cursor + assertEquals(2, vt100.getCursorLine()); + assertEquals(1, vt100.getCursorColumn()); + assertEquals(7, term.getCursorLine()); // the model's cursor too, on the screen's row 2 + // a blank screen adds nothing + vt100.eraseAll(); + assertEquals(9, term.getHeight()); + } } @Test @@ -1555,4 +1579,46 @@ public void testAlternateScreenKeepsTheStyle() { // the shell goes on in its red, and nothing of the program's bold leaks into it assertEquals(red, vt100.getStyle()); } + + @Test + public void testEraseAllEndsTheHistoryOnALineEnd() { + // immediate wrapping: the line runs on to the empty row below, or over a row of spaces first + for (String text : new String[] { "ABCD", "ABCD " }) { + for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) { + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(100); + vt100.setDimensions(4, 4); + vt100.setCursor(0, 0); + vt100.appendString(text); + assertTrue(term.isWrappedLine(term.getHeight() - 4)); + vt100.eraseAll(); + // the rows it runs on to go into the history with it, so the history ends on a line end + int top = term.getHeight() - 4; + assertFalse(term.isWrappedLine(top - 1), text); + } + } + } + + @Test + public void testEraseAllOnTheAlternateScreenKeepsNothing() { + ITerminalTextData term = makeITerminalTextData(); + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(100); + vt100.setDimensions(3, 6); + vt100.setCursor(0, 0); + vt100.appendString("shell"); + int height = term.getHeight(); + vt100.enableAlternateScreen(true); + vt100.setCursor(0, 0); + vt100.appendString("frame"); + vt100.eraseAll(); + // a program redrawing there pushes no frames into the history + assertEquals(3, term.getHeight()); + vt100.enableAlternateScreen(false); + assertEquals(height, term.getHeight()); + vt100.setCursor(0, 0); + vt100.appendString("again"); + vt100.eraseAll(); + assertEquals(height + 1, term.getHeight()); + } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java index b06e26c35ce..12373ff67e1 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java @@ -279,6 +279,21 @@ public void testClear() { assertAll(() -> assertCursorLocation(0, 0), () -> assertTextEquals("")); } + @Test + public void testClearScreenKeepsItInHistory() { + data.setMaxHeight(1000); + for (int i = 0; i < 30; i++) { + run("Hello " + i + "\r\n"); + } + // what ConPTY sends at the start of every connection on Windows + run(CLEAR_ENTIRE_SCREEN + "\u001b[m" + CURSOR_POSITION_TOP_LEFT + "prompt$ "); + int top = data.getHeight() - WINDOW_LINES; // first line of the screen + assertAll(() -> assertEquals("Hello 29", new String(data.getChars(top - 1)).trim()), + () -> assertEquals("Hello 0", new String(data.getChars(0)).trim()), + () -> assertEquals("prompt$", new String(data.getChars(top)).replace('\000', ' ').trim()), + () -> assertCursorLocation(top, 8)); // the top of the screen, counted in the buffer + } + /** * Runs what "up arrow" would send back to terminal in less/man/etc. */ @@ -458,7 +473,12 @@ public void testFullResetLeavesAlternateScreen() { // a late request to leave does not bring back the screen from before the reset run("after"); run(ALTERNATE_SCREEN_OFF); - assertTextEquals("after"); + // the reset kept the shell's lines in the history, and the screen shows what came after + assertEquals("after", new String(data.getChars(data.getHeight() - WINDOW_LINES), 0, 5)); + for (int line = 0; line < data.getHeight(); line++) { + char[] chars = data.getChars(line); + assertTrue(chars == null || !new String(chars).contains("Full screen"), "line " + line); + } } @Test @@ -521,4 +541,25 @@ public void testBufferLineLimitWhileOnTheAlternateScreen() { run(ALTERNATE_SCREEN_OFF); assertEquals(1000, data.getMaxHeight()); } + + @Test + public void testRedrawingOnTheAlternateScreenKeepsTheHistory() { + data.setMaxHeight(1000); + run("Hello 1\r\nHello 2"); + int height = data.getHeight(); + // a full screen program redraws with CSI 2 J; its frames do not go into the history + run(ALTERNATE_SCREEN_ON); + for (int i = 0; i < 5; i++) { + run("\u001b[2J\u001b[H", "frame " + i); + } + run(ALTERNATE_SCREEN_OFF); + assertEquals(height, data.getHeight()); + for (int line = 0; line < data.getHeight(); line++) { + char[] chars = data.getChars(line); + assertTrue(chars == null || !new String(chars).contains("frame"), "line " + line); + } + // back on the normal screen, clearing it keeps it again + run("\u001b[2J"); + assertEquals(height + 2, data.getHeight()); + } }