Skip to content

Commit 879a43b

Browse files
authored
Fix preview command sometimes not executing on Windows (#81)
* 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
1 parent ec4d59d commit 879a43b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/manimShell.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ const ANSI_CONTROL_SEQUENCE_REGEX = /(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x
1717
*/
1818
const IPYTHON_CELL_START_REGEX = /^\s*In \[\d+\]:/gm;
1919

20+
/**
21+
* Regular expression to match IPython multiline input "...:"
22+
* Sometimes IPython does not execute code when entering a newline, but enters a
23+
* multiline input mode, where it expects another line of code. We detect that
24+
* this happens and send an extra newline to run the code
25+
* See: https://github.com/Manim-Notebook/manim-notebook/issues/18
26+
*/
27+
const IPYTHON_MULTILINE_START_REGEX = /^\s*\.{3}:\s+$/m;
28+
2029
/**
2130
* Regular expression to match a KeyboardInterrupt.
2231
*/
@@ -483,12 +492,13 @@ export class ManimShell {
483492
*
484493
* @param shell The shell to execute the command in.
485494
* @param command The command to execute in the shell.
495+
* @param useShellIntegration Whether to use shell integration if available
486496
*/
487-
private exec(shell: Terminal, command: string) {
497+
private exec(shell: Terminal, command: string, useShellIntegration = true) {
488498
this.detectShellExecutionEnd = false;
489499
Logger.debug("🔒 Shell execution end detection disabled");
490500

491-
if (shell.shellIntegration) {
501+
if (useShellIntegration && shell.shellIntegration) {
492502
Logger.debug(`💨 Sending command to terminal (with shell integration): ${command}`);
493503
shell.shellIntegration.executeCommand(command);
494504
} else {
@@ -657,6 +667,14 @@ export class ManimShell {
657667
this.eventEmitter.emit(ManimShellEvent.IPYTHON_CELL_FINISHED);
658668
}
659669

670+
if (this.isExecutingCommand && data.match(IPYTHON_MULTILINE_START_REGEX)) {
671+
Logger.debug(`💨 IPython multiline detected, sending extra newline`);
672+
// do not use shell integration here, as it might send a CTRL-C
673+
// while the prompt is not finished yet
674+
// \x7F deletes the extra line ("...:") from IPython
675+
this.exec(this.activeShell, "\x7F", false);
676+
}
677+
660678
if (data.match(ERROR_REGEX)) {
661679
Logger.debug("🚨 Error in IPython cell detected");
662680
this.activeShell?.show();

src/previewCode.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { ManimShell } from './manimShell';
44
import { EventEmitter } from 'events';
55
import { Logger } from './logger';
66

7-
const PREVIEW_COMMAND = `\x0C checkpoint_paste()\x1b`;
8-
// \x0C: is Ctrl + L
9-
// \x1b: https://github.com/bhoov/manim-notebook/issues/18#issuecomment-2431146809
7+
// \x0C: is Ctrl + L, which clears the terminal screen
8+
const PREVIEW_COMMAND = `\x0Ccheckpoint_paste()`;
109

1110
/**
1211
* Interactively previews the given Manim code by means of the

0 commit comments

Comments
 (0)