diff --git a/src/vs/workbench/contrib/terminal/browser/terminalActions.ts b/src/vs/workbench/contrib/terminal/browser/terminalActions.ts index 0b64e8991254e..9e847e0588388 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.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); + 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 d67386c71b349..dff06787f0e4f 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', @@ -513,7 +515,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,