3

VSCode has a Windows API that allows interaction with the terminal.

For example, you can send the Linux command pwd & the command output may be /usr/home/userName

I have tried writing the output to disk & then reading it later by using something like 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?

M4X_
  • 497
  • 1
  • 5
  • 12
  • If you don't need to show the command being run in a UI terminal, you can just use Node's exec / spawn APIs: https://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js/20643568 – Gama11 Jul 19 '19 at 11:49
  • I have tried this with the node-cmd package & it doesn't seem to be working on Windows Subsystem for Linux – M4X_ Jul 20 '19 at 09:43

1 Answers1

0

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.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.

abtExp
  • 9
  • 1
  • 14