Skip to content

Commit c49150d

Browse files
insjangakurtakov
authored andcommitted
Terminal: keep the screen in the history when it is cleared
Erase in display (CSI 2 J) now moves what is on the screen into the history before erasing it, as Windows Terminal and VTE do, instead of destroying it. Blank lines at the bottom of the screen are left out, except the rows a line runs on to, so the history ends where a line ends, and the cursor does not move. Where the buffer cannot grow, the oldest lines make way; on a buffer capped at the screen height nothing is kept, as before. This matters most on Windows: ConPTY starts every connection with CSI 2 J CSI H, so reconnecting a terminal erased as many lines of the previous session as the screen was high, and a program that clears the screen took the user's last screen of output with it. CSI 3 J still clears the history, so "clear" empties everything as it did. The cursor stays on its row of the screen, in the model as well, however far the screen moved down the buffer. The alternate screen (modes 47, 1047 and 1049) is capped at the screen height and keeps no history, so a program redrawing there with CSI 2 J does not push its old frames into the scrollback: they are gone when the normal screen comes back.
1 parent 164c81a commit c49150d

3 files changed

Lines changed: 142 additions & 2 deletions

File tree

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,45 @@ public void eraseToCursor() {
376376
@Override
377377
public void eraseAll() {
378378
synchronized (fTerminal) {
379+
// What is on the screen goes into the history before it is erased, as in
380+
// Windows Terminal and VTE, so that clearing the screen does not destroy
381+
// what the user was reading. It matters on Windows, where ConPTY clears
382+
// the screen at the start of every connection. Blank lines at the bottom
383+
// of the screen are left out, but not the row a line runs on to: the
384+
// history ends where a line ends. The alternate screen keeps no history,
385+
// so there the lines simply go.
386+
int used = fLines;
387+
while (used > 0 && isBlank(toAbsoluteLine(used - 1))) {
388+
used--;
389+
}
390+
while (used > 0 && used < fLines && fTerminal.isWrappedLine(toAbsoluteLine(used - 1))) {
391+
used++;
392+
}
393+
for (int i = 0; i < used; i++) {
394+
fTerminal.addLine();
395+
}
396+
if (used > 0) {
397+
// the screen moved down the buffer: the cursor stays on its row of it
398+
setCursorLine(fCursorLine);
399+
}
379400
for (int line = toAbsoluteLine(0); line < toAbsoluteLine(fLines); line++) {
380401
fTerminal.cleanLine(line);
381402
}
382403
}
383404
}
384405

406+
private boolean isBlank(int line) {
407+
char[] chars = fTerminal.getChars(line);
408+
if (chars != null) {
409+
for (char c : chars) {
410+
if (c != '\000' && c != ' ') {
411+
return false;
412+
}
413+
}
414+
}
415+
return true;
416+
}
417+
385418
@Override
386419
public void eraseLine() {
387420
synchronized (fTerminal) {

‎terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,31 @@ public void testEraseAll() {
320320
vt100.setDimensions(3, 4);
321321
fill(term, "0000\n" + "1111\n" + "2222\n" + "3333\n" + "4444\n" + "5555");
322322
vt100.eraseAll();
323-
assertEqualsTerm("0000\n" + "1111\n" + "2222\n" + " \n" + " \n" + " ", toMultiLineText(term));
323+
// the screen goes into the history; with no room to grow, the oldest lines make way
324+
assertEqualsTerm("3333\n" + "4444\n" + "5555\n" + " \n" + " \n" + " ", toMultiLineText(term));
325+
}
326+
327+
@Test
328+
public void testEraseAllKeepsTheScreenInHistory() {
329+
// also on the buffer the terminal actually uses
330+
for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) {
331+
IVT100EmulatorBackend vt100 = makeBakend(term);
332+
term.setMaxHeight(100);
333+
vt100.setDimensions(4, 4);
334+
fill(term, "0000\n" + "1111\n" + "2222\n" + "3333\n" + "4444\n" + " ");
335+
vt100.setCursor(2, 1);
336+
vt100.eraseAll();
337+
// the screen was 2222, 3333, 4444 and a blank line, which is not kept
338+
assertEqualsTerm("0000\n" + "1111\n" + "2222\n" + "3333\n" + "4444\n" + " \n" + " \n" + " \n" + " ",
339+
toMultiLineText(term));
340+
// erasing does not move the cursor
341+
assertEquals(2, vt100.getCursorLine());
342+
assertEquals(1, vt100.getCursorColumn());
343+
assertEquals(7, term.getCursorLine()); // the model's cursor too, on the screen's row 2
344+
// a blank screen adds nothing
345+
vt100.eraseAll();
346+
assertEquals(9, term.getHeight());
347+
}
324348
}
325349

326350
@Test
@@ -1555,4 +1579,46 @@ public void testAlternateScreenKeepsTheStyle() {
15551579
// the shell goes on in its red, and nothing of the program's bold leaks into it
15561580
assertEquals(red, vt100.getStyle());
15571581
}
1582+
1583+
@Test
1584+
public void testEraseAllEndsTheHistoryOnALineEnd() {
1585+
// immediate wrapping: the line runs on to the empty row below, or over a row of spaces first
1586+
for (String text : new String[] { "ABCD", "ABCD " }) {
1587+
for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) {
1588+
IVT100EmulatorBackend vt100 = makeBakend(term);
1589+
term.setMaxHeight(100);
1590+
vt100.setDimensions(4, 4);
1591+
vt100.setCursor(0, 0);
1592+
vt100.appendString(text);
1593+
assertTrue(term.isWrappedLine(term.getHeight() - 4));
1594+
vt100.eraseAll();
1595+
// the rows it runs on to go into the history with it, so the history ends on a line end
1596+
int top = term.getHeight() - 4;
1597+
assertFalse(term.isWrappedLine(top - 1), text);
1598+
}
1599+
}
1600+
}
1601+
1602+
@Test
1603+
public void testEraseAllOnTheAlternateScreenKeepsNothing() {
1604+
ITerminalTextData term = makeITerminalTextData();
1605+
IVT100EmulatorBackend vt100 = makeBakend(term);
1606+
term.setMaxHeight(100);
1607+
vt100.setDimensions(3, 6);
1608+
vt100.setCursor(0, 0);
1609+
vt100.appendString("shell");
1610+
int height = term.getHeight();
1611+
vt100.enableAlternateScreen(true);
1612+
vt100.setCursor(0, 0);
1613+
vt100.appendString("frame");
1614+
vt100.eraseAll();
1615+
// a program redrawing there pushes no frames into the history
1616+
assertEquals(3, term.getHeight());
1617+
vt100.enableAlternateScreen(false);
1618+
assertEquals(height, term.getHeight());
1619+
vt100.setCursor(0, 0);
1620+
vt100.appendString("again");
1621+
vt100.eraseAll();
1622+
assertEquals(height + 1, term.getHeight());
1623+
}
15581624
}

