diff --git a/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF b/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF index 055086fd3d6..9ace9ed4bee 100644 --- a/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF +++ b/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF @@ -19,7 +19,7 @@ Export-Package: org.eclipse.terminal.connector;version="1.0.100"; org.eclipse.swt.widgets, org.eclipse.terminal.control", org.eclipse.terminal.connector.provider;version="1.0.100";uses:="org.eclipse.terminal.connector", - org.eclipse.terminal.control;version="1.0.100"; + org.eclipse.terminal.control;version="1.1.0"; uses:="org.eclipse.swt.dnd, org.eclipse.swt.graphics, org.eclipse.swt.widgets, diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/control/ITerminalViewControl.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/control/ITerminalViewControl.java index b567678cc45..9aa72f8ce76 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/control/ITerminalViewControl.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/control/ITerminalViewControl.java @@ -119,6 +119,21 @@ public interface ITerminalViewControl { */ public boolean pasteString(String string); + /** + * Pastes text as the user pastes it, unlike {@link #pasteString(String)}, which + * writes it as it is, for callers that run commands with it: each line break goes + * as the Enter key sends it, and the text is marked as pasted when the program + * asked for bracketed paste. + * + * @param text the text to paste + * @return whether it was sent, which it is not while the terminal is not + * connected + * @since 1.2 + */ + default boolean paste(String text) { + return pasteString(text); + } + boolean isConnected(); /** diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java index aca2a5690c0..66e4a82b25b 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java @@ -47,4 +47,19 @@ public interface ITerminalControlForText { */ void enableApplicationCursorKeys(boolean enable); + /** + * A program that asks for bracketed paste (CSI ?2004) wants pasted text marked + * as such, so that it can take it as text rather than as something typed. + */ + default void enableBracketedPaste(boolean enable) { + } + + /** + * Shows or hides the cursor at the program's request (DEC mode 25). + * + * @param show whether the cursor is to be drawn + */ + default void showCursor(boolean show) { + } + } 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 a65424ad197..832d6e628dd 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 @@ -41,6 +41,7 @@ import java.io.IOException; import java.io.Reader; import java.util.Arrays; +import java.util.function.IntConsumer; import org.eclipse.core.runtime.Platform; import org.eclipse.swt.events.ControlEvent; @@ -521,6 +522,17 @@ private void resetTerminal() { text.setScrollRegion(-1, -1); text.setInsertMode(false); terminal.enableApplicationCursorKeys(false); + resetProgramModes(); + } + + /** + * The modes a program asked for end with a full reset and with its connection: a + * program that hid the cursor and was cut off must not leave the next shell + * without one. + */ + private void resetProgramModes() { + terminal.showCursor(true); + terminal.enableBracketedPaste(false); } /** @@ -1302,13 +1314,33 @@ private void processAnsiCommand_X() { text.eraseCharacters(getAnsiParameter(0)); } + /** + * Runs the action on each mode of a set or reset. Only the first 16 parameters are + * stored, while every ';' is counted: the ones past them are dropped. + */ + private void forEachMode(IntConsumer action) { + for (int i = 0; i <= nextAnsiParameter && i < ansiParameters.length; i++) { + if (ansiParameters[i].length() > 0) { // an empty one names no mode + action.accept(getAnsiParameter(i)); + } + } + } + private void processDecPrivateCommand_h() { - int param = getAnsiParameter(0); + // A program may set several modes at once: ncurses turns the mouse on with CSI ? 1006 ; 1000 h. + forEachMode(this::setDecPrivateMode); + } + + private void setDecPrivateMode(int param) { switch (param) { case 1: // Enable Application Cursor Keys (DECCKM) terminal.enableApplicationCursorKeys(true); break; + case 25: + // Show cursor (DECTCEM). + terminal.showCursor(true); + break; case 47: case 1047: // Use Alternate Screen Buffer, the cursor staying where it is. @@ -1324,6 +1356,10 @@ private void processDecPrivateCommand_h() { // Save cursor, as ESC 7 does. saveCursor(); break; + case 2004: + // Bracketed paste: pasted text is wrapped so a program can tell it from typing. + terminal.enableBracketedPaste(true); + break; default: Logger.log("Unsupported command parameter: CSI ?" + param + 'h'); //$NON-NLS-1$ break; @@ -1331,12 +1367,19 @@ private void processDecPrivateCommand_h() { } private void processDecPrivateCommand_l() { - int param = getAnsiParameter(0); + forEachMode(this::resetDecPrivateMode); + } + + private void resetDecPrivateMode(int param) { switch (param) { case 1: // Enable Normal Cursor Keys (DECCKM) terminal.enableApplicationCursorKeys(false); break; + case 25: + // Hide cursor (DECTCEM). + terminal.showCursor(false); + break; case 47: case 1047: // Use Normal Screen Buffer, putting back what was on it, the cursor @@ -1354,6 +1397,9 @@ private void processDecPrivateCommand_l() { // Restore cursor, as ESC 8 does. restoreCursor(); break; + case 2004: + terminal.enableBracketedPaste(false); + break; default: Logger.log("Unsupported command parameter: CSI ?" + param + 'l'); //$NON-NLS-1$ break; @@ -1627,6 +1673,7 @@ public void resetState() { text.setStyle(text.getDefaultStyle()); text.setScrollRegion(-1, -1); text.setInsertMode(false); + resetProgramModes(); } // public OutputStream getOutputStream() { 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 e8832552fa9..699f6a18630 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 @@ -172,13 +172,23 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC */ private final IPropertyChangeListener fPreferenceListener = this::updatePreferences; private final IPropertyChangeListener fFontListener = this::updateFont; + // set by the reader job, read by the display thread when pasting + private volatile boolean fBracketedPaste; + // asked for by the program (DECTCEM); kept on the control, as the canvas is new after a move. + // Written with the model it is copied to under fCursorLock, so a model made during a move + // cannot miss a hide or show that the reader job makes meanwhile + private boolean fCursorHidden; + private final Object fCursorLock = new Object(); + private static final String PASTE_START = "\u001b[200~"; //$NON-NLS-1$ + private static final String PASTE_END = "\u001b[201~"; //$NON-NLS-1$ /** * Is protected by synchronize on this */ volatile private Job fJob; - private PollingTextCanvasModel fPollingTextCanvasModel; + // replaced on the display thread when the tab moves, read by the reader job in showCursor + private volatile PollingTextCanvasModel fPollingTextCanvasModel; /** * In some circumstances (e.g PowerShell on Windows) the backspace @@ -294,7 +304,7 @@ public void paste() { private void paste(int clipboardType) { TextTransfer textTransfer = TextTransfer.getInstance(); String strText = (String) fClipboard.getContents(textTransfer, clipboardType); - pasteString(strText); + paste(strText); } /** @@ -312,6 +322,18 @@ public boolean pasteString(String strText) { return true; } + @Override + public boolean paste(String text) { + if (!isConnected() || text == null || text.isEmpty()) { + return false; // nothing to paste: no empty brackets either + } + // a connector that echoes locally (a stream, telnet without remote echo) gets the + // text as it always did: InputStreamMonitor converts the line breaks of a stream + ITerminalConnector connector = getTerminalConnector(); + sendString(connector != null && connector.isLocalEcho() ? text : pasteText(text, fBracketedPaste)); + return true; + } + @Override public void selectAll() { getCtlText().selectAll(); @@ -610,6 +632,30 @@ public static String modifiedSpecialKey(int keyCode, int modifierKeys) { return modifier == 1 || key == null ? null : "\u001b[" + key.number() + ';' + modifier + key.last(); //$NON-NLS-1$ } + /** + * Pasted text as it goes to the program: each line break as the Enter key sends + * it, a CR, as Windows Terminal (CR LF to CR) and tmux (LF to CR) paste. A + * Windows clipboard's CR LF would reach the shell as two breaks, and readline + * then draws two rows for every line: a paste taller than the screen pushes the + * prompt off it, and the redraw on the next key lands where the prompt no + * longer is. With bracketed paste on, the text is marked as pasted. + */ + public static String pasteText(String text, boolean bracketedPaste) { + String lines = text.replace("\r\n", "\r").replace('\n', '\r'); //$NON-NLS-1$ //$NON-NLS-2$ + return bracketedPaste ? bracketed(lines) : lines; + } + + /** + * Marks text as pasted, so that a program takes the newlines in it as part of + * the text rather than as the user pressing return on each line. The text cannot + * end the paste early: every ESC and C1 CSI goes, as xterm does, so no end marker + * can be left over or put together from the pieces around one taken out + * ("ESC[20ESC[201~1~"). + */ + private static String bracketed(String text) { + return PASTE_START + text.replace("\u001b", "").replace("\u009b", "") + PASTE_END; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + } + protected void sendChar(char chKey, boolean altKeyPressed) { try { int byteToSend = chKey; @@ -691,8 +737,9 @@ public void setupTerminal(Composite parent) { fClipboard.dispose(); fPollingTextCanvasModel.stopPolling(); } - // set on this terminal alone (Inverted colors in its menu): kept across a move + // set on this terminal alone (Inverted colors and Scroll Lock in its menu): kept across a move boolean inverted = fCtlText != null && fCtlText.isInvertedColors(); + boolean scrollLock = fCtlText != null && fCtlText.isScrollLock(); if (fWndParent != null && !fWndParent.isDisposed()) { // terminal widget gets a new parent (DnD) fWndParent.dispose(); @@ -715,12 +762,15 @@ public void setupTerminal(Composite parent) { onTerminalColorsChanged(); onTerminalFontChanged(); setInvertedColors(inverted); + setScrollLock(scrollLock); } JFaceResources.getFontRegistry().addListener(fFontListener); setupHelp(fWndParent, TerminalPlugin.HELP_VIEW); if (!wasDisposed) { fState = oldState; + // the new canvas starts with the cursor off: a connected terminal's cursor blinks + fCtlText.setCursorEnabled(isConnected()); } } @@ -816,7 +866,11 @@ protected void setupControls(Composite parent) { ITerminalTextDataSnapshot snapshot = fTerminalModel.makeSnapshot(); // TODO how to get the initial size correctly! snapshot.updateSnapshot(false); - fPollingTextCanvasModel = new PollingTextCanvasModel(snapshot); + synchronized (fCursorLock) { + PollingTextCanvasModel model = new PollingTextCanvasModel(snapshot); + model.setCursorHidden(fCursorHidden); + fPollingTextCanvasModel = model; + } fCtlText = new TextCanvas(fWndParent, fPollingTextCanvasModel, SWT.NONE, new TextLineRenderer(() -> fCtlText, fPollingTextCanvasModel)); @@ -1361,6 +1415,11 @@ public void setState(TerminalState state) { }); } + @Override + public void enableBracketedPaste(boolean enable) { + fBracketedPaste = enable; + } + /** * @param runnable run in display thread */ @@ -1480,6 +1539,21 @@ public String getHoverSelection() { return fCtlText.getHoverSelection(); } + /** @return whether the cursor is drawn when on, not hidden by the program (DECTCEM) */ + public boolean isCursorShown() { + return !fPollingTextCanvasModel.isCursorHidden(); + } + + @Override + public void showCursor(boolean show) { + synchronized (fCursorLock) { + fCursorHidden = !show; + if (fPollingTextCanvasModel != null) { + fPollingTextCanvasModel.setCursorHidden(!show); + } + } + } + @Override public void updateTerminalDimensions() { getTerminalText().adjustTerminalDimensions(); diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java index eabf32d33e2..4792864b406 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java @@ -34,6 +34,8 @@ abstract public class AbstractTextCanvasModel implements ITextCanvasModel { private boolean fShowCursor; private long fCursorTime; private boolean fCursorIsEnabled; + // set by the emulator thread, read when painting + private volatile boolean fCursorHidden; private final ITerminalTextDataSnapshot fSnapshot; private int fLines; @@ -150,7 +152,17 @@ public int getCursorLine() { @Override public boolean isCursorOn() { - return fShowCursor && fCursorIsEnabled; + return fShowCursor && fCursorIsEnabled && !fCursorHidden; + } + + /** @return whether the program hid the cursor, see {@link #setCursorHidden(boolean)} */ + public boolean isCursorHidden() { + return fCursorHidden; + } + + /** A program hides the cursor while it draws, or for good, with DEC mode 25. */ + public void setCursorHidden(boolean hidden) { + fCursorHidden = hidden; } /** diff --git a/terminal/bundles/org.eclipse.terminal.view.ui/META-INF/MANIFEST.MF b/terminal/bundles/org.eclipse.terminal.view.ui/META-INF/MANIFEST.MF index 26ecaadcb4b..d1dc7d0b38a 100644 --- a/terminal/bundles/org.eclipse.terminal.view.ui/META-INF/MANIFEST.MF +++ b/terminal/bundles/org.eclipse.terminal.view.ui/META-INF/MANIFEST.MF @@ -16,7 +16,7 @@ Require-Bundle: org.eclipse.core.expressions;bundle-version="[3.9.0,4.0.0)", org.eclipse.ui.editors;bundle-version="[3.20.0,4.0.0)";resolution:=optional, org.eclipse.text;bundle-version="[3.14.0,4.0.0)";resolution:=optional, org.eclipse.terminal.view.core;bundle-version="[1.0.0,2.0.0)", - org.eclipse.terminal.control;bundle-version="[1.0.0,2.0.0)" + org.eclipse.terminal.control;bundle-version="[1.2.0,2.0.0)" Bundle-RequiredExecutionEnvironment: JavaSE-21 Bundle-ActivationPolicy: lazy Bundle-Localization: plugin @@ -36,7 +36,7 @@ Export-Package: org.eclipse.terminal.view.ui;version="1.0.100", Automatic-Module-Name: org.eclipse.terminal.view.ui Import-Package: org.eclipse.terminal.connector;version="[1.0.0,2.0.0)", org.eclipse.terminal.connector.provider;version="[1.0.0,2.0.0)", - org.eclipse.terminal.control;version="[1.0.0,2.0.0)", + org.eclipse.terminal.control;version="[1.1.0,2.0.0)", org.eclipse.terminal.model;version="[1.0.0,2.0.0)" Service-Component: OSGI-INF/org.eclipse.terminal.view.ui.internal.ConsoleManager.xml, OSGI-INF/org.eclipse.terminal.view.ui.internal.LauncherDelegateManager.xml, diff --git a/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/InputChunks.java b/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/InputChunks.java new file mode 100644 index 00000000000..84cc3de0d5d --- /dev/null +++ b/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/InputChunks.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2026 Eclipse contributors and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.terminal.view.ui.internal; + +/** + * Where to cut the input written to a process in pieces. + */ +public final class InputChunks { + + private InputChunks() { + } + + /** + * How much of the data to write next: at most {@code max} bytes from {@code from}, + * but never ending inside a UTF-8 character or an escape sequence. Between two + * writes there is a pause, and ConPTY has passed the pieces of a sequence split + * across reads on as plain text (microsoft/terminal#16352): the end marker of a + * bracketed paste cut as "ESC[20" and "1~" reached the shell as the text "1~", + * and the shell then took everything typed after it as part of the paste. + * + * @return the number of bytes, at least 1 + */ + public static int writeLength(byte[] data, int from, int max) { + int end = from + max; + if (end >= data.length) { + return data.length - from; + } + while (end > from && (data[end] & 0xC0) == 0x80) { + end--; // a UTF-8 continuation byte: the next write starts at its character + } + // an escape sequence that would not be complete in this write moves to the next + for (int i = end - 1; i >= Math.max(from, end - 32); i--) { + if (data[i] == 0x1b) { + if (!isComplete(data, i, end)) { + end = i; + } + break; + } + } + return end > from ? end - from : Math.min(max, data.length - from); + } + + /** whether the escape sequence at {@code esc} ends before {@code end} */ + private static boolean isComplete(byte[] data, int esc, int end) { + if (esc + 1 >= end) { + return false; + } + byte type = data[esc + 1]; + if (type == '[') { + // CSI: parameters and intermediates up to a final byte + for (int i = esc + 2; i < end; i++) { + if (data[i] >= 0x40 && data[i] <= 0x7e) { + return true; + } + } + return false; + } + if (type == 'O') { + return esc + 2 < end; // SS3, as an arrow key in application mode + } + return true; // ESC and one character (Alt with a key) + } +} diff --git a/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/tabs/TabFolderManager.java b/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/tabs/TabFolderManager.java index e415a31910a..5697d6ed945 100644 --- a/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/tabs/TabFolderManager.java +++ b/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/tabs/TabFolderManager.java @@ -481,7 +481,7 @@ public void mouseDown(MouseEvent e) { } String text = (String) clipboard.getContents(TextTransfer.getInstance(), clipboardType); if (text != null && text.length() > 0) { - terminal.pasteString(text); + terminal.paste(text); } } } diff --git a/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/streams/InputStreamMonitor.java b/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/streams/InputStreamMonitor.java index 6583cdc5176..32bcf481b6b 100644 --- a/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/streams/InputStreamMonitor.java +++ b/terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/streams/InputStreamMonitor.java @@ -23,6 +23,7 @@ import org.eclipse.osgi.util.NLS; import org.eclipse.terminal.connector.ITerminalControl; import org.eclipse.terminal.view.core.ILineSeparatorConstants; +import org.eclipse.terminal.view.ui.internal.InputChunks; import org.eclipse.terminal.view.ui.internal.Messages; import org.eclipse.ui.services.IDisposable; @@ -248,7 +249,8 @@ protected void writeStream() { int written = 0; byte[] buf = new byte[1000]; while (written < data.length) { - int len = Math.min(buf.length, data.length - written); + // not cutting a character or an escape sequence + int len = InputChunks.writeLength(data, written, buf.length); System.arraycopy(data, written, buf, 0, len); // Write the data to the stream stream.write(buf, 0, len); 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 9699c3a98f5..6601d6e8de5 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 @@ -54,9 +54,36 @@ public OutputStream getOutputStream() { } + private boolean cursorShown = true; + private boolean bracketedPaste; + + @Override + public void showCursor(boolean show) { + cursorShown = show; + } + + public boolean isCursorShown() { + return cursorShown; + } + + @Override + public void enableBracketedPaste(boolean enable) { + bracketedPaste = enable; + } + + public boolean isBracketedPaste() { + return bracketedPaste; + } + + private boolean applicationCursorKeys; + @Override public void enableApplicationCursorKeys(boolean enable) { - // a full reset (ESC c) turns them off; nothing to record here + applicationCursorKeys = enable; + } + + public boolean isApplicationCursorKeys() { + return applicationCursorKeys; } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/TerminalPasteUITest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/TerminalPasteUITest.java new file mode 100644 index 00000000000..78d4f1d10e8 --- /dev/null +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/TerminalPasteUITest.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2026 Eclipse contributors and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.terminal.internal.emulator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; + +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.terminal.connector.ITerminalConnector; +import org.eclipse.terminal.connector.ITerminalControl; +import org.eclipse.terminal.connector.TerminalState; +import org.eclipse.terminal.connector.provider.AbstractTerminalConnector; +import org.eclipse.terminal.control.ITerminalListener; +import org.eclipse.terminal.control.TerminalTitleRequestor; +import org.eclipse.terminal.internal.connector.TerminalConnector; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * What reaches the program when text is pasted, and when a caller writes it with + * pasteString() to run a command. + */ +public class TerminalPasteUITest { + + private static final String START = "\u001b[200~", END = "\u001b[201~"; //$NON-NLS-1$ //$NON-NLS-2$ + + private Display display; + private Shell shell; + private VT100TerminalControl terminal; + private final ByteArrayOutputStream sent = new ByteArrayOutputStream(); + private boolean localEcho; + + @BeforeEach + public void createTerminal() { + display = Display.getCurrent() != null ? null : new Display(); + shell = new Shell(); + shell.setLayout(new FillLayout()); + ITerminalListener listener = new ITerminalListener() { + @Override + public void setState(TerminalState state) { + } + + @Override + public void setTerminalSelectionChanged() { + } + + @Override + public void setTerminalTitle(String title, TerminalTitleRequestor requestor) { + } + }; + terminal = new VT100TerminalControl(listener, new Composite(shell, 0), new ITerminalConnector[0]); + } + + @AfterEach + public void dispose() { + terminal.disposeTerminal(); + shell.dispose(); + if (display != null) { + display.dispose(); + } + } + + private void connect(boolean echo) { + localEcho = echo; + AbstractTerminalConnector connector = new AbstractTerminalConnector() { + @Override + public void connect(ITerminalControl control) { + super.connect(control); + control.setState(TerminalState.CONNECTED); + } + + @Override + public boolean isLocalEcho() { + return localEcho; + } + + @Override + public OutputStream getTerminalToRemoteStream() { + return sent; + } + + @Override + public String getSettingsSummary() { + return ""; //$NON-NLS-1$ + } + }; + ITerminalConnector wrapped = new TerminalConnector(() -> connector, "test", "test", false); //$NON-NLS-1$ //$NON-NLS-2$ + terminal.setConnector(wrapped); + wrapped.connect(terminal); + assertTrue(terminal.isConnected()); + } + + private String taken() { + String text = sent.toString(StandardCharsets.UTF_8); + sent.reset(); + return text; + } + + @Test + public void pasteSendsLineBreaksAsEnterAndBrackets() { + connect(false); + terminal.paste("a\r\nb\n"); //$NON-NLS-1$ + assertEquals("a\rb\r", taken()); //$NON-NLS-1$ + terminal.enableBracketedPaste(true); + terminal.paste("a\nb"); //$NON-NLS-1$ + assertEquals(START + "a\rb" + END, taken()); //$NON-NLS-1$ + } + + @Test + public void pasteStringWritesTheTextAsItIs() { + // as CDT runs gdb commands, and the command input field sends its line: never + // bracketed, so the shell runs the line instead of inserting it + connect(false); + terminal.enableBracketedPaste(true); + terminal.pasteString("info threads\n"); //$NON-NLS-1$ + assertEquals("info threads\n", taken()); //$NON-NLS-1$ + } + + @Test + public void pasteToALocalEchoConnectorLeavesTheLineBreaks() { + // InputStreamMonitor converts them for such a connector, from the text as it was + connect(true); + terminal.paste("a\nb\n"); //$NON-NLS-1$ + assertEquals("a\nb\n", taken()); //$NON-NLS-1$ + } +} 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 a1b6365b4d6..32366a9f172 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 @@ -616,4 +616,66 @@ public void testModifiedSpecialKeys() { assertEquals(null, VT100TerminalControl.modifiedSpecialKey(SWT.ARROW_LEFT, SWT.COMMAND)); assertEquals(null, VT100TerminalControl.modifiedSpecialKey(SWT.ARROW_LEFT, SWT.COMMAND | SWT.SHIFT)); } + + @Test + public void testDecPrivateModes() { + // the cursor hidden and shown again by a program + run("\u001b[?25l"); + assertFalse(control.isCursorShown()); + run("\u001b[?25h"); + assertTrue(control.isCursorShown()); + // several modes in one sequence, as ncurses sends them: every one of them counts + run("\u001b[?2004;25l"); + assertAll(() -> assertFalse(control.isCursorShown()), () -> assertFalse(control.isBracketedPaste())); + run("\u001b[?25;2004h"); + assertAll(() -> assertTrue(control.isCursorShown()), () -> assertTrue(control.isBracketedPaste())); + } + + @Test + public void testResetStateEndsDecPrivateModes() { + run("\u001b[?25l\u001b[?2004h"); + assertAll(() -> assertFalse(control.isCursorShown()), () -> assertTrue(control.isBracketedPaste())); + // the connection ends while the program has them set; the next one starts clean + emulator.resetState(); + assertAll(() -> assertTrue(control.isCursorShown()), () -> assertFalse(control.isBracketedPaste())); + } + + @Test + public void testEmptyDecPrivateModeNamesNoMode() { + // CSI ? 2004 ; h: the empty parameter is no mode, not 1 (application cursor keys) + run("\u001b[?2004;h"); + assertAll(() -> assertTrue(control.isBracketedPaste()), () -> assertFalse(control.isApplicationCursorKeys())); + } + + @Test + public void testModesPastTheSixteenthAreDropped() { + // 17 modes: the first 16 are taken, the 17th is dropped, and the sequence does not + // stop the text after it + run("\u001b[?25l\u001b[?" + "2004;".repeat(16) + "25hx"); + assertAll(() -> assertTrue(control.isBracketedPaste()), () -> assertFalse(control.isCursorShown())); + run("\u001b[?25h\u001b[?" + "2004;".repeat(16) + "25ly"); + assertAll(() -> assertFalse(control.isBracketedPaste()), () -> assertTrue(control.isCursorShown()), + () -> assertTextEquals("xy")); + } + + @Test + public void testFullResetEndsDecPrivateModes() { + run("\u001b[?25l\u001b[?2004h"); + // ESC c (RIS) resets every mode a program set, as a new connection does + run("\u001bc"); + assertAll(() -> assertTrue(control.isCursorShown()), () -> assertFalse(control.isBracketedPaste())); + } + + @Test + public void testPasteText() { + // each line break goes as a CR, as the Enter key sends it: a Windows CR LF is one break + assertEquals("a\rb\rc\rd", VT100TerminalControl.pasteText("a\r\nb\nc\rd", false)); + String start = "\033[200~", end = "\033[201~"; + assertEquals(start + "a\rb" + end, VT100TerminalControl.pasteText("a\r\nb", true)); + // the text cannot end the paste early: every ESC and C1 CSI goes + assertEquals(start + "a[201~b" + end, VT100TerminalControl.pasteText("a\033[201~b", true)); + // not even with a marker put together from the pieces left after taking one out + assertEquals(start + "[20[201~1~x" + end, VT100TerminalControl.pasteText("\033[20\033[201~1~x", true)); + assertEquals(start + "201~x" + end, VT100TerminalControl.pasteText("\u009b201~x", true)); + } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java index 4820462fdab..5a5931d3424 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java @@ -12,6 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -103,12 +104,34 @@ public void recreatedCanvasKeepsThePreferences() { public void recreatedCanvasKeepsWhatWasSetOnThisTerminal() { // set on this terminal, not in the preferences: a move must not reset them to the preferences terminal.setInvertedColors(true); + terminal.setScrollLock(true); terminal.setBufferLineLimit(123); recreateInNewParent(); assertTrue(terminal.isInvertedColors()); + assertTrue(terminal.isScrollLock()); assertEquals(123, terminal.getBufferLineLimit()); } + @Test + public void recreatedCanvasKeepsTheCursorTheProgramHid() { + // CSI ? 25 l: a program hid the cursor; the new canvas must not show it again + terminal.showCursor(false); + recreateInNewParent(); + assertFalse(terminal.isCursorShown()); + terminal.showCursor(true); + assertTrue(terminal.isCursorShown()); + } + + @Test + public void recreatedCanvasOfAConnectedTerminalBlinksTheCursor() { + terminal.setState(TerminalState.CONNECTED); + while (Display.getCurrent().readAndDispatch()) { + // let setState enable the cursor + } + recreateInNewParent(); + assertTrue(((TextCanvas) terminal.getControl()).isCursorEnabled()); + } + @Test public void recreatedWithoutPreferencesKeepsTheInvertedColors() { // a control made without a preference store, as TerminalViewControlFactory's default makes it diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java index a6c8b0970ab..e8673b2cf64 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java @@ -27,6 +27,8 @@ org.eclipse.terminal.internal.connector.TerminalConnectorTest.class, // org.eclipse.terminal.internal.connector.TerminalToRemoteInjectionOutputStreamTest.class, // org.eclipse.terminal.internal.textcanvas.TerminalControlRecreateUITest.class, // + org.eclipse.terminal.internal.emulator.TerminalPasteUITest.class, // + org.eclipse.terminal.view.ui.tests.InputChunksTest.class, // org.eclipse.terminal.view.ui.tests.TerminalsViewReorderTest.class, // }) public class AutomatedTestSuite { diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/view/ui/tests/InputChunksTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/view/ui/tests/InputChunksTest.java new file mode 100644 index 00000000000..914a50ff7e6 --- /dev/null +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/view/ui/tests/InputChunksTest.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2026 Eclipse contributors and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.terminal.view.ui.tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.nio.charset.StandardCharsets; + +import org.eclipse.terminal.view.ui.internal.InputChunks; +import org.junit.jupiter.api.Test; + +public class InputChunksTest { + + private static byte[] bytes(String s) { + return s.getBytes(StandardCharsets.UTF_8); + } + + @Test + public void testWritesDoNotCutAnEscapeSequence() { + // the end marker of a bracketed paste across the 1000th byte goes whole into the next write + byte[] cut = bytes("x".repeat(996) + "\033[201~more"); + assertEquals(996, InputChunks.writeLength(cut, 0, 1000)); + assertEquals(10, InputChunks.writeLength(cut, 996, 1000)); + // one that ends before the cut stays in this write + assertEquals(1000, InputChunks.writeLength(bytes("x".repeat(990) + "\033[201~" + "y".repeat(20)), 0, 1000)); + // an arrow key in application mode, ESC O A + assertEquals(998, InputChunks.writeLength(bytes("x".repeat(998) + "\033OAz"), 0, 1000)); + } + + @Test + public void testWritesDoNotCutACharacter() { + // the 1000th byte is inside a three byte character: the write stops before it + assertEquals(999, InputChunks.writeLength(bytes("x".repeat(999) + "한" + "y"), 0, 1000)); + assertEquals(1000, InputChunks.writeLength(bytes("x".repeat(997) + "한" + "y"), 0, 1000)); + } + + @Test + public void testShortWritesAndLongSequences() { + assertEquals(5, InputChunks.writeLength(bytes("hello"), 0, 1000)); + // nothing to cut at: the write is as long as allowed rather than empty + assertEquals(4, InputChunks.writeLength(bytes("\033[" + "1".repeat(10)), 0, 4)); + } +}