Skip to content

Commit

Permalink
Fix preview command sometimes not executing on Windows (#81)
Browse files Browse the repository at this point in the history
* Fix preview command sometimes not executing on Windows

* Fix clear command not executing and change call to ManimShell.exec

* Revert adding flag to ManimShell.exec to not use shell integration

* Revert back to sendText for sending extra newline
  • Loading branch information
RickLuiken authored Nov 10, 2024
1 parent ec4d59d commit 879a43b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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`);
// 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();
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`;
// \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

0 comments on commit 879a43b

Please sign in to comment.