@@ -17,6 +17,7 @@ const MODE_RAWPASTE = 3;
17
17
18
18
const TYPE_DIR = 16384 ;
19
19
const TYPE_FILE = 32768 ;
20
+ const DEBUG = false ;
20
21
21
22
export const LINE_ENDING_CRLF = "\r\n" ;
22
23
export const LINE_ENDING_LF = "\n" ;
@@ -25,15 +26,11 @@ const CONTROL_SEQUENCES = [
25
26
REGEX_RAW_PASTE_RESPONSE
26
27
] ;
27
28
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
-
32
29
// Mostly needed when the terminal echos back the input
33
30
const IGNORE_OUTPUT_LINE_PREFIXES = [ / ^ \. .. / , / ^ > > > / ] ;
34
31
35
32
// Default timeouts in milliseconds (can be overridden with properties)
36
- const PROMPT_TIMEOUT = 10000 ;
33
+ const PROMPT_TIMEOUT = 20000 ;
37
34
const CODE_EXECUTION_TIMEOUT = 15000 ;
38
35
const PROMPT_CHECK_INTERVAL = 50 ;
39
36
@@ -119,7 +116,7 @@ with open("${path}", "w") as f:
119
116
120
117
// 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
121
118
async writeFile ( path , contents , offset = 0 , modificationTime = null , raw = false ) {
122
- this . _repl . terminalOutput = false ;
119
+ this . _repl . terminalOutput = DEBUG ;
123
120
124
121
if ( raw ) {
125
122
await this . _writeRawFile ( path , contents , offset , modificationTime ) ;
@@ -183,7 +180,7 @@ with open("${path}", "r") as f:
183
180
// Read a file from the device
184
181
async readFile ( path , raw = false ) {
185
182
let result ;
186
- this . _repl . terminalOutput = false ;
183
+ this . _repl . terminalOutput = DEBUG ;
187
184
188
185
if ( raw ) {
189
186
result = await this . _readRawFile ( path ) ;
@@ -198,7 +195,7 @@ with open("${path}", "r") as f:
198
195
// List files using paste mode on the device returning the result as a javascript array
199
196
// We need the file name, whether or not it is a directory, file size and file date
200
197
async listDir ( path ) {
201
- this . _repl . terminalOutput = false ;
198
+ this . _repl . terminalOutput = DEBUG ;
202
199
// Mask sure path has a trailing slash
203
200
if ( path [ path . length - 1 ] != "/" ) {
204
201
path += "/" ;
@@ -231,7 +228,7 @@ for item in contents:
231
228
}
232
229
233
230
async isReadOnly ( ) {
234
- this . _repl . terminalOutput = false ;
231
+ this . _repl . terminalOutput = DEBUG ;
235
232
236
233
let code = `
237
234
import storage
@@ -246,7 +243,7 @@ print(storage.getmount("/").readonly)
246
243
247
244
async makeDir ( path , modificationTime = null ) {
248
245
await this . _checkReadOnly ( ) ;
249
- this . _repl . terminalOutput = false ;
246
+ this . _repl . terminalOutput = DEBUG ;
250
247
let code = `os.mkdir("${ path } ")\n` ;
251
248
if ( modificationTime ) {
252
249
code += `os.utime("${ path } ", (os.path.getatime("${ path } "), ${ modificationTime } ))\n` ;
@@ -258,7 +255,7 @@ print(storage.getmount("/").readonly)
258
255
259
256
async delete ( path ) {
260
257
await this . _checkReadOnly ( ) ;
261
- this . _repl . terminalOutput = false ;
258
+ this . _repl . terminalOutput = DEBUG ;
262
259
let code = `
263
260
import os
264
261
@@ -278,7 +275,7 @@ else:
278
275
// we need to check if the new path already exists
279
276
// Return true on success and false on failure
280
277
281
- this . _repl . terminalOutput = false ;
278
+ this . _repl . terminalOutput = DEBUG ;
282
279
let code = `
283
280
import os
284
281
os.rename("${ oldPath } ", "${ newPath } ")
@@ -453,7 +450,23 @@ export class REPL {
453
450
}
454
451
455
452
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
+
457
470
}
458
471
459
472
getErrorOutput ( raw = false ) {
@@ -493,10 +506,10 @@ export class REPL {
493
506
async getToPrompt ( ) {
494
507
// We use GetToPrompt to ensure we are at a known place before running code
495
508
// 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 ( ) ;
497
510
// This will get from Raw Paste or Raw Mode to Normal Prompt
498
511
await this . serialTransmit ( CHAR_CTRL_B + CHAR_CTRL_D + CHAR_CTRL_B ) ;
499
- this . mode = MODE_NORMAL ;
512
+ this . _mode = MODE_NORMAL ;
500
513
}
501
514
502
515
async execRawMode ( code ) {
0 commit comments