Skip to content

Commit

Permalink
Add command to copy output of the last command (microsoft#152097)
Browse files Browse the repository at this point in the history
  • Loading branch information
MonadChains committed Jun 26, 2022
1 parent bc403a0 commit 40ea22d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ export interface ITerminalInstance {
*/
copySelection(asHtml?: boolean, command?: ITerminalCommand): Promise<void>;


/**
* Copies the ouput of the last command
*/
copyLastOutput(): Promise<void>;

/**
* Current selection in the terminal.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,23 @@ export function registerTerminalActions() {
}
}
});
registerAction2(class extends Action2 {
constructor() {
super({
id: TerminalCommandId.CopyLastOutput,
title: { value: localize('workbench.action.terminal.CopyLastOutput', 'Copy output of the last command'), original: 'Copy the output of the last command' },
f1: true,
category,
precondition: ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated)
});
}
async run(accessor: ServicesAccessor): Promise<void> {
const instance = accessor.get(ITerminalService).activeInstance;
if (instance) {
instance.copyLastOutput();
}
}
});
registerAction2(class extends Action2 {
constructor() {
super({
Expand Down
11 changes: 11 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,17 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
}
}

async copyLastOutput(): Promise<void> {
const cmdDetection = this.capabilities.get(TerminalCapability.CommandDetection);
const commands = cmdDetection?.commands;
if (commands && commands.length > 0) {
const output = commands[commands.length - 1]?.getOutput();
if (output) {
await this._clipboardService.writeText(output);
}
}
}

get selection(): string | undefined {
return this.xterm && this.hasSelection() ? this.xterm.raw.getSelection() : undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ export const enum TerminalCommandId {
OpenFileLink = 'workbench.action.terminal.openFileLink',
OpenWebLink = 'workbench.action.terminal.openUrlLink',
RunRecentCommand = 'workbench.action.terminal.runRecentCommand',
CopyLastOutput = 'workbench.action.terminal.CopyLastOutput',
GoToRecentDirectory = 'workbench.action.terminal.goToRecentDirectory',
CopySelection = 'workbench.action.terminal.copySelection',
CopySelectionAsHtml = 'workbench.action.terminal.copySelectionAsHtml',
Expand Down Expand Up @@ -578,6 +579,7 @@ export const DEFAULT_COMMANDS_TO_SKIP_SHELL: string[] = [
TerminalCommandId.Clear,
TerminalCommandId.CopySelection,
TerminalCommandId.CopySelectionAsHtml,
TerminalCommandId.CopyLastOutput,
TerminalCommandId.DeleteToLineStart,
TerminalCommandId.DeleteWordLeft,
TerminalCommandId.DeleteWordRight,
Expand Down

0 comments on commit 40ea22d

Please sign in to comment.