VSCode有一个允许与终端交互的Windows。
例如,您可以发送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()
的输出作为字符串,而不必首先将它们写入磁盘?
发布于 2020-08-14 06:51:19
vscode还提供了一个事件来侦听正在写入终端的任何数据,使用以下代码侦听终端的写入:
vscode.window.onDidWriteTerminalData((e) => {console.log(e.data)})
但是它会监听所有的书写,所以您必须添加一些条件来防止读取终端上的每一个击键,也许您只能在e.data == \n或其他条件下才能阅读。
https://stackoverflow.com/questions/57111832
复制相似问题