diff --git a/src/manimShell.ts b/src/manimShell.ts index 2ac8041..7937281 100644 --- a/src/manimShell.ts +++ b/src/manimShell.ts @@ -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. */ @@ -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 { @@ -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`); + // 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 + this.exec(this.activeShell, "\x7F", false); + } + if (data.match(ERROR_REGEX)) { Logger.debug("🚨 Error in IPython cell detected"); this.activeShell?.show(); diff --git a/src/previewCode.ts b/src/previewCode.ts index d583353..0977ed3 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -4,9 +4,8 @@ import { ManimShell } from './manimShell'; import { EventEmitter } from 'events'; import { Logger } from './logger'; -const PREVIEW_COMMAND = `\x0C checkpoint_paste()\x1b`; -// \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