How do I change the name of the embedded terminal from my VSCode extension when it is created by the user?

Viewed 243

My extension affects the environment in VSCode instance, so I want to help the user identify terminals that contain changes to the environment (after applying my extension) and those that have been created up to this point. To do this I would like to do something like this:

    vscode.window.onDidOpenTerminal((terminal: vscode.Terminal) => {
        if(CONDITION){
        //terminal. = 'label:' + terminal.name; Won't work because the name is a constant
    };
    });

I am not very experienced in writing extensions and could not find any references in the documentation on how best to do this. Is there any way to change the name of a terminal that is created by a user not from my extension?

1 Answers

To rename the current terminal in an extension you could do this:

vscode.window.onDidOpenTerminal((terminal) => {
   if (true) {       // with your condition
     vscode.commands.executeCommand('workbench.action.terminal.renameWithArg', { name: 'myName' });
   }
});

That alone won't distinguish between a terminal opened by the extension from one opened by the user (other than by timing or setting a variable perhaps - first time it is called, second etc. update a global variable).

Related