Skip to content

Commit aa18faf

Browse files
committed
feat: support shift+enter in terminal
It acts the same alt+enter, but is more familiar to users.
1 parent 28789d7 commit aa18faf

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

site/src/pages/TerminalPage/TerminalPage.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ const TerminalPage: FC = () => {
148148
}),
149149
);
150150

151+
// Make shift+enter send ^[^M (escaped carriage return). Applications
152+
// typically take this to mean to insert a literal newline. There is no way
153+
// to remove this handler, so we must attach it once and rely on a ref to
154+
// send it to the current socket.
155+
terminal.attachCustomKeyEventHandler((ev) => {
156+
if (ev.shiftKey && ev.key === "Enter") {
157+
if (ev.type === "keydown") {
158+
websocketRef.current?.send(
159+
new TextEncoder().encode(JSON.stringify({ data: "\x1b\r" })),
160+
);
161+
}
162+
return false;
163+
}
164+
return true;
165+
});
166+
151167
terminal.open(terminalWrapperRef.current);
152168

153169
// We have to fit twice here. It's unknown why, but the first fit will
@@ -190,6 +206,7 @@ const TerminalPage: FC = () => {
190206
}, [navigate, reconnectionToken, searchParams]);
191207

192208
// Hook up the terminal through a web socket.
209+
const websocketRef = useRef<Websocket>();
193210
useEffect(() => {
194211
if (!terminal) {
195212
return;
@@ -270,6 +287,7 @@ const TerminalPage: FC = () => {
270287
.withBackoff(new ExponentialBackoff(1000, 6))
271288
.build();
272289
websocket.binaryType = "arraybuffer";
290+
websocketRef.current = websocket;
273291
websocket.addEventListener(WebsocketEvent.open, () => {
274292
// Now that we are connected, allow user input.
275293
terminal.options = {
@@ -333,6 +351,7 @@ const TerminalPage: FC = () => {
333351
d.dispose();
334352
}
335353
websocket?.close(1000);
354+
websocketRef.current = undefined;
336355
};
337356
}, [
338357
command,

0 commit comments

Comments
 (0)