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

Code before:
            ErrorCode.BAD_CONFIG,
            `Invalid value for config.${name}: the only accepted` +
                `values are true, false and "${dflt}", got "${got}".`,
        );
    }
};

export class PyTerminalPlugin extends Plugin {
    app: PyScriptApp;

    constructor(app: PyScriptApp) {
        super();
        this.app = app;
    }

    configure(config: AppConfigStyle) {
        // validate the terminal config and handle default values
        const { terminal: t, docked: d } = config;
        validate('terminal', 'auto', t);
        validate('docked', 'docked', d);
        if (t === undefined) {
            config.terminal = 'auto'; // default value
        }
        if (d === undefined) {
            config.docked = 'docked'; // default value
        }
    }

    beforeLaunch(config: AppConfigStyle) {
        // if config.terminal is "yes" or "auto", let's add a <py-terminal> to
        // the document, unless it's already present.
        const { terminal: t, docked: d } = config;
        const auto = t === true || t === 'auto';
        const docked = d === true || d === 'docked';
        if (auto && document.querySelector('py-terminal') === null) {
            logger.info('No <py-terminal> found, adding one');
            const termElem = document.createElement('py-terminal');
            if (auto) termElem.setAttribute('auto', '');
            if (docked) termElem.setAttribute('docked', '');
            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
        //
        //   2. if there is a <py-terminal> tag on the page, it will register
        //      a Stdio listener just before the user code executes, ensuring

Review comment (suggestion):
nitpick, but you could also add this logic to `validate`, couldn't you? So that `validate` returns the `default_` value in case you pass `undefined`. Something like this:
```javascript
config.termal = validate('terminal', 'auto', t);
config.docked = validate('docket', 'docket', d);
```

Code after:
        throw new UserError(
            ErrorCode.BAD_CONFIG,
            `Invalid value for config.${name}: the only accepted` +
                `values are true, false and "${default_}", got "${got}".`,
        );
    }
    if (value === undefined) {
        config[name] = default_;
    }
};

export class PyTerminalPlugin extends Plugin {
    app: PyScriptApp;

    constructor(app: PyScriptApp) {
        super();
        this.app = app;
    }

    configure(config: AppConfigStyle) {
        // validate the terminal config and handle default values
        validate(config, 'terminal', 'auto');
        validate(config, 'docked', 'docked');
    }

    beforeLaunch(config: AppConfigStyle) {
        // if config.terminal is "yes" or "auto", let's add a <py-terminal> to
        // the document, unless it's already present.
        const { terminal: t, docked: d } = config;
        const auto = t === true || t === 'auto';
        const docked = d === true || d === 'docked';
        if (auto && document.querySelector('py-terminal') === null) {
            logger.info('No <py-terminal> found, adding one');
            const termElem = document.createElement('py-terminal');
            if (auto) termElem.setAttribute('auto', '');
            if (docked) termElem.setAttribute('docked', '');
            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
        //
        //   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