Repository: pyscript/pyscript
File: pyscriptjs/src/plugins/pyterminal.ts (TypeScript)

Code before:
            }
            if (this.autoShowOnNextLine) {
                this.classList.remove('py-terminal-hidden');
                this.autoShowOnNextLine = false;
            }
        }

        stderr_writeline(msg: string) {
            this.stdout_writeline(msg);
        }
        // end of the Stdio interface
    }

    return PyTerminalPre;
}

declare const Terminal: typeof TerminalType;

function make_PyTerminal_xterm(app: PyScriptApp) {
    /** The <py-terminal> custom element, which automatically register a stdio
     *  listener to capture and display stdout/stderr
     */
    class PyTerminalXterm extends PyTerminalBaseClass {
        outElem: HTMLDivElement;
        moduleResolved: boolean;
        xtermReadyPromise: Promise<TerminalType>;
        xterm: TerminalType;
        cachedStdOut: Array<string>;
        cachedStdErr: Array<string>;

        constructor() {
            super();
            this.moduleResolved = false;
            this.cachedStdOut = [];
            this.cachedStdErr = [];

            //Required to make xterm appear properly
            this.style.width = '100%';
            this.style.height = '100%';
        }

        async connectedCallback() {
            this.outElem = document.createElement('div');
            //this.outElem.className = 'py-terminal';
            this.appendChild(this.outElem);

            this.setupPosition(app);

            this.xtermReadyPromise = this.setupXterm();
            await this.xtermReadyPromise;
        }

Review comment (suggestion):
Nit: non blocking, just personal taste ... as we deal with custom elements here, there's already a standard naming convention for these kind of promises: `whenDefined().then(...)` and here it could be `whenReady().then(...)` so that `xterm` is not repeated too much.

Code after:
        stdout_writeline(msg: string) {
            this.outElem.innerText += msg + '\n';
            if (this.isDocked()) {
                this.scrollTop = this.scrollHeight;
            }
            if (this.autoShowOnNextLine) {
                this.classList.remove('py-terminal-hidden');
                this.autoShowOnNextLine = false;
            }
        }

        stderr_writeline(msg: string) {
            this.stdout_writeline(msg);
        }
        // end of the Stdio interface
    }

    return PyTerminalPre;
}

declare const Terminal: typeof TerminalType;

function make_PyTerminal_xterm(app: PyScriptApp) {
    /** The <py-terminal> custom element, which automatically register a stdio
     *  listener to capture and display stdout/stderr
     */
    class PyTerminalXterm extends PyTerminalBaseClass {
        outElem: HTMLDivElement;
        _moduleResolved: boolean;
        xtermReady: Promise<TerminalType>;
        xterm: TerminalType;
        cachedStdOut: Array<string>;
        cachedStdErr: Array<string>;
        _xterm_cdn_base_url = 'https://cdn.jsdelivr.net/npm/xterm@5.1.0';

        constructor() {
            super();
            this.cachedStdOut = [];
            this.cachedStdErr = [];

            // While this is false, store writes to stdout/stderr to a buffer
            // when the xterm.js is actually ready, we will "replay" those writes
            // and set this to true
            this._moduleResolved = false;

            //Required to make xterm appear properly
            this.style.width = '100%';
            this.style.height = '100%';
        }

        async connectedCallback() {