From 26f93898b4e5742aacb346ba42406f88b32a2825 Mon Sep 17 00:00:00 2001 From: Hamir Mahal Date: Mon, 6 Nov 2023 11:24:53 -0800 Subject: [PATCH 1/3] feat: allow users to create new keyboard shortcuts This allows keyboard shortcuts for 1. copying the last command and 2. copying the last command **and** its output Previously, it was only possible for users to copy the last command's output, without copying the last command itself. --- .../terminal/browser/terminalActions.ts | 42 ++++++++++++++++++- .../contrib/terminal/common/terminal.ts | 4 ++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/vs/workbench/contrib/terminal/browser/terminalActions.ts b/src/vs/workbench/contrib/terminal/browser/terminalActions.ts index 9d5896ce8f6..f07693ef409 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminalActions.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminalActions.ts @@ -433,9 +433,27 @@ export function registerTerminalActions() { } }); + registerActiveInstanceAction({ + id: TerminalCommandId.CopyLastCommand, + title: { value: localize('workbench.action.terminal.copyLastCommand', 'Copy Last Command'), original: 'Copy Last Command' }, + precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), + run: async (instance, c, accessor) => { + const clipboardService = accessor.get(IClipboardService); + const commands = instance.capabilities.get(TerminalCapability.CommandDetection)?.commands; + if (!commands || commands.length === 0) { + return; + } + const command = commands[commands.length - 1]; + if (!command.command) { + return; + } + await clipboardService.writeText(command.command); + } + }); + registerActiveInstanceAction({ id: TerminalCommandId.CopyLastCommandOutput, - title: { value: localize('workbench.action.terminal.copyLastCommand', 'Copy Last Command Output'), original: 'Copy Last Command Output' }, + title: { value: localize('workbench.action.terminal.copyLastCommandOutput', 'Copy Last Command Output'), original: 'Copy Last Command Output' }, precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), run: async (instance, c, accessor) => { const clipboardService = accessor.get(IClipboardService); @@ -454,6 +472,28 @@ export function registerTerminalActions() { } }); + registerActiveInstanceAction({ + id: TerminalCommandId.CopyLastCommandAndLastCommandOutput, + title: { value: localize('workbench.action.terminal.copyLastCommandAndLastCommandOutput', 'Copy Last Command And Last Command Output'), original: 'Copy Last Command And Last Command Output' }, + precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), + run: async (instance, c, accessor) => { + const clipboardService = accessor.get(IClipboardService); + const commands = instance.capabilities.get(TerminalCapability.CommandDetection)?.commands; + if (!commands || commands.length === 0) { + return; + } + const command = commands[commands.length - 1]; + if (!command?.hasOutput()) { + return; + } + const output = command.getOutput(); + if (isString(output)) { + await clipboardService.writeText(`${command.command !== '' ? command.command + '\n' : ''}${output}`); + } + } + }); + + registerActiveInstanceAction({ id: TerminalCommandId.GoToRecentDirectory, title: { value: localize('workbench.action.terminal.goToRecentDirectory', "Go to Recent Directory..."), original: 'Go to Recent Directory...' }, diff --git a/src/vs/workbench/contrib/terminal/common/terminal.ts b/src/vs/workbench/contrib/terminal/common/terminal.ts index 69930c200e8..d2c3f91d8e7 100644 --- a/src/vs/workbench/contrib/terminal/common/terminal.ts +++ b/src/vs/workbench/contrib/terminal/common/terminal.ts @@ -410,7 +410,9 @@ export const enum TerminalCommandId { FocusAccessibleBuffer = 'workbench.action.terminal.focusAccessibleBuffer', AccessibleBufferGoToNextCommand = 'workbench.action.terminal.accessibleBufferGoToNextCommand', AccessibleBufferGoToPreviousCommand = 'workbench.action.terminal.accessibleBufferGoToPreviousCommand', + CopyLastCommand = 'workbench.action.terminal.copyLastCommand', CopyLastCommandOutput = 'workbench.action.terminal.copyLastCommandOutput', + CopyLastCommandAndLastCommandOutput = 'workbench.action.terminal.copyLastCommandAndLastCommandOutput', GoToRecentDirectory = 'workbench.action.terminal.goToRecentDirectory', CopyAndClearSelection = 'workbench.action.terminal.copyAndClearSelection', CopySelection = 'workbench.action.terminal.copySelection', @@ -512,7 +514,9 @@ export const DEFAULT_COMMANDS_TO_SKIP_SHELL: string[] = [ TerminalCommandId.CopyAndClearSelection, TerminalCommandId.CopySelection, TerminalCommandId.CopySelectionAsHtml, + TerminalCommandId.CopyLastCommand, TerminalCommandId.CopyLastCommandOutput, + TerminalCommandId.CopyLastCommandAndLastCommandOutput, TerminalCommandId.DeleteToLineStart, TerminalCommandId.DeleteWordLeft, TerminalCommandId.DeleteWordRight, From 1d592f0a15df88659afadfb6a5c160fc450c6f35 Mon Sep 17 00:00:00 2001 From: meganrogge Date: Wed, 15 Nov 2023 09:25:08 -0800 Subject: [PATCH 2/3] rename command --- src/vs/workbench/contrib/terminal/browser/terminalActions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vs/workbench/contrib/terminal/browser/terminalActions.ts b/src/vs/workbench/contrib/terminal/browser/terminalActions.ts index ad60374dd1a..35da4352e33 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminalActions.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminalActions.ts @@ -474,7 +474,7 @@ export function registerTerminalActions() { registerActiveInstanceAction({ id: TerminalCommandId.CopyLastCommandAndLastCommandOutput, - title: { value: localize('workbench.action.terminal.copyLastCommandAndLastCommandOutput', 'Copy Last Command And Last Command Output'), original: 'Copy Last Command And Last Command Output' }, + title: { value: localize('workbench.action.terminal.copyLastCommandAndOutput', 'Copy Last Command and its Output'), original: 'Copy Last Command and its Output' }, precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), run: async (instance, c, accessor) => { const clipboardService = accessor.get(IClipboardService); From 0edd8b42dbbcbb682e59b9d8425466e37daa6420 Mon Sep 17 00:00:00 2001 From: meganrogge Date: Wed, 15 Nov 2023 09:29:40 -0800 Subject: [PATCH 3/3] tweak name --- src/vs/workbench/contrib/terminal/browser/terminalActions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vs/workbench/contrib/terminal/browser/terminalActions.ts b/src/vs/workbench/contrib/terminal/browser/terminalActions.ts index 35da4352e33..9e847e05883 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminalActions.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminalActions.ts @@ -474,7 +474,7 @@ export function registerTerminalActions() { registerActiveInstanceAction({ id: TerminalCommandId.CopyLastCommandAndLastCommandOutput, - title: { value: localize('workbench.action.terminal.copyLastCommandAndOutput', 'Copy Last Command and its Output'), original: 'Copy Last Command and its Output' }, + title: { value: localize('workbench.action.terminal.copyLastCommandAndOutput', 'Copy Last Command and Output'), original: 'Copy Last Command and Output' }, precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), run: async (instance, c, accessor) => { const clipboardService = accessor.get(IClipboardService);