Skip to content

Commit 0707309

Browse files
authored
Merge pull request #7 from makermelissa/main
Fix issues by adding better code interruption
2 parents 5c5c33c + b64c5f1 commit 0707309

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

repl.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const MODE_RAWPASTE = 3;
1717

1818
const TYPE_DIR = 16384;
1919
const TYPE_FILE = 32768;
20+
const DEBUG = false;
2021

2122
export const LINE_ENDING_CRLF = "\r\n";
2223
export const LINE_ENDING_LF = "\n";
@@ -25,15 +26,11 @@ const CONTROL_SEQUENCES = [
2526
REGEX_RAW_PASTE_RESPONSE
2627
];
2728

28-
// TODO: The title occasionally has duplicate characters (such as 2 snakes). Likely the buffer pointers need adjusting.
29-
// TODO: Get Raw Mode working
30-
// TODO: Add parsing for non-english languages (alternatively, only look at symbols that are common across the languages)
31-
3229
// Mostly needed when the terminal echos back the input
3330
const IGNORE_OUTPUT_LINE_PREFIXES = [/^\... /, /^>>> /];
3431

3532
// Default timeouts in milliseconds (can be overridden with properties)
36-
const PROMPT_TIMEOUT = 10000;
33+
const PROMPT_TIMEOUT = 20000;
3734
const CODE_EXECUTION_TIMEOUT = 15000;
3835
const PROMPT_CHECK_INTERVAL = 50;
3936

@@ -119,7 +116,7 @@ with open("${path}", "w") as f:
119116

120117
// Write a file to the device path with contents beginning at offset. Modification time can be set and if raw is true, contents is written as binary
121118
async writeFile(path, contents, offset=0, modificationTime=null, raw=false) {
122-
this._repl.terminalOutput = false;
119+
this._repl.terminalOutput = DEBUG;
123120

124121
if (raw) {
125122
await this._writeRawFile(path, contents, offset, modificationTime);
@@ -183,7 +180,7 @@ with open("${path}", "r") as f:
183180
// Read a file from the device
184181
async readFile(path, raw=false) {
185182
let result;
186-
this._repl.terminalOutput = false;
183+
this._repl.terminalOutput = DEBUG;
187184

188185
if (raw) {
189186
result = await this._readRawFile(path);
@@ -198,7 +195,7 @@ with open("${path}", "r") as f:
198195
// List files using paste mode on the device returning the result as a javascript array
199196
// We need the file name, whether or not it is a directory, file size and file date
200197
async listDir(path) {
201-
this._repl.terminalOutput = false;
198+
this._repl.terminalOutput = DEBUG;
202199
// Mask sure path has a trailing slash
203200
if (path[path.length - 1] != "/") {
204201
path += "/";
@@ -231,7 +228,7 @@ for item in contents:
231228
}
232229

233230
async isReadOnly() {
234-
this._repl.terminalOutput = false;
231+
this._repl.terminalOutput = DEBUG;
235232

236233
let code = `
237234
import storage
@@ -246,7 +243,7 @@ print(storage.getmount("/").readonly)
246243

247244
async makeDir(path, modificationTime=null) {
248245
await this._checkReadOnly();
249-
this._repl.terminalOutput = false;
246+
this._repl.terminalOutput = DEBUG;
250247
let code = `os.mkdir("${path}")\n`;
251248
if (modificationTime) {
252249
code += `os.utime("${path}", (os.path.getatime("${path}"), ${modificationTime}))\n`;
@@ -258,7 +255,7 @@ print(storage.getmount("/").readonly)
258255

259256
async delete(path) {
260257
await this._checkReadOnly();
261-
this._repl.terminalOutput = false;
258+
this._repl.terminalOutput = DEBUG;
262259
let code = `
263260
import os
264261
@@ -278,7 +275,7 @@ else:
278275
// we need to check if the new path already exists
279276
// Return true on success and false on failure
280277

281-
this._repl.terminalOutput = false;
278+
this._repl.terminalOutput = DEBUG;
282279
let code = `
283280
import os
284281
os.rename("${oldPath}", "${newPath}")
@@ -453,7 +450,23 @@ export class REPL {
453450
}
454451

455452
async interruptCode() {
456-
await this.serialTransmit(CHAR_CTRL_C);
453+
this._pythonCodeRunning = true;
454+
// Wait for a prompt
455+
try {
456+
await this._timeout(
457+
async () => {
458+
while (this._pythonCodeRunning) {
459+
await this.serialTransmit(CHAR_CTRL_C);
460+
await this.checkPrompt();
461+
await this._sleep(50);
462+
}
463+
}, this.promptTimeout
464+
);
465+
} catch (error) {
466+
console.error("Awaiting prompt timed out.");
467+
return false;
468+
}
469+
457470
}
458471

459472
getErrorOutput(raw = false) {
@@ -493,10 +506,10 @@ export class REPL {
493506
async getToPrompt() {
494507
// We use GetToPrompt to ensure we are at a known place before running code
495508
// This will get from Paste Mode or Running App to Normal Prompt
496-
await this.serialTransmit(CHAR_CTRL_C + CHAR_CTRL_C);
509+
await this.interruptCode();
497510
// This will get from Raw Paste or Raw Mode to Normal Prompt
498511
await this.serialTransmit(CHAR_CTRL_B + CHAR_CTRL_D + CHAR_CTRL_B);
499-
this.mode = MODE_NORMAL;
512+
this._mode = MODE_NORMAL;
500513
}
501514

502515
async execRawMode(code) {

0 commit comments

Comments
 (0)