Skip to content

Commit d5d6e08

Browse files
MonadChainsTyriar
andauthored
Add command to copy output of the last command (microsoft#152097) (microsoft#153235)
* Add command to copy output of the last command (microsoft#152097) * Polish changes, casing, wording, etc. Co-authored-by: Daniel Imms <[email protected]>
1 parent 2a5d381 commit d5d6e08

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,12 @@ export interface ITerminalInstance {
678678
*/
679679
copySelection(asHtml?: boolean, command?: ITerminalCommand): Promise<void>;
680680

681+
682+
/**
683+
* Copies the ouput of the last command
684+
*/
685+
copyLastCommandOutput(): Promise<void>;
686+
681687
/**
682688
* Current selection in the terminal.
683689
*/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ export function registerTerminalActions() {
327327
}
328328
}
329329
});
330+
registerAction2(class extends Action2 {
331+
constructor() {
332+
super({
333+
id: TerminalCommandId.CopyLastCommand,
334+
title: { value: localize('workbench.action.terminal.copyLastCommand', 'Copy Last Command'), original: 'Copy Last Command' },
335+
f1: true,
336+
category,
337+
precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated)
338+
});
339+
}
340+
async run(accessor: ServicesAccessor): Promise<void> {
341+
await accessor.get(ITerminalService).activeInstance?.copyLastCommandOutput();
342+
}
343+
});
330344
registerAction2(class extends Action2 {
331345
constructor() {
332346
super({

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,21 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
12391239
}
12401240
}
12411241

1242+
async copyLastCommandOutput(): Promise<void> {
1243+
const commands = this.capabilities.get(TerminalCapability.CommandDetection)?.commands;
1244+
if (!commands || commands.length === 0) {
1245+
return;
1246+
}
1247+
const command = commands[commands.length - 1];
1248+
if (!command?.hasOutput) {
1249+
return;
1250+
}
1251+
const output = command.getOutput();
1252+
if (output) {
1253+
await this._clipboardService.writeText(output);
1254+
}
1255+
}
1256+
12421257
get selection(): string | undefined {
12431258
return this.xterm && this.hasSelection() ? this.xterm.raw.getSelection() : undefined;
12441259
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ export const enum TerminalCommandId {
476476
OpenFileLink = 'workbench.action.terminal.openFileLink',
477477
OpenWebLink = 'workbench.action.terminal.openUrlLink',
478478
RunRecentCommand = 'workbench.action.terminal.runRecentCommand',
479+
CopyLastCommand = 'workbench.action.terminal.copyLastCommand',
479480
GoToRecentDirectory = 'workbench.action.terminal.goToRecentDirectory',
480481
CopySelection = 'workbench.action.terminal.copySelection',
481482
CopySelectionAsHtml = 'workbench.action.terminal.copySelectionAsHtml',
@@ -574,6 +575,7 @@ export const DEFAULT_COMMANDS_TO_SKIP_SHELL: string[] = [
574575
TerminalCommandId.Clear,
575576
TerminalCommandId.CopySelection,
576577
TerminalCommandId.CopySelectionAsHtml,
578+
TerminalCommandId.CopyLastCommand,
577579
TerminalCommandId.DeleteToLineStart,
578580
TerminalCommandId.DeleteWordLeft,
579581
TerminalCommandId.DeleteWordRight,

0 commit comments

Comments
 (0)