From e9a976f4e82a30713fd93d3afd21d55caeb7e1b5 Mon Sep 17 00:00:00 2001 From: insjang Date: Wed, 2 Sep 2026 21:21:34 +0900 Subject: [PATCH] Terminal: implement the alternate screen buffer (DEC mode 1049) Full screen programs - vi, less, htop, and lately CLIs that draw their own UI - ask for the alternate screen with CSI ? 1049 h and give it back with CSI ? 1049 l. The emulator accepted the sequences and ignored them, so such a program drew over the shell's scrollback and left its last screen behind when it exited. Switching to the alternate screen now saves the normal buffer and the cursor, clears the screen and keeps the buffer exactly as high as the screen, so that scrolling drops the top line instead of growing history the alternate screen is not supposed to have. The cap follows every resize; on a shorter screen the lines above the cursor are dropped, as many as it takes to keep the cursor on the screen. Switching back puts the normal screen back as if it had been resized along with the alternate one, since the window may have been resized in the meantime: as wide as the screen and at least as high; with no history, the unused rows below the cursor go when the screen got shorter; rows the buffer gains are cleared, as they may still hold what the program drew there; and a buffer line limit lowered meanwhile takes the oldest lines, never the screen. The cursor is saved as a line of the normal buffer, not a row of the screen, so it comes back to the same text however the screen has changed, and a wrap pending on the last column is pending again if the line is as wide as it was. The rendition the shell had selected carries over into the alternate screen, as no SGR reset was sent, and comes back with the cursor, as DECSC saves it, so the program's colours do not leak into the shell's output. The restored buffer tells its snapshots so the view redraws instead of keeping the program's last screen. The scroll region is reset only when the screen actually switches back. A reset leaves the alternate screen, so a connection that ended while a full screen program had the screen does not leave the next one on the capped buffer, and so does a full reset (ESC c). It leaves the alternate screen before resetting the rendition, which leaving it puts back. The buffer line limit from the preferences goes through the backend, which keeps it for the normal screen while the alternate one is showing. The modes differ in the cursor as in xterm: 1049 saves it on the way in and restores it on the way out, as DECSC and DECRC do, while 47 and 1047 move it neither way in nor out, a pending wrap included; 1048 saves and restores it as ESC 7 and ESC 8 do. ESC 7 keeps a slot per screen, so a program saving its cursor does not overwrite the one the shell saved. The alternate screen starts blank for all three, where xterm keeps its contents between switches for 47. Asking for the screen one is already on is a no-op, as programs do ask twice. Clear Terminal while a program has the alternate screen clears the normal screen behind it as well, its cursor kept on its row, so leaving the program does not bring back what the user cleared. The buffer line limit is read through the backend as it is written, so it reports the normal screen's limit while the alternate screen shows. Tests run on both TerminalTextDataStore and TerminalTextDataFastScroll, the buffer the terminal actually uses. TerminalTextDataSnapshotTest checks that a copy into the model refreshes its snapshots, at the same size and at another. --- .../emulator/IVT100EmulatorBackend.java | 37 ++++ .../emulator/VT100BackendTraceDecorator.java | 17 ++ .../internal/emulator/VT100Emulator.java | 84 ++++++-- .../emulator/VT100EmulatorBackend.java | 167 ++++++++++++++++ .../emulator/VT100TerminalControl.java | 15 +- .../internal/model/TerminalTextData.java | 7 + .../emulator/MockTerminalControlForText.java | 3 +- .../emulator/VT100EmulatorBackendTest.java | 180 ++++++++++++++++++ .../internal/emulator/VT100EmulatorTest.java | 146 ++++++++++++++ .../model/TerminalTextDataSnapshotTest.java | 36 ++++ 10 files changed, 664 insertions(+), 28 deletions(-) diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java index 08ad52bd45c..7f1d0af4c16 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java @@ -25,6 +25,43 @@ public interface IVT100EmulatorBackend { */ void clearAll(); + /** + * Switches between the screen a shell writes to, which keeps its scrollback, and + * the one a full screen program is given, which has none. Leaving the alternate + * screen puts back exactly what was on the normal one, so a program that takes + * over the display does not cost the user the history behind it. + * + * @param enable whether to show the alternate screen + */ + default void enableAlternateScreen(boolean enable) { + enableAlternateScreen(enable, true); + } + + /** + * As {@link #enableAlternateScreen(boolean)}, for the modes that differ in the + * cursor: 1049 saves it on the way in and restores it on the way out, as DECSC + * and DECRC do, while 47 and 1047 leave it where the program put it. + * + * @param enable whether to show the alternate screen + * @param cursor whether the cursor and its rendition come back with the normal screen + */ + void enableAlternateScreen(boolean enable, boolean cursor); + + /** + * @return how many lines the normal screen keeps, history included, also while the + * alternate screen, which keeps none, is showing + */ + int getBufferLineLimit(); + + /** + * Sets how many lines the normal screen keeps, history included. While the + * alternate screen is showing, the limit is kept for the normal screen and + * applies once it is back, since the alternate screen keeps no history. + * + * @param bufferLineLimit the maximum number of lines, ignored when not positive + */ + void setBufferLineLimit(int bufferLineLimit); + /** * Sets the Dimensions of the addressable scroll space of the screen.... * Keeps the cursor position relative to the bottom of the screen! diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java index a67cbff2f48..7fa3c6f6ad9 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java @@ -38,6 +38,23 @@ public void clearAll() { fBackend.clearAll(); } + @Override + public void enableAlternateScreen(boolean enable, boolean cursor) { + fWriter.println("enableAlternateScreen(" + enable + ", " + cursor + ')'); //$NON-NLS-1$ //$NON-NLS-2$ + fBackend.enableAlternateScreen(enable, cursor); + } + + @Override + public int getBufferLineLimit() { + return fBackend.getBufferLineLimit(); + } + + @Override + public void setBufferLineLimit(int bufferLineLimit) { + fWriter.println("setBufferLineLimit(" + bufferLineLimit + ')'); //$NON-NLS-1$ + fBackend.setBufferLineLimit(bufferLineLimit); + } + @Override public void deleteCharacters(int n) { fWriter.println("deleteCharacters(" + n + ")"); //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java index 659fd5cd4fd..18716a57ae2 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java @@ -119,16 +119,15 @@ public class VT100Emulator implements ControlListener { */ final private IVT100EmulatorBackend text; /** - * This field hold the saved absolute line number of the cursor when - * processing the "ESC 7" and "ESC 8" command sequences. + * The cursor saved by "ESC 7" (and CSI ? 1048 h) for "ESC 8", one slot per screen + * as in xterm: [0] on the normal screen, [1] on the alternate one, so a full screen + * program saving its cursor does not overwrite the one the shell saved. */ - private int savedCursorLine = 0; + private final int[] savedCursorLine = new int[2]; + private final int[] savedCursorColumn = new int[2]; - /** - * This field hold the saved column number of the cursor when processing the - * "ESC 7" and "ESC 8" command sequences. - */ - private int savedCursorColumn = 0; + /** whether a program asked for the alternate screen (47, 1047 or 1049) */ + private boolean alternateScreen; /** * This field holds an array of StringBuffer objects, each of which is one @@ -197,6 +196,31 @@ public void setInputStreamReader(Reader reader) { fReader = reader; } + /** + * @see IVT100EmulatorBackend#setBufferLineLimit(int) + */ + public void setBufferLineLimit(int bufferLineLimit) { + text.setBufferLineLimit(bufferLineLimit); + } + + /** + * @see IVT100EmulatorBackend#getBufferLineLimit() + */ + public int getBufferLineLimit() { + return text.getBufferLineLimit(); + } + + private void saveCursor() { + int screen = alternateScreen ? 1 : 0; + savedCursorLine[screen] = relativeCursorLine(); + savedCursorColumn[screen] = getCursorColumn(); + } + + private void restoreCursor() { + int screen = alternateScreen ? 1 : 0; + moveCursor(savedCursorLine[screen], savedCursorColumn[screen]); + } + public void setDimensions(int lines, int cols) { text.setDimensions(lines, cols); ITerminalConnector telnetConnection = getConnector(); @@ -383,8 +407,7 @@ private void processNewText() throws IOException { // Save cursor position and character attributes ansiState = ANSISTATE_INITIAL; - savedCursorLine = relativeCursorLine(); - savedCursorColumn = getCursorColumn(); + saveCursor(); break; case '8': @@ -392,7 +415,7 @@ private void processNewText() throws IOException { // position ansiState = ANSISTATE_INITIAL; - moveCursor(savedCursorLine, savedCursorColumn); + restoreCursor(); break; case 'c': @@ -486,6 +509,11 @@ private void processNewText() throws IOException { } private void resetTerminal() { + // a full reset (ESC c) puts the normal screen back first, history and all, as a + // reset before a connection does; else the history stays capped away and a + // later CSI ? 1049 l would bring back the screen from before the reset + text.enableAlternateScreen(false); + alternateScreen = false; text.eraseAll(); text.setCursor(0, 0); text.setStyle(text.getDefaultStyle()); @@ -1272,9 +1300,18 @@ private void processDecPrivateCommand_h() { break; case 47: case 1047: - case 1048: + // Use Alternate Screen Buffer, the cursor staying where it is. + text.enableAlternateScreen(true, false); + alternateScreen = true; + break; case 1049: - // Use Alternate Screen Buffer (ignored). + // Save the cursor as DECSC does, then use Alternate Screen Buffer. + text.enableAlternateScreen(true, true); + alternateScreen = true; + break; + case 1048: + // Save cursor, as ESC 7 does. + saveCursor(); break; default: Logger.log("Unsupported command parameter: CSI ?" + param + 'h'); //$NON-NLS-1$ @@ -1291,10 +1328,20 @@ private void processDecPrivateCommand_l() { break; case 47: case 1047: - case 1048: + // Use Normal Screen Buffer, putting back what was on it, the cursor + // staying where the program left it. Leaving also resets the margins, + // which a repeated request must not do to the normal screen. + text.enableAlternateScreen(false, false); + alternateScreen = false; + break; case 1049: - // Use Normal Screen Buffer (ignored, but reset scroll region). - text.setScrollRegion(-1, -1); + // Use Normal Screen Buffer and restore the cursor saved on the way in. + text.enableAlternateScreen(false, true); + alternateScreen = false; + break; + case 1048: + // Restore cursor, as ESC 8 does. + restoreCursor(); break; default: Logger.log("Unsupported command parameter: CSI ?" + param + 'l'); //$NON-NLS-1$ @@ -1561,6 +1608,11 @@ private void moveCursorBackward(int columnsToMove) { */ public void resetState() { ansiState = ANSISTATE_INITIAL; + // a new connection starts on the normal screen, even if the last one ended + // while a full screen program had the alternate one; first, as leaving it puts + // back the shell's rendition, which the reset then resets + text.enableAlternateScreen(false); + alternateScreen = false; text.setStyle(text.getDefaultStyle()); text.setScrollRegion(-1, -1); text.setInsertMode(false); 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 5da57615594..ffcfc404e7b 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 @@ -15,6 +15,7 @@ package org.eclipse.terminal.internal.emulator; import org.eclipse.terminal.internal.model.CharWidth; +import org.eclipse.terminal.internal.model.TerminalTextDataStore; import org.eclipse.terminal.model.ITerminalTextData; import org.eclipse.terminal.model.TerminalStyle; @@ -93,6 +94,22 @@ int getHeight() { int fLines; int fColumns; final private ITerminalTextData fTerminal; + + /** + * What the normal screen held while the alternate one is showing, and null the + * rest of the time, which is also how we know which screen we are on. + */ + private ITerminalTextData fNormalScreen; + private int fNormalMaxHeight; + /** in the normal buffer, not on the screen: the screen may be resized meanwhile */ + private int fNormalCursorLine; + private int fNormalCursorColumn; + /** the screen the normal buffer was saved from, and whether a wrap was pending on it */ + private int fNormalLines; + private int fNormalColumns; + private boolean fNormalWrapPending; + /** the rendition the shell had selected: 1049 saves it with the cursor, as DECSC does */ + private TerminalStyle fNormalStyle; private boolean fVT100LineWrapping; private ScrollRegion fScrollRegion = ScrollRegion.FULL_WINDOW; @@ -100,8 +117,142 @@ public VT100EmulatorBackend(ITerminalTextData terminal) { fTerminal = terminal; } + @Override + public void enableAlternateScreen(boolean enable, boolean cursor) { + synchronized (fTerminal) { + if (enable == (fNormalScreen != null)) { + // Already on the screen being asked for. Programs do ask twice. + return; + } + if (enable) { + fNormalScreen = new TerminalTextDataStore(); + fNormalScreen.copy(fTerminal); + fNormalCursorLine = toAbsoluteLine(fCursorLine); + fNormalCursorColumn = fCursorColumn; + fNormalLines = fLines; + fNormalColumns = fColumns; + fNormalWrapPending = fWrapPending; + fNormalStyle = fStyle; + fNormalMaxHeight = fTerminal.getMaxHeight(); + // clearing leaves the buffer the size of the screen, which is what the + // alternate screen is: no history to scroll back through. Capping the + // buffer there keeps it so: scrolling drops the top line instead of + // growing the buffer, as a program on this screen expects. + clearBuffer(); + fTerminal.setMaxHeight(fLines); + // the rendition carries over: clearing resets it, entering does not + setStyle(fNormalStyle); + if (!cursor) { + // 47 and 1047 move no cursor: it stays on its row and column, a + // wrap pending there still pending + setCursor(fNormalCursorLine - (fNormalScreen.getHeight() - fLines), fNormalCursorColumn); + fWrapPending = fNormalWrapPending; + } + } else { + restoreNormalScreen(cursor); + } + } + } + + /** + * Puts the normal screen back as if it had been resized along with the alternate + * one: the window may have changed while the program had it, and the buffer put + * back is the one from before. Narrower, and every write past its margin throws; + * shorter than the screen, and the top of the screen sits above its first line. + */ + private void restoreNormalScreen(boolean cursorBack) { + // where the program left the cursor, for the modes that keep it there + int programLine = fCursorLine; + int programColumn = fCursorColumn; + boolean programWrapPending = fWrapPending; + TerminalStyle programStyle = fStyle; + ITerminalTextData saved = fNormalScreen; + fNormalScreen = null; + // the margins were the full screen program's, not the shell's + fScrollRegion = ScrollRegion.FULL_WINDOW; + int height = saved.getHeight(); + int cursor = fNormalCursorLine; // a line of the saved buffer + // the height setDimensions would give the normal screen: at least the screen, + // and with no history, the rows below the cursor go when the screen shrinks + int newHeight = Math.max(fLines, height); + if (fLines < fNormalLines && height == fNormalLines) { + newHeight = Math.max(fLines, cursor + 1); + } + fTerminal.setMaxHeight(Math.max(fTerminal.getMaxHeight(), Math.max(height, newHeight))); + fTerminal.copy(saved); + fTerminal.setDimensions(newHeight, fColumns); + // rows the buffer gains are blank: they may still hold what the program drew there + for (int line = height; line < newHeight; line++) { + fTerminal.cleanLine(line); + } + // a limit lowered meanwhile takes the oldest lines, never the screen + int limit = Math.max(fNormalMaxHeight, fLines); + int excess = newHeight - limit; + if (excess > 0) { + fTerminal.scroll(0, newHeight, -excess); + fTerminal.setDimensions(limit, fColumns); + cursor -= excess; + } + fTerminal.setMaxHeight(Math.max(limit, fTerminal.getHeight())); + // back to the line the cursor was on, wherever it is on the screen now + if (!cursorBack) { + // 47 and 1047 save no cursor: it stays on the row and column the program left it + setCursor(programLine, programColumn); + fWrapPending = programWrapPending && fCursorColumn == fColumns - 1; + setStyle(programStyle); + return; + } + setCursor(cursor - (fTerminal.getHeight() - fLines), fNormalCursorColumn); + // and to a wrap that was pending there, if the line is as wide as it was + fWrapPending = fNormalWrapPending && fColumns == fNormalColumns && fCursorColumn == fColumns - 1; + // in the colours the shell was writing in, not the ones the program left selected + setStyle(fNormalStyle); + } + + @Override + public int getBufferLineLimit() { + synchronized (fTerminal) { + // while the alternate screen shows, the model's limit is that screen's height + return fNormalScreen != null ? fNormalMaxHeight : fTerminal.getMaxHeight(); + } + } + + @Override + public void setBufferLineLimit(int bufferLineLimit) { + if (bufferLineLimit <= 0) { + return; + } + synchronized (fTerminal) { + if (fNormalScreen != null) { + // the alternate screen keeps its cap; the normal screen gets the limit back + fNormalMaxHeight = bufferLineLimit; + return; + } + if (fTerminal.getHeight() > bufferLineLimit) { + fTerminal.setDimensions(bufferLineLimit, fTerminal.getWidth()); + } + fTerminal.setMaxHeight(bufferLineLimit); + } + } + @Override public void clearAll() { + synchronized (fTerminal) { + if (fNormalScreen != null) { + // Clearing while a full screen program shows clears what is behind it as + // well, or leaving the program would bring back what the user cleared: + // a blank normal screen, the cursor on the row of it where it was. + int row = fNormalCursorLine - (fNormalScreen.getHeight() - fNormalLines); + fNormalScreen = new TerminalTextDataStore(); + fNormalScreen.setDimensions(fNormalLines, fNormalColumns); + fNormalCursorLine = Math.max(0, Math.min(row, fNormalLines - 1)); + fNormalWrapPending = false; + } + clearBuffer(); + } + } + + private void clearBuffer() { synchronized (fTerminal) { // clear the history int n = fTerminal.getHeight(); @@ -123,6 +274,22 @@ public void setDimensions(int lines, int cols) { // relative cursor line int cl = getCursorLine(); int cc = getCursorColumn(); + if (fNormalScreen != null) { + // The alternate screen has no history, so its buffer stays exactly as + // high as the screen. On a shorter screen the lines above the cursor go + // first, as many as it takes to keep the cursor on it; the cap follows + // once the buffer is no higher than it, which the buffer requires. + int drop = Math.max(0, cl + 1 - lines); + if (drop > 0) { + fTerminal.scroll(0, fTerminal.getHeight(), -drop); + } + fLines = lines; + fColumns = cols; + fTerminal.setDimensions(lines, cols); + fTerminal.setMaxHeight(lines); + setCursor(cl - drop, cc); + return; + } int height = fTerminal.getHeight(); // absolute cursor line int acl = cl + height - fLines; diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java index e80d897f11c..5d4692115b0 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java @@ -1336,20 +1336,15 @@ public void setCommandInputField(ICommandInputField inputField) { @Override public int getBufferLineLimit() { - return fTerminalModel.getMaxHeight(); + // through the emulator as well: while the alternate screen shows, the model's + // limit is that screen's height, and writing it back would cap the history there + return fTerminalText.getBufferLineLimit(); } @Override public void setBufferLineLimit(int bufferLineLimit) { - if (bufferLineLimit <= 0) { - return; - } - synchronized (fTerminalModel) { - if (fTerminalModel.getHeight() > bufferLineLimit) { - fTerminalModel.setDimensions(bufferLineLimit, fTerminalModel.getWidth()); - } - fTerminalModel.setMaxHeight(bufferLineLimit); - } + // through the emulator, which knows whether the alternate screen is showing + fTerminalText.setBufferLineLimit(bufferLineLimit); } @Override diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java index baa3b620df8..78990827baf 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java @@ -262,9 +262,16 @@ public void addLine() { @Override public void copy(ITerminalTextData source) { + int height = getHeight(), width = getWidth(); fData.copy(source); fCursorLine = source.getCursorLine(); fCursorColumn = source.getCursorColumn(); + // Every line and the size of the buffer with them. Without saying so the view + // keeps drawing what it last saw, which is the screen a program left behind. + sendLinesChangedToSnapshot(0, Math.max(height, getHeight())); + if (height != getHeight() || width != getWidth()) { + sendDimensionsChanged(); + } } @Override diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java index 52e8680e2e2..9699c3a98f5 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java @@ -56,8 +56,7 @@ public OutputStream getOutputStream() { @Override public void enableApplicationCursorKeys(boolean enable) { - throw new UnsupportedOperationException(); - + // a full reset (ESC c) turns them off; nothing to record here } } 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 f7bc9d0de6c..95a6848efdd 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 @@ -17,6 +17,7 @@ package org.eclipse.terminal.internal.emulator; import static org.eclipse.terminal.model.TerminalColor.BLACK; +import static org.eclipse.terminal.model.TerminalColor.RED; import static org.eclipse.terminal.model.TerminalColor.WHITE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -24,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.eclipse.terminal.internal.model.TerminalTextData; import org.eclipse.terminal.internal.model.TerminalTextDataStore; import org.eclipse.terminal.internal.model.TerminalTextTestHelper; import org.eclipse.terminal.model.ITerminalTextData; @@ -1375,4 +1377,182 @@ public void testAppendStringWideOneColumn() { vt100.appendString("한"); assertEquals("한", new String(term.getChars(1))); } + + @Test + public void testAlternateScreenBuffer() { + ITerminalTextData term = makeITerminalTextData(); + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(100); + vt100.setDimensions(3, 10); + vt100.setCursor(0, 0); + vt100.appendString("one\r\n"); + for (int i = 0; i < 5; i++) { + vt100.appendString("more"); + vt100.processNewline(); + vt100.setCursorColumn(0); + } + vt100.appendString("last"); + int historyHeight = term.getHeight(); + assertTrue(historyHeight > 3); + + vt100.enableAlternateScreen(true); + // no history to scroll back through, and none accumulates: scrolling drops the top line + assertEquals(3, term.getHeight()); + assertEquals(3, term.getMaxHeight()); + vt100.appendString("alt"); + vt100.setCursor(2, 0); + vt100.processNewline(); + vt100.processNewline(); + assertEquals(3, term.getHeight()); + assertNull(term.getChars(0)); + + // resized while the program had the screen + vt100.setDimensions(5, 7); + vt100.enableAlternateScreen(false); + // the buffer put back is as wide as the screen is now and at least as tall + assertEquals(7, term.getWidth()); + assertTrue(term.getHeight() >= 5); + assertEquals(100, term.getMaxHeight()); + assertEquals("last", new String(term.getChars(term.getHeight() - 1), 0, 4)); + vt100.appendString("x"); // a write past the old margin used to throw + assertEquals(5, vt100.getCursorColumn()); + // the cursor is back on the line it left, now the bottom of a taller screen + assertEquals(4, vt100.getCursorLine()); + assertEquals("lastx", new String(term.getChars(term.getHeight() - 1), 0, 5)); + } + + @Test + public void testAlternateScreenShrunkKeepsNoHistory() { + // also on the buffer the terminal actually uses, which rejects a cap below its height + for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) { + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(100); + vt100.setDimensions(5, 10); + vt100.enableAlternateScreen(true); + vt100.setCursor(4, 0); + vt100.appendString("bottom"); + vt100.setDimensions(3, 10); + assertEquals(3, term.getHeight()); + assertEquals(3, term.getMaxHeight()); + // the lines above the cursor went, the cursor and its line stayed + assertEquals(2, vt100.getCursorLine()); + assertEquals("bottom", new String(term.getChars(2), 0, 6)); + for (int i = 0; i < 6; i++) { + vt100.processNewline(); + vt100.appendString("x"); + } + // scrolling drops the top line instead of growing back to the old height + assertEquals(3, term.getHeight()); + vt100.setDimensions(6, 10); + assertEquals(6, term.getHeight()); + assertEquals(6, term.getMaxHeight()); + vt100.enableAlternateScreen(false); + assertEquals(100, term.getMaxHeight()); + } + } + + @Test + public void testAlternateScreenKeepsALoweredLimit() { + for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) { + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(1000); + vt100.setDimensions(3, 10); + for (int i = 0; i < 750; i++) { + vt100.appendString("l" + i); + vt100.processNewline(); + vt100.setCursorColumn(0); + } + vt100.appendString("last"); + vt100.enableAlternateScreen(true); + vt100.setBufferLineLimit(500); // the preference changes while the program has the screen + vt100.enableAlternateScreen(false); + // the oldest lines went, not the screen, and the limit holds from now on + assertEquals(500, term.getHeight()); + assertEquals(500, term.getMaxHeight()); + assertEquals("last", new String(term.getChars(499), 0, 4)); + assertEquals(2, vt100.getCursorLine()); + assertEquals(4, vt100.getCursorColumn()); + } + } + + @Test + public void testAlternateScreenGrownLeavesNoTextBehind() { + for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) { + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(100); + vt100.setDimensions(3, 10); + vt100.setCursor(0, 0); + vt100.appendString("shell"); + vt100.enableAlternateScreen(true); + vt100.setDimensions(6, 10); + for (int line = 0; line < 6; line++) { + vt100.setCursor(line, 0); + vt100.appendString("program"); + } + vt100.enableAlternateScreen(false); + // the rows the screen gained show nothing of the program + assertEquals(6, term.getHeight()); + assertEquals("shell", new String(term.getChars(0), 0, 5)); + for (int line = 1; line < 6; line++) { + char[] chars = term.getChars(line); + assertTrue(chars == null || new String(chars).trim().replace("\000", "").isEmpty(), "row " + line); + } + } + } + + @Test + public void testAlternateScreenShrunkKeepsTheCursorOnItsLine() { + for (ITerminalTextData term : new ITerminalTextData[] { makeITerminalTextData(), new TerminalTextData() }) { + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(100); + vt100.setDimensions(24, 10); + vt100.setCursor(0, 0); + vt100.appendString("a"); + vt100.setCursor(1, 0); + vt100.appendString("$ "); + vt100.enableAlternateScreen(true); + vt100.setDimensions(12, 10); + vt100.enableAlternateScreen(false); + // no history: the unused rows below the cursor go, and the prompt stays where it was + assertEquals(12, term.getHeight()); + assertEquals(1, vt100.getCursorLine()); + assertEquals(2, vt100.getCursorColumn()); + vt100.appendString("ls"); + assertEquals("$ ls", new String(term.getChars(1), 0, 4)); + } + } + + @Test + public void testAlternateScreenKeepsAPendingWrap() { + ITerminalTextData term = makeITerminalTextData(); + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(10); + vt100.setDimensions(2, 4); + vt100.setVT100LineWrapping(true); + vt100.setCursor(0, 0); + vt100.appendString("abcd"); // the cursor waits on the last column to wrap + vt100.enableAlternateScreen(true); + vt100.enableAlternateScreen(false); + vt100.appendString("e"); + assertEquals("abcd", new String(term.getChars(0), 0, 4)); + assertEquals('e', term.getChar(1, 0)); + } + + @Test + public void testAlternateScreenKeepsTheStyle() { + ITerminalTextData term = makeITerminalTextData(); + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(10); + vt100.setDimensions(2, 8); + vt100.setDefaultStyle(TerminalStyle.getStyle(WHITE, BLACK)); + TerminalStyle red = TerminalStyle.getStyle(RED, BLACK); + vt100.setStyle(red); + vt100.enableAlternateScreen(true); + // entering keeps what was selected: no SGR reset was sent + assertEquals(red, vt100.getStyle()); + vt100.setStyle(TerminalStyle.getStyle(WHITE, BLACK).setBold(true)); // the program's own + vt100.enableAlternateScreen(false); + // the shell goes on in its red, and nothing of the program's bold leaks into it + assertEquals(red, vt100.getStyle()); + } } 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 d7c5692f1a9..b06e26c35ce 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 @@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; import java.io.Reader; @@ -53,10 +54,14 @@ private static String SCROLL_REGION(int startRow, int endRow) { return "\033[" + startRow + ";" + endRow + "r"; } + private static final String ALTERNATE_SCREEN_ON = "\u001b[?1049h"; + private static final String ALTERNATE_SCREEN_OFF = "\u001b[?1049l"; + /** * Set the cursor position to line/column. Note that this is the logical * line and column, so 1, 1 is the top left. */ + private static String CURSOR_POSITION(int line, int column) { return "\033[" + line + ";" + column + "H"; } @@ -375,4 +380,145 @@ public void testMalformedStringTerminator() { () -> assertEquals(List.of("TITLE1", "TITLE2"), control.getAllTitles())); } + @Test + public void testAlternateScreen() { + data.setMaxHeight(1000); + run("Hello 1\r\nHello 2"); + assertAll(() -> assertCursorLocation(1, 7), () -> assertTextEquals("Hello 1", "Hello 2")); + // a full screen program takes the screen: it starts blank and no history is kept + run(ALTERNATE_SCREEN_ON); + assertAll(() -> assertCursorLocation(0, 0), () -> assertTextEquals("")); + assertEquals(WINDOW_LINES, data.getHeight()); + run("Full screen"); + assertTextEquals("Full screen"); + run(ALTERNATE_SCREEN_ON); // programs do ask twice + assertTextEquals("Full screen"); + // and gives it back with what was there before, cursor included + run(ALTERNATE_SCREEN_OFF); + assertAll(() -> assertCursorLocation(1, 7), () -> assertTextEquals("Hello 1", "Hello 2")); + run(ALTERNATE_SCREEN_OFF); // harmless twice as well + assertAll(() -> assertCursorLocation(1, 7), () -> assertTextEquals("Hello 1", "Hello 2")); + } + + @Test + public void testResetStateLeavesAlternateScreen() { + data.setMaxHeight(1000); + run("Hello 1\r\nHello 2"); + run(ALTERNATE_SCREEN_ON); + run("Full screen"); + // the connection ends while the program has the screen; the next one starts + // on the normal screen, with its history back + emulator.resetState(); + assertAll(() -> assertTextEquals("Hello 1", "Hello 2"), () -> assertEquals(1000, data.getMaxHeight())); + // a late request to leave does not bring back the old program's screen + run(ALTERNATE_SCREEN_OFF); + assertTextEquals("Hello 1", "Hello 2"); + for (int i = 0; i < WINDOW_LINES; i++) { + run("\r\nmore"); + } + assertEquals(WINDOW_LINES + 2, data.getHeight()); // history accumulates again + } + + @Test + public void testRepeatedAlternateScreenOffKeepsMargins() { + run("A\r\nB\r\nC"); + run(SCROLL_REGION(1, 2)); + run(ALTERNATE_SCREEN_OFF); // already on the normal screen: must change nothing + run(CURSOR_POSITION(2, 1), "\n"); + // the newline on the bottom margin scrolls rows 1 and 2 only + assertTextEquals("B", "", "C"); + } + + @Test + public void testBufferLineLimitWhileAlternateScreen() { + data.setMaxHeight(1000); + emulator.setBufferLineLimit(300); + assertEquals(300, data.getMaxHeight()); + run(ALTERNATE_SCREEN_ON); + // a preference change must not give the alternate screen a history + emulator.setBufferLineLimit(500); + assertEquals(WINDOW_LINES, data.getMaxHeight()); + for (int i = 0; i < 2 * WINDOW_LINES; i++) { + run("line\r\n"); + } + assertEquals(WINDOW_LINES, data.getHeight()); + // and the new limit is the normal screen's once it is back + run(ALTERNATE_SCREEN_OFF); + assertEquals(500, data.getMaxHeight()); + } + + @Test + public void testFullResetLeavesAlternateScreen() { + data.setMaxHeight(1000); + run("Hello 1\r\nHello 2"); + run(ALTERNATE_SCREEN_ON); + run("Full screen"); + run("\u001bc"); // ESC c, a full reset + assertEquals(1000, data.getMaxHeight()); + // a late request to leave does not bring back the screen from before the reset + run("after"); + run(ALTERNATE_SCREEN_OFF); + assertTextEquals("after"); + } + + @Test + public void testResetStateResetsTheStyleAfterTheAlternateScreen() { + run("\u001b[31m"); + run(ALTERNATE_SCREEN_ON); + emulator.resetState(); + // leaving the alternate screen puts back the red; the reset after it still resets + run("x\u001b[0my"); + int line = data.getHeight() - WINDOW_LINES; + assertEquals(data.getStyle(line, 1), data.getStyle(line, 0)); + } + + @Test + public void testAlternateScreenModesAndTheCursor() { + data.setMaxHeight(1000); + run("Hello 1\r\nHello 2"); + // 47 and 1047 move no cursor, neither in nor out: it stays where it is + run(CURSOR_POSITION(5, 3), "\u001b[?1047h"); + assertCursorLocation(4, 2); + run("x", "\u001b[?1047l"); + assertAll(() -> assertCursorLocation(4, 3), () -> assertTextEquals("Hello 1", "Hello 2")); + run(CURSOR_POSITION(2, 8)); + // 1049 brings it back to where it was on the way in + run(ALTERNATE_SCREEN_ON, CURSOR_POSITION(5, 3), "x", ALTERNATE_SCREEN_OFF); + assertCursorLocation(1, 7); + // ESC 7 / ESC 8 keep a slot per screen: the program's does not overwrite the shell's + run(CURSOR_POSITION(1, 2), "\u001b7", ALTERNATE_SCREEN_ON, CURSOR_POSITION(6, 6), "\u001b7", + CURSOR_POSITION(1, 1), "\u001b8"); + assertCursorLocation(5, 5); + run(ALTERNATE_SCREEN_OFF, CURSOR_POSITION(3, 3), "\u001b8"); + assertCursorLocation(0, 1); + // and 1048 saves and restores as they do + run(CURSOR_POSITION(2, 4), "\u001b[?1048h", CURSOR_POSITION(6, 1), "\u001b[?1048l"); + assertCursorLocation(1, 3); + } + + @Test + public void testClearOnAlternateScreenClearsWhatIsBehindIt() { + data.setMaxHeight(1000); + run("secret 1\r\nsecret 2"); + run(ALTERNATE_SCREEN_ON, "Full screen"); + emulator.clearTerminal(); // Clear Terminal while the program shows + run(ALTERNATE_SCREEN_OFF); + // what the user cleared does not come back with the shell + for (int line = 0; line < data.getHeight(); line++) { + char[] chars = data.getChars(line); + assertTrue(chars == null || !new String(chars).contains("secret"), "line " + line); + } + assertEquals(1000, data.getMaxHeight()); + } + + @Test + public void testBufferLineLimitWhileOnTheAlternateScreen() { + emulator.setBufferLineLimit(1000); + run(ALTERNATE_SCREEN_ON); + // the normal screen's limit, not the alternate screen's height + assertEquals(1000, emulator.getBufferLineLimit()); + emulator.setBufferLineLimit(emulator.getBufferLineLimit()); // read and written back + run(ALTERNATE_SCREEN_OFF); + assertEquals(1000, data.getMaxHeight()); + } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java index 24e4ecb2103..ad9814f5002 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java @@ -1252,4 +1252,40 @@ public void testGetTerminalTextData() { ITerminalTextDataSnapshot snapshot = term.makeSnapshot(); assertSame(term, snapshot.getTerminalTextData()); } + + @Test + public void testCopyRefreshesSnapshotOfTheSameSize() { + ITerminalTextData term = makeITerminalTextData(); + TerminalTextTestHelper.fill(term, "12345\n" + "abcde\n" + "ABCDE"); + ITerminalTextDataSnapshot snapshot = term.makeSnapshot(); + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + // what the alternate screen puts back when a program leaves it + ITerminalTextData source = new TerminalTextDataStore(); + TerminalTextTestHelper.fill(source, "vwxyz\n" + "VWXYZ\n" + "98765"); + term.copy(source); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertEquals(toMultiLineText(source), toMultiLineText(snapshot)); + assertEquals(0, snapshot.getFirstChangedLine()); + assertEquals(term.getHeight() - 1, snapshot.getLastChangedLine()); + assertFalse(snapshot.hasDimensionsChanged()); + } + + @Test + public void testCopyRefreshesSnapshotOfAnotherSize() { + ITerminalTextData term = makeITerminalTextData(); + TerminalTextTestHelper.fill(term, "12345\n" + "abcde\n" + "ABCDE"); + ITerminalTextDataSnapshot snapshot = term.makeSnapshot(); + snapshot.updateSnapshot(false); + ITerminalTextData source = new TerminalTextDataStore(); + TerminalTextTestHelper.fill(source, "vwx\n" + "VWX\n" + "987\n" + "654\n" + "321"); + term.copy(source); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasDimensionsChanged()); + assertEquals(5, snapshot.getHeight()); + assertEquals(3, snapshot.getWidth()); + assertEquals(toMultiLineText(source), toMultiLineText(snapshot)); + } }