[英]How to get the output via the VSCode terminal API as string?
VSCode has a Windows API that allows interaction with the terminal. VSCode 有一个Windows API ,允许与终端交互。
For example, you can send the Linux command pwd
& the command output may be /usr/home/userName
例如,您可以发送 Linux 命令pwd
& 命令输出可能是/usr/home/userName
I have tried writing the output to disk & then reading it later by using something like pwd > directory.txt
;我尝试将输出写入磁盘,然后稍后使用pwd > directory.txt
类的内容读取它;
terminal.sendText(`pwd > directory.txt`);
This seems to work, but I was wondering if there was something more elegant.这似乎有效,但我想知道是否有更优雅的东西。
//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`);
I know for sure the code above works because I can write the outputs to a file using;我确信上面的代码有效,因为我可以使用以下方法将输出写入文件;
terminal.sendText(`pwd > directory.txt`);
So the question is, how do I get the outputs of terminal.sendText()
as a string without having to first write them to disk?所以问题是,如何将terminal.sendText()
的输出作为字符串获取,而不必先将它们写入磁盘?
vscode also provides an event to listen to any data being written to the terminal, use the following code to listen to the terminal write : vscode 还提供了一个事件来监听任何写入终端的数据,使用以下代码监听终端写入:
vscode.window.onDidWriteTerminalData((e) => {console.log(e.data)})
but it'll listen for all the writes, so you'll have to put in some conditonals to prevent reading every keystroke on the terminal, maybe you can read only when e.data == \\n or other conditions.但它会侦听所有写入,因此您必须添加一些条件以防止读取终端上的每个按键,也许您只能在 e.data == \\n 或其他条件时读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.