Open Terminal on Load unless already open

In my onStart I have the following:

if (!this.terminalWidget) {
    // Layout doesn't need to be initialized before we can start a terminal
    if (!this.terminal.getById("terminal")) {
        this.terminalWidget = await this.terminal.newTerminal({
            title: "Terminal",
            destroyTermOnClose: true,
            useServerTitle: false,
            id: "terminal"
        });
        this.terminalWidget.start().then(a => this.terminal.open(this.terminalWidget));
    }
}

The idea is that when they launch Theia it will open a new terminal if (and only if) the terminal isn’t open.

What actually happens is a new terminal is opened every time I refresh.

Additionally, the terminal “tab” title is “Terminal 0”, when it should simply be “Terminal” (as set in the options above).

Any help would be appreciated…

[original thread by Davey Shafik]

Hi @dshafik . Just a theory: maybe workbench layout restoration has not had the chance to restore any previous terminal(s), when you code above runs? Instead of running this on “onStart”, maybe you could try on “initializeLayout”?

@dshafik I was able to get it to work.

  • only opens a terminal if none exist with the same id.

  • if a terminal currently exists, it does not create a new one.

  • the terminal title does display properly.

I created a gist so you can try :slight_smile:

using DOM to determine existence of terminal looks weird. Don’t we have a proper abstraction which can give us all existing terminals?