@@ -0,0 +1,109 @@
|
| 1 | +// PyScript py-terminal plugin |
| 2 | +import { hooks } from "../core.js"; |
| 3 | + |
| 4 | +// XXX TODO: |
| 5 | +// 1. these imports should be lazy? |
| 6 | +// 2. would be nice to automatically add xterm.css on demand |
| 7 | +import { Terminal } from 'https://cdn.jsdelivr.net/npm/xterm@5.3.0/+esm' |
| 8 | +import { Readline } from 'https://cdn.jsdelivr.net/npm/xterm-readline@1.1.1/+esm' |
| 9 | +const makePyTerminal = () => { |
| 10 | + const element = document.querySelector('py-terminal'); |
| 11 | + if (element === null) { |
| 12 | + return false; |
| 13 | + } |
| 14 | + |
| 15 | + const term = new Terminal({ |
| 16 | + theme: { |
| 17 | + background: "#191A19", |
| 18 | + foreground: "#F5F2E7", |
| 19 | + }, |
| 20 | + cursorBlink: true, |
| 21 | + cursorStyle: "block", |
| 22 | + rows: 50 |
| 23 | + }); |
| 24 | + |
| 25 | + const rl = new Readline(); |
| 26 | + term.loadAddon(rl); |
| 27 | + term.open(element); |
| 28 | + term.focus(); |
| 29 | + |
| 30 | + async function readline(prompt) { |
| 31 | + //console.log("readline", prompt); |
| 32 | + const line = await rl.read(prompt); |
| 33 | + return line; |
| 34 | + } |
| 35 | + |
| 36 | + async function write(line) { |
| 37 | + //console.log("write", line); |
| 38 | + rl.write(line); |
| 39 | + } |
| 40 | + |
| 41 | + console.log("PyTerminal made?"); |
| 42 | + return { term, readline, write }; |
| 43 | +} |
| 44 | + |
| 45 | +// this is ONLY for non-workers, correct? |
| 46 | +// TODO: how to make it working for workers? |
| 47 | +hooks.onInterpreterReady.add(function override(pyScript) { |
| 48 | + console.log("hello onInterpreterReady"); |
| 49 | + const t = makePyTerminal(); |
| 50 | + if (!t) { |
| 51 | + console.log("<py-terminal> not found, nothing to do"); |
| 52 | + return; |
| 53 | + } |
| 54 | + const { stdout, stderr } = pyScript.io; |
| 55 | + |
| 56 | + pyScript.io.stdout = (s, ...rest) => { |
| 57 | + // XXX: the + "\n" is conceptually wrong. |
| 58 | + // We probably need to configure pyodide's stdout as "raw mode" |
| 59 | + // instead of "batched mode": |
| 60 | + // https://pyodide.org/en/stable/usage/streams.html#a-raw-handler |
| 61 | + t.write(s + "\n"); |
| 62 | + stdout(s, ...rest); |
| 63 | + } |
| 64 | + |
| 65 | + pyScript.io.stderr = (s, ...rest) => { |
| 66 | + t.write(s + "\n"); // see above for the "\n" |
| 67 | + stderr(s, ...rest); |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | + |
| 72 | +// this is mostly pseudo-code for what it *should* happen for the workers case |
| 73 | +/* |
| 74 | +addEventListener("py:ready", (event) => { |
| 75 | + console.log("hello py:ready"); |
| 76 | + if (!event.detail.worker) { |
| 77 | + return; |
| 78 | + } |
| 79 | +
|
| 80 | + const t = makePyTerminal(); |
| 81 | + if (!t) { |
| 82 | + console.log("<py-terminal> not found, nothing to do"); |
| 83 | + return; |
| 84 | + } |
| 85 | +
|
| 86 | + const xworker = event.target.xworker; |
| 87 | +
|
| 88 | + xworker.sync.pyterminal_readline = t.readline; |
| 89 | + xworker.sync.pyterminal_write = t.write; |
| 90 | +
|
| 91 | + // XXX: I know that the following lines don't work, but this is more or |
| 92 | + // lesswhat I would like to happen |
| 93 | + pyScript.io.stdout = (s, ...rest) => { |
| 94 | + // this is JS code, and we cannot send arbitrary JS code from the main |
| 95 | + // to the worker. So maybe a solution is to hardcode this logic |
| 96 | + // directly inside the worker code? |
| 97 | + xworker.sync.pyterminal_write(s); |
| 98 | + } |
| 99 | + pyScript.io.stderr = (s, ...rest) => { |
| 100 | + xworker.sync.pyterminal_write(s); |
| 101 | + } |
| 102 | + something.runPython(` |
| 103 | + import builtins |
| 104 | + import pyscript |
| 105 | + builtins.input = sync.pyterminal_readline |
| 106 | + `) |
| 107 | +
|
| 108 | +}); |
| 109 | +*/ |
0 commit comments