Skip to content

Commit 340c1bb

Browse files
CopilotTyriar
andcommitted
Enhance SendSequence command to support manual input and escape sequences
Co-authored-by: Tyriar <[email protected]>
1 parent d3cd900 commit 340c1bb

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/vs/workbench/contrib/terminal/browser/terminalActions.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,48 @@ export async function getCwdForSplit(
121121
}
122122

123123
export const terminalSendSequenceCommand = async (accessor: ServicesAccessor, args: unknown) => {
124-
const instance = accessor.get(ITerminalService).activeInstance;
125-
if (instance) {
126-
const text = isObject(args) && 'text' in args ? toOptionalString(args.text) : undefined;
124+
const terminalService = accessor.get(ITerminalService);
125+
const instance = terminalService.activeInstance || await terminalService.getActiveOrCreateInstance();
126+
if (!instance) {
127+
return;
128+
}
129+
130+
let text = isObject(args) && 'text' in args ? toOptionalString(args.text) : undefined;
131+
132+
// If no text provided, prompt user for input
133+
if (!text) {
134+
const quickInputService = accessor.get(IQuickInputService);
135+
text = await quickInputService.input({
136+
value: '',
137+
placeHolder: 'Enter sequence to send (supports \\n, \\r, \\x escape sequences)',
138+
prompt: localize('workbench.action.terminal.sendSequence.prompt', "Enter sequence to send to the terminal"),
139+
});
127140
if (!text) {
128141
return;
129142
}
130-
const configurationResolverService = accessor.get(IConfigurationResolverService);
131-
const workspaceContextService = accessor.get(IWorkspaceContextService);
132-
const historyService = accessor.get(IHistoryService);
133-
const activeWorkspaceRootUri = historyService.getLastActiveWorkspaceRoot(instance.isRemote ? Schemas.vscodeRemote : Schemas.file);
134-
const lastActiveWorkspaceRoot = activeWorkspaceRootUri ? workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri) ?? undefined : undefined;
135-
const resolvedText = await configurationResolverService.resolveAsync(lastActiveWorkspaceRoot, text);
136-
instance.sendText(resolvedText, false);
137143
}
144+
145+
// Process escape sequences similar to WriteDataToTerminal
146+
let processedText = text
147+
.replace(/\\n/g, '\n')
148+
.replace(/\\r/g, '\r');
149+
150+
// Process hex escape sequences (\xNN)
151+
while (true) {
152+
const match = processedText.match(/\\x([0-9a-fA-F]{2})/);
153+
if (match === null || match.index === undefined || match.length < 2) {
154+
break;
155+
}
156+
processedText = processedText.slice(0, match.index) + String.fromCharCode(parseInt(match[1], 16)) + processedText.slice(match.index + 4);
157+
}
158+
159+
const configurationResolverService = accessor.get(IConfigurationResolverService);
160+
const workspaceContextService = accessor.get(IWorkspaceContextService);
161+
const historyService = accessor.get(IHistoryService);
162+
const activeWorkspaceRootUri = historyService.getLastActiveWorkspaceRoot(instance.isRemote ? Schemas.vscodeRemote : Schemas.file);
163+
const lastActiveWorkspaceRoot = activeWorkspaceRootUri ? workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri) ?? undefined : undefined;
164+
const resolvedText = await configurationResolverService.resolveAsync(lastActiveWorkspaceRoot, processedText);
165+
instance.sendText(resolvedText, false);
138166
};
139167

140168
export class TerminalLaunchHelpAction extends Action {
@@ -957,14 +985,13 @@ export function registerTerminalActions() {
957985
registerTerminalAction({
958986
id: TerminalCommandId.SendSequence,
959987
title: terminalStrings.sendSequence,
960-
f1: false,
988+
f1: true,
961989
metadata: {
962990
description: terminalStrings.sendSequence.value,
963991
args: [{
964992
name: 'args',
965993
schema: {
966994
type: 'object',
967-
required: ['text'],
968995
properties: {
969996
text: {
970997
description: localize('sendSequence', "The sequence of text to send to the terminal"),

0 commit comments

Comments
 (0)