8000 PyTerminal .process(code) utility (#2026) · pyscript/pyscript@44cd627 · GitHub
[go: up one dir, main page]

Skip to content

Commit 44cd627

Browse files
PyTerminal .process(code) utility (#2026)
* PyTerminal .process(code) utility * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d7d2dfb commit 44cd627

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

pyscript.core/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyscript.core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.4.18",
3+
"version": "0.4.20",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",

pyscript.core/src/plugins/py-terminal.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// PyScript py-terminal plugin
22
import { TYPES, hooks } from "../core.js";
33
import { notify } from "./error.js";
4-
import { customObserver, defineProperty } from "polyscript/exports";
4+
import { customObserver, defineProperties } from "polyscript/exports";
55

66
// will contain all valid selectors
77
const SELECTORS = [];
@@ -170,7 +170,25 @@ const pyTerminal = async (element) => {
170170
terminal.open(target);
171171
fitAddon.fit();
172172
terminal.focus();
173-
defineProperty(element, "terminal", { value: terminal });
173+
defineProperties(element, {
174+
terminal: { value: terminal },
175+
process: {
176+
value: async (code) => {
177+
// this loop is the only way I could find to actually simulate
178+
// the user input char after char in a way that works in both
179+
// MicroPython and Pyodide
180+
for (const line of code.split(/(?:\r|\n|\r\n)/)) {
181+
terminal.paste(`${line}\n`);
182+
do {
183+
await new Promise((resolve) =>
184+
setTimeout(resolve, 0),
185+
);
186+
} while (!readline.activeRead?.resolve);
187+
readline.activeRead.resolve(line);
188+
}
189+
},
190+
},
191+
});
174192
return terminal;
175193
};
176194

0 commit comments

Comments
 (0)
0