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

Code before:
    }

    afterSetup(_interpreter: InterpreterClient) {
        // the Python interpreter has been initialized and we are ready to
        // execute user code:
        //
        //   1. define the "py-terminal" custom element, either a <pre> element
        //      or using xterm.js
        //
        //   2. if there is a <py-terminal> tag on the page, it will register
        //      a Stdio listener just before the user code executes, ensuring
        //      that we capture all the output
        //
        //   3. everything which was written to stdout BEFORE this moment will
        //      NOT be shown on the py-terminal; in particular, pyodide
        //      startup messages will not be shown (but they will go to the
        //      console as usual).
        //
        //   4. (in the future we might want to add an option to start the
        //      capture earlier, but I don't think it's important now).
        const PyTerminal = _interpreter.config.xterm ? make_PyTerminal_xterm(this.app) : make_PyTerminal_pre(this.app);
        customElements.define('py-terminal', PyTerminal);
    }
}

abstract class PyTerminalBaseClass extends HTMLElement implements Stdio {
    autoShowOnNextLine: boolean;

    isAuto() {
        return this.hasAttribute('auto');
    }

    isDocked() {
        return this.hasAttribute('docked');
    }

    setupPosition(app: PyScriptApp) {
        if (this.isAuto()) {
            this.classList.add('py-terminal-hidden');
            this.autoShowOnNextLine = true;
        } else {
            this.autoShowOnNextLine = false;
        }

        if (this.isDocked()) {
            this.classList.add('py-terminal-docked');
        }

        logger.info('Registering stdio listener');
        app.registerStdioListener(this);
    }

Review comment (suggestion):
Just to clarify, as the registry for Custom Elements is globally shared and not mutable (no way to unregister), we're saying that, because there could only be one *py-config* per page (multiple get ignored) there could never be a desired case where a *py-terminal* is sometimes *xterm* and sometimes the *default one* ... right? This is nothing to change, I just wanted to be sure we are aligned in here.

Code after:
            if (docked) termElem.setAttribute('docked', '');
            if (xterm) termElem.setAttribute('xterm', '');
            document.body.appendChild(termElem);
        }
    }

    afterSetup(_interpreter: InterpreterClient) {
        // the Python interpreter has been initialized and we are ready to
        // execute user code:
        //
        //   1. define the "py-terminal" custom element, either a <pre> element
        //      or using xterm.js
        //
        //   2. if there is a <py-terminal> tag on the page, it will register
        //      a Stdio listener just before the user code executes, ensuring
        //      that we capture all the output
        //
        //   3. everything which was written to stdout BEFORE this moment will
        //      NOT be shown on the py-terminal; in particular, pyodide
        //      startup messages will not be shown (but they will go to the
        //      console as usual).
        //
        //   4. (in the future we might want to add an option to start the
        //      capture earlier, but I don't think it's important now).
        const PyTerminal = _interpreter.config.xterm ? make_PyTerminal_xterm(this.app) : make_PyTerminal_pre(this.app);
        customElements.define('py-terminal', PyTerminal);
    }
}

abstract class PyTerminalBaseClass extends HTMLElement implements Stdio {
    autoShowOnNextLine: boolean;

    isAuto() {
        return this.hasAttribute('auto');
    }

    isDocked() {
        return this.hasAttribute('docked');
    }

    setupPosition(app: PyScriptApp) {
        if (this.isAuto()) {
            this.classList.add('py-terminal-hidden');
            this.autoShowOnNextLine = true;
        } else {
            this.autoShowOnNextLine = false;
        }

        if (this.isDocked()) {
            this.classList.add('py-terminal-docked');
        }