‎terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ public void testClear() {
279279
assertAll(() -> assertCursorLocation(0, 0), () -> assertTextEquals(""));
280280
}
281281

282+
@Test
283+
public void testClearScreenKeepsItInHistory() {
284+
data.setMaxHeight(1000);
285+
for (int i = 0; i < 30; i++) {
286+
run("Hello " + i + "\r\n");
287+
}
288+
// what ConPTY sends at the start of every connection on Windows
289+
run(CLEAR_ENTIRE_SCREEN + "\u001b[m" + CURSOR_POSITION_TOP_LEFT + "prompt$ ");
290+
int top = data.getHeight() - WINDOW_LINES; // first line of the screen
291+
assertAll(() -> assertEquals("Hello 29", new String(data.getChars(top - 1)).trim()),
292+
() -> assertEquals("Hello 0", new String(data.getChars(0)).trim()),
293+
() -> assertEquals("prompt$", new String(data.getChars(top)).replace('\000', ' ').trim()),
294+
() -> assertCursorLocation(top, 8)); // the top of the screen, counted in the buffer
295+
}
296+
282297
/**
283298
* Runs what "up arrow" would send back to terminal in less/man/etc.
284299
*/
@@ -458,7 +473,12 @@ public void testFullResetLeavesAlternateScreen() {
458473
// a late request to leave does not bring back the screen from before the reset
459474
run("after");
460475
run(ALTERNATE_SCREEN_OFF);
461-
assertTextEquals("after");
476+
// the reset kept the shell's lines in the history, and the screen shows what came after
477+
assertEquals("after", new String(data.getChars(data.getHeight() - WINDOW_LINES), 0, 5));
478+
for (int line = 0; line < data.getHeight(); line++) {
479+
char[] chars = data.getChars(line);
480+
assertTrue(chars == null || !new String(chars).contains("Full screen"), "line " + line);
481+
}
462482
}
463483

464484
@Test
@@ -521,4 +541,25 @@ public void testBufferLineLimitWhileOnTheAlternateScreen() {
521541
run(ALTERNATE_SCREEN_OFF);
522542
assertEquals(1000, data.getMaxHeight());
523543
}
544+
545+
@Test
546+
public void testRedrawingOnTheAlternateScreenKeepsTheHistory() {
547+
data.setMaxHeight(1000);
548+
run("Hello 1\r\nHello 2");
549+
int height = data.getHeight();
550+
// a full screen program redraws with CSI 2 J; its frames do not go into the history
551+
run(ALTERNATE_SCREEN_ON);
552+
for (int i = 0; i < 5; i++) {
553+
run("\u001b[2J\u001b[H", "frame " + i);
554+
}
555+
run(ALTERNATE_SCREEN_OFF);
556+
assertEquals(height, data.getHeight());
557+
for (int line = 0; line < data.getHeight(); line++) {
558+
char[] chars = data.getChars(line);
559+
assertTrue(chars == null || !new String(chars).contains("frame"), "line " + line);
560+
}
561+
// back on the normal screen, clearing it keeps it again
562+
run("\u001b[2J");
563+
assertEquals(height + 2, data.getHeight());
564+
}
524565
}

0 commit comments

Comments
 (0)