Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append ESC + ENTER at end of commands sent to IPython shell #110

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"Sanderson",
"Sanderson's",
"setuptools",
"trippy",
"venv",
"virtualenvs",
"wglinfo",
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async function previewSelection() {
*/
async function clearScene() {
try {
await ManimShell.instance.executeCommandErrorOnNoActiveSession("clear()");
await ManimShell.instance.executeIPythonCommandExpectSession("clear()");
} catch (error) {
if (error instanceof NoActiveShellError) {
Window.showWarningMessage("No active Manim session found to remove objects from.");
Expand Down
31 changes: 17 additions & 14 deletions src/manimShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,42 +229,42 @@ export class ManimShell {
}

/**
* Executes the given command. If no active terminal running Manim is found,
* a new terminal is spawned, and a new Manim session is started in it
* before executing the given command.
* Executes the given IPython command. If no active terminal running Manim
* is found, a new terminal is spawned, and a new Manim session is started
* in it beforehand.
*
* This command is locked during startup to prevent multiple new scenes from
* being started at the same time, see `lockDuringStartup`.
*
* For params explanations, see the docs for `execCommand()`.
* For params explanations, see the docs for `execIPythonCommand()`.
*/
public async executeCommand(
public async executeIPythonCommand(
command: string, startLine: number, waitUntilFinished = false,
handler?: CommandExecutionEventHandler,
) {
await this.execCommand(
await this.execIPythonCommand(
command, waitUntilFinished, false, false, startLine, handler);
}

/**
* Executes the given command, but only if an active ManimGL shell exists.
* Otherwise throws a `NoActiveShellError`.
* Executes the given IPython command, but only if an active ManimGL shell
* exists. Otherwise throws a `NoActiveShellError`.
*
* For params explanations, see the docs for `execCommand()`.
* For params explanations, see the docs for `execIPythonCommand()`.
* @throws NoActiveShellError If no active shell is found.
*/
public async executeCommandErrorOnNoActiveSession(
public async executeIPythonCommandExpectSession(
command: string, waitUntilFinished = false, forceExecute = false,
) {
await this.execCommand(
await this.execIPythonCommand(
command, waitUntilFinished, forceExecute, true, undefined, undefined);
}

/**
* Returns whether the command execution is currently locked, i.e. when
* Manim is starting up or another command is currently running.
*
* @param forceExecute see `execCommand()`
* @param forceExecute see `execIPythonCommand()`
* @returns true if the command execution is locked, false otherwise.
*/
private async isLocked(forceExecute = false): Promise<boolean> {
Expand All @@ -291,7 +291,7 @@ export class ManimShell {
}

/**
* Executes a given command and bundles many different behaviors and options.
* Executes a given IPython command.
*
* This method is internal and only exposed via other public methods that
* select a specific behavior.
Expand All @@ -318,14 +318,17 @@ export class ManimShell {
* shell is required for the command execution (when `errorOnNoActiveShell`
* is set to true).
*/
private async execCommand(
private async execIPythonCommand(
command: string,
waitUntilFinished: boolean,
forceExecute: boolean,
errorOnNoActiveShell: boolean,
startLine?: number,
handler?: CommandExecutionEventHandler,
) {
// append ESC + ENTER to avoid IPython starting a multi-line input (#18)
command += "\x1b\x0d";

Logger.debug(`🚀 Exec command: ${command}, waitUntilFinished=${waitUntilFinished}`
+ `, forceExecute=${forceExecute}, errorOnNoActiveShell=${errorOnNoActiveShell}`);

Expand Down
4 changes: 2 additions & 2 deletions src/previewCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: n
if (ManimShell.instance.hasActiveShell()) {
const reloadCmd = `reload(${startLineParsed + 1})`;
await ManimShell.instance.nextTimeWaitForRestartedIPythonInstance();
await ManimShell.instance.executeCommandErrorOnNoActiveSession(reloadCmd, true);
await ManimShell.instance.executeIPythonCommandExpectSession(reloadCmd, true);
}
await previewManimCell(cellCodeParsed, startLineParsed);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ export async function previewCode(code: string, startLine: number): Promise<void
const clipboardBuffer = await vscode.env.clipboard.readText();
await vscode.env.clipboard.writeText(code);

await ManimShell.instance.executeCommand(
await ManimShell.instance.executeIPythonCommand(
PREVIEW_COMMAND, startLine, true, {
onCommandIssued: (shellStillExists) => {
Logger.debug(`📊 Command issued: ${PREVIEW_COMMAND}. Will restore clipboard`);
Expand Down
Loading