|
| 1 | +// Xterm.js dependencies via CDN |
| 2 | +const CDN = 'https://cdn.jsdelivr.net/npm'; |
| 3 | +const XTERM = '5.3.0'; |
| 4 | +const ADDON_FIT = '0.10.0'; |
| 5 | +const ADDON_WEB_LINKS = '0.11.0'; |
| 6 | +const READLINE = '1.1.1'; |
| 7 | + |
| 8 | +const { assign } = Object; |
| 9 | + |
| 10 | +const dependencies = ({ ownerDocument }) => { |
| 11 | + const rel = 'stylesheet'; |
| 12 | + const href = `${CDN}/xterm@${XTERM}/css/xterm.min.css`; |
| 13 | + const link = `link[rel="${rel}"][href="${href}"]`; |
| 14 | + if (!ownerDocument.querySelector(link)) { |
| 15 | + ownerDocument.head.append( |
| 16 | + assign(ownerDocument.createElement('link'), { rel, href }) |
| 17 | + ); |
| 18 | + } |
| 19 | + return [ |
| 20 | + import(`${CDN}/xterm@${XTERM}/+esm`), |
| 21 | + import(`${CDN}/@xterm/addon-fit@${ADDON_FIT}/+esm`), |
| 22 | + import(`${CDN}/@xterm/addon-web-links@${ADDON_WEB_LINKS}/+esm`), |
| 23 | + import(`${CDN}/xterm-readline@${READLINE}/+esm`), |
| 24 | + ]; |
| 25 | +}; |
| 26 | + |
| 27 | +export default async ({ interpreter, run }, target) => { |
| 28 | + const libs = dependencies(target); |
| 29 | + |
| 30 | + const namespace = interpreter.globals.get('dict')(); |
| 31 | + |
| 32 | + run(` |
| 33 | + import sys |
| 34 | + from pyodide.ffi import to_js |
| 35 | + from pyodide.console import PyodideConsole, repr_shorten, BANNER |
| 36 | + import __main__ |
| 37 | + BANNER = "Welcome to the Pyodide terminal emulator 🐍\\n" + BANNER |
| 38 | + pyconsole = PyodideConsole(__main__.__dict__) |
| 39 | + import builtins |
| 40 | + async def await_fut(fut): |
| 41 | + res = await fut |
| 42 | + if res is not None: |
| 43 | + builtins._ = res |
| 44 | + return to_js([res], depth=1) |
| 45 | + def clear_console(): |
| 46 | + pyconsole.buffer = [] |
| 47 | + `, { globals: namespace }); |
| 48 | + |
| 49 | + const repr_shorten = namespace.get('repr_shorten'); |
| 50 | + const banner = namespace.get('BANNER'); |
| 51 | + const await_fut = namespace.get('await_fut'); |
| 52 | + const pyconsole = namespace.get('pyconsole'); |
| 53 | + |
| 54 | + const [ |
| 55 | + { Terminal }, |
| 56 | + { FitAddon }, |
| 57 | + { WebLinksAddon }, |
| 58 | + ] = await Promise.all(libs); |
| 59 | + |
| 60 | + const terminal = new Terminal({ |
| 61 | + cursorBlink: true, |
| 62 | + cursorStyle: "block", |
| 63 | + theme: { |
| 64 | + background: "#191A19", |
| 65 | + foreground: "#F5F2E7", |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + let queue = Promise.resolve(''); |
| 70 | + let acc = ''; |
| 71 | + terminal.onData(buffer => { |
| 72 | + terminal.write(buffer); |
| 73 | + acc += buffer; |
| 74 | + if (acc.endsWith('\r')) { |
| 75 | + const line = acc; |
| 76 | + acc = ''; |
| 77 | + const fut = pyconsole.push(line); |
| 78 | + const wrapped = await_fut(fut); |
| 79 | + queue = queue.then(async () => { |
| 80 | + try { |
| 81 | + const [ value ] = await wrapped; |
| 82 | + terminal.write('\r\n'); |
| 83 | + if (value) { |
| 84 | + repr_shorten.callKwargs(value, { |
| 85 | + separator: "\n<long output truncated>\n", |
| 86 | + }); |
| 87 | + terminal.write(String(value) + '\r\n>>> '); |
| 88 | + } |
| 89 | + else { |
| 90 | + terminal.write('... '); |
| 91 | + } |
| 92 | + if (value instanceof interpreter.ffi.PyProxy) |
| 93 | + value.destroy(); |
| 94 | + } |
| 95 | + catch(e) { |
| 96 | + if (e.constructor.name === "PythonError") { |
| 97 | + const message = fut.formatted_error || e.message; |
| 98 | + terminal.write(message.trimEnd().replace(/\n/g, '\r\n>>> ')); |
| 99 | + } else { |
| 100 | + throw e; |
| 101 | + } |
| 102 | + } |
| 103 | + finally { |
| 104 | + fut.destroy(); |
| 105 | + wrapped.destroy(); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + const fitAddon = new FitAddon; |
| 112 | + terminal.loadAddon(fitAddon); |
| 113 | + terminal.loadAddon(new WebLinksAddon); |
| 114 | + terminal.open(target); |
| 115 | + fitAddon.fit(); |
| 116 | + terminal.focus(); |
| 117 | + |
| 118 | + return terminal; |
| 119 | +}; |
0 commit comments