| 63 | // common main thread initialization for both worker |
| 64 | // or main case, bootstrapping the terminal on its target |
| 65 | const init = (options) => { |
| 66 | let target = element; |
| 67 | const selector = target.getAttribute("target"); |
| 68 | if (selector) { |
| 69 | target = |
| 70 | document.getElementById(selector) || |
| 71 | document.querySelector(selector); |
| 72 | if (!target) throw new Error(`Unknown target ${selector}`); |
| 73 | } else { |
| 74 | target = document.createElement("py-terminal"); |
| 75 | target.style.display = "block"; |
| 76 | element.after(target); |
| 77 | } |
| 78 | const terminal = new Terminal({ |
| 79 | theme: { |
| 80 | background: "#191A19", |
| 81 | foreground: "#F5F2E7", |
| 82 | }, |
| 83 | ...options, |
| 84 | }); |
| 85 | const fitAddon = new FitAddon(); |
| 86 | terminal.loadAddon(fitAddon); |
| 87 | terminal.loadAddon(readline); |
| 88 | terminal.loadAddon(new WebLinksAddon()); |
| 89 | terminal.open(target); |
| 90 | fitAddon.fit(); |
| 91 | terminal.focus(); |
| 92 | defineProperties(element, { |
| 93 | terminal: { value: terminal }, |
| 94 | process: { |
| 95 | value: async (code) => { |
| 96 | for (const line of code.split(/(?:\r\n|\r|\n)/)) { |
| 97 | terminal.paste(`${line}`); |
| 98 | terminal.write("\r\n"); |
| 99 | do { |
| 100 | await new Promise((resolve) => |
| 101 | setTimeout(resolve, 0), |
| 102 | ); |
| 103 | } while (!readline.activeRead?.resolve); |
| 104 | readline.activeRead.resolve(line); |
| 105 | } |
| 106 | }, |
| 107 | }, |
| 108 | }); |
| 109 | return terminal; |
| 110 | }; |
| 111 | |
| 112 | // branch logic for the worker |
| 113 | if (element.hasAttribute("worker")) { |