Home > Back-end >  Alternative of vscode.window.onDidWriteTerminalData
Alternative of vscode.window.onDidWriteTerminalData

Time:04-19

The vscode.window.onDidWriteTerminalData event cannot be used when publishing extensions for Visual Studio Code. Does anyone know of an alternative method to capture the text and changes from the built-in terminal to handle this information in a variable?

CodePudding user response:

searching in some places, I checked a NodeJs library called child_process and imported it into my project, after that, I declared a Promise to check the result of the exec method of this library. It was possible to get the execution data and treat it using a regexp to match a text or URL in the terminal.

Example:

function runApp(command) {
  setApplicationProperties();
  outputChannel.show();
  let promise = new Promise(resolve => {
    vscode.window.showInformationMessage(`Running App '${application_name}'...`);   
    let result = child.exec(command, {cwd: getWorkspaceDir()});
    result.stdout.on("data", (data) => {
        outputFilter = data;
        infoCatcher = outputFilter.match(/\w. /gi);
        if(infoCatcher != null) outputChannel.append(`${infoCatcher[0]}\n`);
        urlCatcher = outputFilter.match(/(http|https)?:\/\/. /gi);
        if(urlCatcher != null) vscode.env.openExternal(vscode.Uri.parse(urlCatcher[0]));
        resolve(); 
    });
});
return promise;

}

Thanks for the help.

  • Related