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

Fix preview command sometimes not executing on Windows #81

Merged
merged 4 commits into from
Nov 10, 2024
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
22 changes: 20 additions & 2 deletions src/manimShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const ANSI_CONTROL_SEQUENCE_REGEX = /(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x
*/
const IPYTHON_CELL_START_REGEX = /^\s*In \[\d+\]:/gm;

/**
* Regular expression to match IPython multiline input "...:"
* Sometimes IPython does not execute code when entering a newline, but enters a
* multiline input mode, where it expects another line of code. We detect that
* this happens and send an extra newline to run the code
* See: https://github.com/Manim-Notebook/manim-notebook/issues/18
*/
const IPYTHON_MULTILINE_START_REGEX = /^\s*\.{3}:\s+$/m;

/**
* Regular expression to match a KeyboardInterrupt.
*/
Expand Down Expand Up @@ -483,12 +492,13 @@ export class ManimShell {
*
* @param shell The shell to execute the command in.
* @param command The command to execute in the shell.
* @param useShellIntegration Whether to use shell integration if available
*/
private exec(shell: Terminal, command: string) {
private exec(shell: Terminal, command: string, useShellIntegration = true) {
this.detectShellExecutionEnd = false;
Logger.debug("🔒 Shell execution end detection disabled");

if (shell.shellIntegration) {
if (useShellIntegration && shell.shellIntegration) {
Logger.debug(`💨 Sending command to terminal (with shell integration): ${command}`);
shell.shellIntegration.executeCommand(command);
} else {
Expand Down Expand Up @@ -657,6 +667,14 @@ export class ManimShell {
this.eventEmitter.emit(ManimShellEvent.IPYTHON_CELL_FINISHED);
}

if (this.isExecutingCommand && data.match(IPYTHON_MULTILINE_START_REGEX)) {
Logger.debug(`💨 IPython multiline detected, sending extra newline`);
RickLuiken marked this conversation as resolved.
Show resolved Hide resolved
// do not use shell integration here, as it might send a CTRL-C
// while the prompt is not finished yet
// \x7F deletes the extra line ("...:") from IPython
Splines marked this conversation as resolved.
Show resolved Hide resolved
this.exec(this.activeShell, "\x7F", false);
}

if (data.match(ERROR_REGEX)) {
Logger.debug("🚨 Error in IPython cell detected");
this.activeShell?.show();
Expand Down
5 changes: 2 additions & 3 deletions src/previewCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { ManimShell } from './manimShell';
import { EventEmitter } from 'events';
import { Logger } from './logger';

const PREVIEW_COMMAND = `\x0C checkpoint_paste()\x1b`;
VladimirFokow marked this conversation as resolved.
Show resolved Hide resolved
// \x0C: is Ctrl + L
// \x1b: https://github.com/bhoov/manim-notebook/issues/18#issuecomment-2431146809
// \x0C: is Ctrl + L, which clears the terminal screen
const PREVIEW_COMMAND = `\x0Ccheckpoint_paste()`;

/**
* Interactively previews the given Manim code by means of the
Expand Down