简体   繁体   English

如何使用终端 API 监听 vscode 中的所有终端输出?

[英]How to use Terminal API to listen to all terminal output in vscode?

I want to listen to terminal output from extension, such as tsc -w and catch the moment if the output contains similar text:我想听扩展的终端输出,例如tsc -w并抓住输出包含类似文本的时刻:

Found 1 error.发现 1 个错误。 Watching for file changes.监视文件更改。

Or the error exit code or something like that.或者错误退出代码或类似的东西。 Is it possible to do with old API or Proposed API?是否可以使用旧的 API 或提议的 API?

Tried:尝试:

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

It just outputs autogenerated rubbish like:它只是输出自动生成的垃圾,如:

Windows PowerShell Copyright (C) Microsoft Corporation. Windows PowerShell 版权所有 (C) Microsoft Corporation。 All rights reserved.版权所有。

Looks like it is deprecated in insiders edition.看起来它在内部人员版本中已被弃用。 Try using window.onDidWriteTerminalData :尝试使用window.onDidWriteTerminalData

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

Reference参考

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.有一个事件 API - window.onDidWriteTerminalData ,但它是一个“建议的”API,这意味着它只在 VSCode 的 Insiders 版本中,你不能在已发布的扩展中使用它。 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.作为替代方案,也许您可​​以通过另一个程序通过管道tsc -w转发 stdout/stderr 并侦听触发字符串。 When it sees it it can notify your extension through some other means, eg writing a file that you watch or some IPC system.当它看到它时,它可以通过其他方式通知您的扩展程序,例如编写您观看的文件或某些 IPC 系统。

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.不幸的是,这种方法的缺点是您不能再将终端与 VSCode 实例相关联,因此如果您有多个 VSCode 实例正在运行,它可能无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM