| 82 | // common main thread initialization for both worker |
| 83 | // or main case, bootstrapping the terminal on its target |
| 84 | const init = () => { |
| 85 | let target = element; |
| 86 | const selector = target.getAttribute("target"); |
| 87 | if (selector) { |
| 88 | target = |
| 89 | document.getElementById(selector) || |
| 90 | document.querySelector(selector); |
| 91 | if (!target) throw new Error(`Unknown target ${selector}`); |
| 92 | } else { |
| 93 | target = document.createElement("py-terminal"); |
| 94 | target.style.display = "block"; |
| 95 | element.after(target); |
| 96 | } |
| 97 | const terminal = new Terminal({ |
| 98 | theme: { |
| 99 | background: "#191A19", |
| 100 | foreground: "#F5F2E7", |
| 101 | }, |
| 102 | ...terminalOptions, |
| 103 | }); |
| 104 | const fitAddon = new FitAddon(); |
| 105 | terminal.loadAddon(fitAddon); |
| 106 | terminal.loadAddon(new WebLinksAddon()); |
| 107 | terminal.open(target); |
| 108 | fitAddon.fit(); |
| 109 | terminal.focus(); |
| 110 | defineProperties(element, { |
| 111 | terminal: { value: terminal }, |
| 112 | process: { |
| 113 | value: async (code) => { |
| 114 | for (const line of code.split(/(?:\r\n|\r|\n)/)) { |
| 115 | await stream.write(`${line}\r`); |
| 116 | } |
| 117 | }, |
| 118 | }, |
| 119 | }); |
| 120 | return terminal; |
| 121 | }; |
| 122 | |
| 123 | // branch logic for the worker |
| 124 | if (element.hasAttribute("worker")) { |