How to use Terminal API to listen to all terminal commands/output in VS Code?
Asked Answered
S

4

8

I want to listen to terminal output from extension, such as tsc -w and catch the moment if the output contains similar text:

Found 1 error. Watching for file changes.

Or the error exit code or something like that. Is it possible to do with old API or Proposed API?

Tried:

terminal.onDidWriteData(data => {
    console.log('onDidWriteData: ', data.trim());
});

It just outputs autogenerated rubbish like:

Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.

Semimonthly answered 23/8, 2019 at 16:56 Comment(0)
V
0

Looks like it is deprecated in insiders edition. Try using window.onDidWriteTerminalData:

window.onDidWriteTerminalData(event => console.log(event.data.trim()))

Reference

Verda answered 23/8, 2019 at 17:4 Comment(0)
G
2

Unfortunately there is no way. There is an event API - window.onDidWriteTerminalData, however it is a "proposed" API which means it's only in the Insiders version of VSCode and you can't use it in a published extension. Basically you can't use it.

According to this comment and this comment although it is "proposed" never going to make it stable due to performance concerns.

As an alternative perhaps you could pipe tsc -w through another program that forwards stdout/stderr and listens for the trigger string. When it sees it it can notify your extension through some other means, e.g. writing a file that you watch or some IPC system.

Unfortunately the downside of that approach is that you can no longer associate the terminal with the VSCode instance, so it might not work if you have multiple VSCode instances running.

Grilse answered 8/10, 2021 at 9:13 Comment(0)
V
0

Looks like it is deprecated in insiders edition. Try using window.onDidWriteTerminalData:

window.onDidWriteTerminalData(event => console.log(event.data.trim()))

Reference

Verda answered 23/8, 2019 at 17:4 Comment(0)
M
0

Shell Integration API was released in v1.93. See release notes. You can find an example here. Some of the related API surface is:

I think you'd use window.onDidStartTerminalShellExecution, which is fired when a terminal command is started in a terminal with shell integration, take the TerminalShellExecutionStartEvent's exection property, which is a TerminalShellExecution, and immediately call read to get a stream of the written data.

Old answer

For performance reasons, API: onDidWriteTerminalData event #78502 is succeeded by Expose shell integration command knowledge to extensions #145234.

The latest draft of the API is in src/vscode-dts/vscode.proposed.terminalShellIntegration.d.ts. I'd quote it in this answer post, but it keeps changing and it's annoying for me to keep track of it and update this.

You can find related release notes here in the release notes for VS Code 1.88 (where the API is now released in the "proposed" status). It includes a couple of API usage examples.

The relevant proposal file if you want to try this out is src/vscode-dts/vscode.proposed.terminalExecuteCommandEvent.d.ts. See also https://code.visualstudio.com/api/advanced-topics/using-proposed-api.

Mambo answered 9/11, 2023 at 20:52 Comment(0)
R
0

Note that there is the proposed API Terminal.shellIntegration.executeCommand in the March 2024 (version 1.88) release. So watch that space.

// Execute a command in a terminal immediately after being created
const myTerm = window.createTerminal();
window.onDidActivateTerminalShellIntegration(async ({ terminal, shellIntegration }) => {
  if (terminal === myTerm) {
    const command = shellIntegration.executeCommand('echo "Hello world"');
    const code = await command.exitCode;
    console.log(`Command exited with code ${code}`);
  }
}));
Rudin answered 16/5, 2024 at 6:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.