【问题标题】:How to get the output via the VSCode terminal API as string? [duplicate]如何通过 VSCode 终端 API 将输出作为字符串获取? [复制]
【发布时间】:2019-07-19 11:43:02
【问题描述】:

VSCode 有一个Windows API,允许与终端交互。

比如你可以发送Linux命令pwd&命令输出可能是/usr/home/userName

我尝试将输出写入磁盘,然后稍后使用pwd > directory.txt 之类的方式读取它;

terminal.sendText(`pwd > directory.txt`);

这似乎可行,但我想知道是否有更优雅的东西。

//Create a new terminal
let terminal = vscode.window.createTerminal(`Name of terminal`, 'C:\path\to\terminal\shell\shell.exe');

// send command to newly created terminal
terminal.sendText(`pwd`);

我确信上面的代码可以工作,因为我可以使用;

将输出写入文件
terminal.sendText(`pwd > directory.txt`);

所以问题是,我如何将terminal.sendText() 的输出作为字符串获取,而不必先将它们写入磁盘?

【问题讨论】:

  • 如果您不需要在 UI 终端中显示正在运行的命令,您可以使用 Node 的 exec / spawn API:stackoverflow.com/questions/20643470/…
  • 我已经用 node-cmd 包尝试过这个,它似乎不适用于 Linux 的 Windows 子系统

标签: typescript visual-studio-code vscode-extensions


【解决方案1】:

vscode 还提供了一个事件来监听正在写入终端的任何数据,使用以下代码来监听终端写入:

vscode.window.onDidWriteTerminalData((e) => {console.log(e.data)})

但它会监听所有写入,因此您必须添加一些条件以防止读取终端上的每个按键,也许您只能在 e.data == \n 或其他条件下读取。

【讨论】:

  • 这只是一个提议的 API,永远不会发布,所以这不再是一个解决方案。
猜你喜欢
  • 2021-07-12
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
相关资源
最近更新 更多