Skip to content

Commit 8cac42e

Browse files
authored
Merge pull request #197099 from hamirmahal/feat/allow-keyboard-shortcut-creation-for-terminal-copy-commands
feat: allow keyboard shortcut creation for terminal copy commands
2 parents 9998f63 + 0edd8b4 commit 8cac42e

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,27 @@ export function registerTerminalActions() {
433433
}
434434
});
435435

436+
registerActiveInstanceAction({
437+
id: TerminalCommandId.CopyLastCommand,
438+
title: { value: localize('workbench.action.terminal.copyLastCommand', 'Copy Last Command'), original: 'Copy Last Command' },
439+
precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated),
440+
run: async (instance, c, accessor) => {
441+
const clipboardService = accessor.get(IClipboardService);
442+
const commands = instance.capabilities.get(TerminalCapability.CommandDetection)?.commands;
443+
if (!commands || commands.length === 0) {
444+
return;
445+
}
446+
const command = commands[commands.length - 1];
447+
if (!command.command) {
448+
return;
449+
}
450+
await clipboardService.writeText(command.command);
451+
}
452+
});
453+
436454
registerActiveInstanceAction({
437455
id: TerminalCommandId.CopyLastCommandOutput,
438-
title: { value: localize('workbench.action.terminal.copyLastCommand', 'Copy Last Command Output'), original: 'Copy Last Command Output' },
456+
title: { value: localize('workbench.action.terminal.copyLastCommandOutput', 'Copy Last Command Output'), original: 'Copy Last Command Output' },
439457
precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated),
440458
run: async (instance, c, accessor) => {
441459
const clipboardService = accessor.get(IClipboardService);
@@ -454,6 +472,28 @@ export function registerTerminalActions() {
454472
}
455473
});
456474

475+
registerActiveInstanceAction({
476+
id: TerminalCommandId.CopyLastCommandAndLastCommandOutput,
477+
title: { value: localize('workbench.action.terminal.copyLastCommandAndOutput', 'Copy Last Command and Output'), original: 'Copy Last Command and Output' },
478+
precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated),
479+
run: async (instance, c, accessor) => {
480+
const clipboardService = accessor.get(IClipboardService);
481+
const commands = instance.capabilities.get(TerminalCapability.CommandDetection)?.commands;
482+
if (!commands || commands.length === 0) {
483+
return;
484+
}
485+
const command = commands[commands.length - 1];
486+
if (!command?.hasOutput()) {
487+
return;
488+
}
489+
const output = command.getOutput();
490+
if (isString(output)) {
491+
await clipboardService.writeText(`${command.command !== '' ? command.command + '\n' : ''}${output}`);
492+
}
493+
}
494+
});
495+
496+
457497
registerActiveInstanceAction({
458498
id: TerminalCommandId.GoToRecentDirectory,
459499
title: { value: localize('workbench.action.terminal.goToRecentDirectory', "Go to Recent Directory..."), original: 'Go to Recent Directory...' },

src/vs/workbench/contrib/terminal/common/terminal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ export const enum TerminalCommandId {
410410
FocusAccessibleBuffer = 'workbench.action.terminal.focusAccessibleBuffer',
411411
AccessibleBufferGoToNextCommand = 'workbench.action.terminal.accessibleBufferGoToNextCommand',
412412
AccessibleBufferGoToPreviousCommand = 'workbench.action.terminal.accessibleBufferGoToPreviousCommand',
413+
CopyLastCommand = 'workbench.action.terminal.copyLastCommand',
413414
CopyLastCommandOutput = 'workbench.action.terminal.copyLastCommandOutput',
415+
CopyLastCommandAndLastCommandOutput = 'workbench.action.terminal.copyLastCommandAndLastCommandOutput',
414416
GoToRecentDirectory = 'workbench.action.terminal.goToRecentDirectory',
415417
CopyAndClearSelection = 'workbench.action.terminal.copyAndClearSelection',
416418
CopySelection = 'workbench.action.terminal.copySelection',
@@ -513,7 +515,9 @@ export const DEFAULT_COMMANDS_TO_SKIP_SHELL: string[] = [
513515
TerminalCommandId.CopyAndClearSelection,
514516
TerminalCommandId.CopySelection,
515517
TerminalCommandId.CopySelectionAsHtml,
518+
TerminalCommandId.CopyLastCommand,
516519
TerminalCommandId.CopyLastCommandOutput,
520+
TerminalCommandId.CopyLastCommandAndLastCommandOutput,
517521
TerminalCommandId.DeleteToLineStart,
518522
TerminalCommandId.DeleteWordLeft,
519523
TerminalCommandId.DeleteWordRight,

0 commit comments

Comments
 (0)