8000 Introduce a Plugin system, implement <py-terminal> as a plugin by antocuni · Pull Request #917 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Introduce a Plugin system, implement <py-terminal> as a plugin #917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Nov 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
implement the Stdio interface directly inside PyTerminal
  • Loading branch information
antocuni committed Nov 7, 2022
commit ad8233c6ff6566033ee78afacbc0c15999976eb0
46 changes: 19 additions & 27 deletions pyscriptjs/src/plugins/pyterminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,35 @@ export class PyTerminalPlugin {
}
}

/** A Stdio provider which appends messages to an HTMLElement
*/
class ElementStdio implements Stdio {
el: HTMLElement;

constructor(el: HTMLElement) {
this.el = el;
}

stdout_writeline(msg: string) {
this.el.innerText += msg + "\n";
}

stderr_writeline(msg: string) {
this.stdout_writeline(msg);
}

}

function make_PyTerminal(app: PyScriptApp) {

/** The <py-terminal> custom element, which automatically register a stdio
* listener to capture and display stdout/stderr
*/
class PyTerminal extends HTMLElement {
constructor() {
super();
}
class PyTerminal extends HTMLElement implements Stdio {
outElem: HTMLElement;

connectedCallback() {
const outElem = document.createElement('pre');
outElem.className = 'py-terminal';
this.appendChild(outElem);
const stdio = new ElementStdio(outElem);
// should we use a shadowRoot instead? It looks unnecessarily
// complicated to me, but I'm not really sure about the
// implications
this.outElem = document.createElement('pre');
this.outElem.className = 'py-terminal';
this.appendChild(this.outElem);
logger.info('Registering stdio listener');
app.registerStdioListener(stdio);
app.registerStdioListener(this);
}

// implementation of the Stdio interface
stdout_writeline(msg: string) {
this.outElem.innerText += msg + "\n";
}

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

return PyTerminal;
Expand Down
0