Skip to content

Commit

Permalink
Fix #269, Frozen 'Stop' button after unplugging
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Jan 17, 2025
1 parent 12f86ff commit c3c88cd
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/activator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class Activator {
// TODO: currently only used as file path - replace with proper type
// to support different target if needed
private outputRedirectionTarget?: string;
private commandExecuting = false;

constructor() {
this.logger = new Logger("Activator");
Expand Down Expand Up @@ -180,9 +181,9 @@ export default class Activator {
"\x1b[0m\r\n" // Reset text color to default
);
});
let commandExecuting = false;

this.terminal.onDidSubmit(async (cmd: string) => {
if (commandExecuting) {
if (this.commandExecuting) {
PicoMpyCom.getInstance().emit(
PicoSerialEvents.relayInput,
Buffer.from(cmd.trim(), "utf-8")
Expand All @@ -199,7 +200,7 @@ export default class Activator {

// TODO: maybe this.ui?.userOperationStarted();
// this will make waiting for prompt falsethis.terminal.freeze();
commandExecuting = true;
this.commandExecuting = true;
const result = await PicoMpyCom.getInstance().runFriendlyCommand(
cmd,
(open: boolean) => {
Expand Down Expand Up @@ -228,7 +229,7 @@ export default class Activator {
this.terminal?.clean(true);
}
this.ui?.userOperationStopped();
commandExecuting = false;
this.commandExecuting = false;
this.terminal?.prompt();
});
this.terminal.onDidRequestTabComp(async (buf: string) => {
Expand Down Expand Up @@ -492,7 +493,7 @@ export default class Activator {
return;
}

commandExecuting = true;
this.commandExecuting = true;
this.terminal?.cleanAndStore();
this.ui?.userOperationStarted();
},
Expand All @@ -510,7 +511,7 @@ export default class Activator {
if (data.type !== OperationResultType.commandResult || !data.result) {
this.logger.warn("Failed to execute script on Pico.");
}
commandExecuting = false;
this.commandExecuting = false;
this.terminal?.restore();
}
);
Expand Down Expand Up @@ -561,7 +562,7 @@ export default class Activator {

// tells the terminal that it should
// emit input events to relay user input
commandExecuting = true;
this.commandExecuting = true;
this.terminal?.cleanAndStore();
this.ui?.userOperationStarted();
},
Expand All @@ -576,7 +577,7 @@ export default class Activator {
await PicoMpyCom.getInstance().softReset();
}
this.ui?.userOperationStopped();
commandExecuting = false;
this.commandExecuting = false;
this.terminal?.restore();
}
);
Expand Down Expand Up @@ -616,7 +617,7 @@ export default class Activator {
return;
}

commandExecuting = true;
this.commandExecuting = true;
this.terminal?.cleanAndStore();
this.ui?.userOperationStarted();
},
Expand All @@ -629,7 +630,7 @@ export default class Activator {
this.pythonPath,
true
);
commandExecuting = false;
this.commandExecuting = false;
this.ui?.userOperationStopped();
if (data.type === OperationResultType.commandResult) {
// const result = data as PyOutCommandResult;
Expand Down Expand Up @@ -1361,7 +1362,7 @@ export default class Activator {
return;
}

commandExecuting = true;
this.commandExecuting = true;
this.terminal?.cleanAndStore();
this.ui?.userOperationStarted();

Expand All @@ -1374,7 +1375,7 @@ export default class Activator {
}
);
this.terminal?.restore();
commandExecuting = false;
this.commandExecuting = false;
this.ui?.userOperationStopped();
if (result.type === OperationResultType.commandResult) {
if (result.result) {
Expand Down Expand Up @@ -1402,7 +1403,7 @@ export default class Activator {
const result = await PicoMpyCom.getInstance().sendCtrlD(
(open: boolean) => {
if (open) {
commandExecuting = true;
this.commandExecuting = true;
//terminal?.freeze();
this.terminal?.clean(true);
//terminal?.write("\r\n");
Expand All @@ -1414,7 +1415,7 @@ export default class Activator {
this.terminal?.write(data.toString("utf-8"));
}
);
commandExecuting = false;
this.commandExecuting = false;
this.ui?.userOperationStopped();
if (result.type === OperationResultType.commandResult) {
if (result.result) {
Expand Down Expand Up @@ -1678,7 +1679,7 @@ export default class Activator {
: "Output redirection is disabled"
);
break;
case "$(arrow-right) File":
case "$(arrow-right) File": {
const file = await vscode.window.showSaveDialog({
filters: {
"Text files": ["txt"],
Expand All @@ -1692,6 +1693,7 @@ export default class Activator {
this.outputRedirectionTarget = file.fsPath;
}
break;
}
}
}
);
Expand Down Expand Up @@ -1947,6 +1949,17 @@ export default class Activator {
if (error === undefined) {
this.logger.info(`Connection to board was closed.`);
if (this.comDevice !== undefined) {
// check for running operation and cancel it
if (this.ui?.isUserOperationOngoing()) {
void vscode.window.showWarningMessage(
"Connection to board was closed. Stopping ongoing operation."
);
this.ui?.userOperationStopped();
this.commandExecuting = false;
// has no benefit as the terminal will be reloaded on reconnect anyway
//this.terminal?.restore();
}
// END
void vscode.window.showInformationMessage("Disconnected from board.");
this.terminal?.freeze();
this.terminal?.write(
Expand Down Expand Up @@ -2058,7 +2071,8 @@ export default class Activator {
bootPyResult.type === OperationResultType.getItemStat &&
bootPyResult.stat !== null
) {
// warn that boot.py could prevent device from entering REPL or delay the amount we have to wait before we can reconnect
// warn that boot.py could prevent device from entering REPL or
// delay the amount we have to wait before we can reconnect
const result = await vscode.window.showWarningMessage(
"A boot.py script is present on the Pico. " +
"If it contains an infinite loop or long running code, " +
Expand Down Expand Up @@ -2091,7 +2105,7 @@ export default class Activator {
} catch (error) {
this.logger.error(
`Failed to redirect output to file: ${
error instanceof Error ? error.message : error
error instanceof Error ? error.message : (error as string)
}`
);
}
Expand Down

0 comments on commit c3c88cd

Please sign in to comment.