Skip to content

Commit

Permalink
Merge pull request #197099 from hamirmahal/feat/allow-keyboard-shortc…
Browse files Browse the repository at this point in the history
…ut-creation-for-terminal-copy-commands

feat: allow keyboard shortcut creation for terminal copy commands
  • Loading branch information
Tyriar authored Nov 15, 2023
2 parents 9998f63 + 0edd8b4 commit 8cac42e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/vs/workbench/contrib/terminal/browser/terminalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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...' },
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/contrib/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8cac42e

Please sign in to comment.