Skip to content

Commit 17ab58d

Browse files
committed
Fix broken utf-8 multi-byte decoding on code execution
Signed-off-by: paulober <[email protected]>
1 parent 471e877 commit 17ab58d

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/activator.mts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
import { flashPicoInteractively } from "./flash.mjs";
4040
import { appendFileSync, existsSync } from "fs";
4141
import { unknownErrorToString } from "./errorHelper.mjs";
42+
import { StringDecoder } from "string_decoder";
4243

4344
/*const pkg: {} | undefined = vscode.extensions.getExtension("paulober.pico-w-go")
4445
?.packageJSON as object;*/
@@ -201,6 +202,7 @@ export default class Activator {
201202
return;
202203
}
203204

205+
const decoder = new StringDecoder("utf-8");
204206
// TODO: maybe this.ui?.userOperationStarted();
205207
// this will make waiting for prompt falsethis.terminal.freeze();
206208
this.commandExecuting = true;
@@ -216,7 +218,10 @@ export default class Activator {
216218
(data: Buffer) => {
217219
if (data.length > 0) {
218220
this.redirectOutput(data);
219-
this.terminal?.write(data.toString("utf-8"));
221+
const text = decoder.write(data); // streaming decode
222+
if (text.length > 0) {
223+
this.terminal?.write(text);
224+
}
220225
}
221226
},
222227
this.pythonPath,
@@ -550,6 +555,7 @@ export default class Activator {
550555
if (!noSoftReset && !forceDisableSoftReset) {
551556
await PicoMpyCom.getInstance().softReset();
552557
}
558+
const decoder = new StringDecoder("utf-8");
553559
// TODO: maybe freeze terminal until this operation runs to prevent user input
554560
const data = await PicoMpyCom.getInstance().runFile(
555561
file,
@@ -566,7 +572,10 @@ export default class Activator {
566572
(data: Buffer) => {
567573
if (data.length > 0) {
568574
this.redirectOutput(data);
569-
this.terminal?.write(data.toString("utf-8"));
575+
const text = decoder.write(data); // streaming decode
576+
if (text.length > 0) {
577+
this.terminal?.write(text);
578+
}
570579
}
571580
}
572581
);
@@ -624,6 +633,7 @@ export default class Activator {
624633
await PicoMpyCom.getInstance().softReset();
625634
}
626635
await focusTerminal(this.terminalOptions);
636+
const decoder = new StringDecoder("utf-8");
627637
await PicoMpyCom.getInstance().runRemoteFile(
628638
file,
629639
(open: boolean) => {
@@ -640,7 +650,10 @@ export default class Activator {
640650
(data: Buffer) => {
641651
if (data.length > 0) {
642652
this.redirectOutput(data);
643-
this.terminal?.write(data.toString("utf-8"));
653+
const text = decoder.write(data); // streaming decode
654+
if (text.length > 0) {
655+
this.terminal?.write(text);
656+
}
644657
}
645658
}
646659
);
@@ -680,6 +693,7 @@ export default class Activator {
680693
return;
681694
} else {
682695
await focusTerminal(this.terminalOptions);
696+
const decoder = new StringDecoder("utf-8");
683697
const data = await PicoMpyCom.getInstance().runFriendlyCommand(
684698
code,
685699
(open: boolean) => {
@@ -695,7 +709,10 @@ export default class Activator {
695709
(data: Buffer) => {
696710
if (data.length > 0) {
697711
this.redirectOutput(data);
698-
this.terminal?.write(data.toString("utf-8"));
712+
const text = decoder.write(data); // streaming decode
713+
if (text.length > 0) {
714+
this.terminal?.write(text);
715+
}
699716
}
700717
},
701718
this.pythonPath,
@@ -1420,6 +1437,7 @@ export default class Activator {
14201437
}
14211438

14221439
await focusTerminal(this.terminalOptions);
1440+
const decoder = new StringDecoder("utf-8");
14231441
const result = await PicoMpyCom.getInstance().hardReset(
14241442
(open: boolean) => {
14251443
if (!open) {
@@ -1435,7 +1453,10 @@ export default class Activator {
14351453
},
14361454
(data: Buffer) => {
14371455
this.redirectOutput(data);
1438-
this.terminal?.write(data.toString("utf-8"));
1456+
const text = decoder.write(data); // streaming decode
1457+
if (text.length > 0) {
1458+
this.terminal?.write(text);
1459+
}
14391460
}
14401461
);
14411462
this.terminal?.restore();
@@ -1464,6 +1485,7 @@ export default class Activator {
14641485
}
14651486

14661487
await focusTerminal(this.terminalOptions);
1488+
const decoder = new StringDecoder("utf-8");
14671489
const result = await PicoMpyCom.getInstance().sendCtrlD(
14681490
(open: boolean) => {
14691491
if (open) {
@@ -1476,7 +1498,10 @@ export default class Activator {
14761498
},
14771499
(data: Buffer) => {
14781500
this.redirectOutput(data);
1479-
this.terminal?.write(data.toString("utf-8"));
1501+
const text = decoder.write(data); // streaming decode
1502+
if (text.length > 0) {
1503+
this.terminal?.write(text);
1504+
}
14801505
}
14811506
);
14821507
this.commandExecuting = false;

0 commit comments

Comments
 (0)