Skip to content

Commit

Permalink
Added redirectOutput experimental command
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Nov 17, 2024
1 parent 6ff83aa commit 5043cd0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@
"command": "micropico.flashPico",
"title": "Flash Pico in BOOTSEL mode",
"category": "MicroPico"
},
{
"command": "micropico.redirectOutput",
"title": "MicroPython output redirection settings (experimental)",
"category": "MicroPico",
"enablement": "micropico.isConnected"
}
],
"menus": {
Expand Down Expand Up @@ -323,6 +329,13 @@
"when": "view == micropico-device-wifi",
"group": "navigation@0"
}
],
"terminal/context": [
{
"command": "micropico.redirectOutput",
"group": "MicroPico",
"when": "micropico.isConnected"
}
]
},
"configuration": {
Expand Down
71 changes: 71 additions & 0 deletions src/activator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
PythonExtension,
} from "@vscode/python-extension";
import { flashPicoInteractively } from "./flash.mjs";
import { appendFileSync } from "fs";

/*const pkg: {} | undefined = vscode.extensions.getExtension("paulober.pico-w-go")
?.packageJSON as object;*/
Expand All @@ -58,6 +59,9 @@ export default class Activator {
private autoConnectTimer?: NodeJS.Timeout;
private comDevice?: string;
private noCheckForUSBMSDs = false;
// TODO: currently only used as file path - replace with proper type
// to support different target if needed
private outputRedirectionTarget?: string;

constructor() {
this.logger = new Logger("Activator");
Expand Down Expand Up @@ -207,6 +211,7 @@ export default class Activator {
},
(data: Buffer) => {
if (data.length > 0) {
this.redirectOutput(data);
this.terminal?.write(data.toString("utf-8"));
}
},
Expand Down Expand Up @@ -493,6 +498,7 @@ export default class Activator {
},
(data: Buffer) => {
if (data.length > 0) {
this.redirectOutput(data);
this.terminal?.write(data.toString("utf-8"));
}
}
Expand Down Expand Up @@ -561,6 +567,7 @@ export default class Activator {
},
(data: Buffer) => {
if (data.length > 0) {
this.redirectOutput(data);
this.terminal?.write(data.toString("utf-8"));
}
}
Expand Down Expand Up @@ -615,6 +622,7 @@ export default class Activator {
},
(data: Buffer) => {
if (data.length > 0) {
this.redirectOutput(data);
this.terminal?.write(data.toString("utf-8"));
}
},
Expand Down Expand Up @@ -1361,6 +1369,7 @@ export default class Activator {
this.terminal?.write("\x1b[33mPerforming hard reset...\x1b[0m\r\n");
},
(data: Buffer) => {
this.redirectOutput(data);
this.terminal?.write(data.toString("utf-8"));
}
);
Expand Down Expand Up @@ -1401,6 +1410,7 @@ export default class Activator {
}
},
(data: Buffer) => {
this.redirectOutput(data);
this.terminal?.write(data.toString("utf-8"));
}
);
Expand Down Expand Up @@ -1642,6 +1652,50 @@ export default class Activator {
);
context.subscriptions.push(disposable);

// TODO: add context key to show command in context menu only if vREPL is focused
disposable = vscode.commands.registerCommand(
commandPrefix + "redirectOutput",
async () => {
const location = await vscode.window.showQuickPick(
["$(x) Disable", "$(info) Status", "$(arrow-right) File"],
{
canPickMany: false,
placeHolder: "Select the output location or manage settings",
title: "Output redirection for this session",
ignoreFocusOut: false,
}
);

switch (location) {
case "$(x) Disable":
this.outputRedirectionTarget = undefined;
break;
case "$(info) Status":
// show status if disabled to redirected into a file with path
void vscode.window.showInformationMessage(
this.outputRedirectionTarget
? `Output is redirected to: ${this.outputRedirectionTarget}`
: "Output redirection is disabled"
);
break;
case "$(arrow-right) File":
const file = await vscode.window.showSaveDialog({
filters: {
"Text files": ["txt"],
"Log files": ["log"],
"All files": ["*"],
},
saveLabel: "Save output to file",
});

if (file) {
this.outputRedirectionTarget = file.fsPath;
}
break;
}
}
);

const packagesWebviewProvider = new PackagesWebviewProvider(
context.extensionUri
);
Expand Down Expand Up @@ -2025,4 +2079,21 @@ export default class Activator {
// or the user wants to continue even if there is one
return false;
}

// TODO: maybe use a stream instead of spaming syscalls
private redirectOutput(data: Buffer): void {
if (this.outputRedirectionTarget === undefined) {
return;
}

try {
appendFileSync(this.outputRedirectionTarget, data);
} catch (error) {
this.logger.error(
`Failed to redirect output to file: ${
error instanceof Error ? error.message : error
}`
);
}
}
}

0 comments on commit 5043cd0

Please sign in to comment.