Skip to content

Commit dcf0669

Browse files
Add option to delay commands after terminal start (#78)
* Wait a bit when creating a new terminal * Make delay a user setting (defaults to 0) * Add docstrings * Add "Python" to markdown description * small spelling fix --------- Co-authored-by: Vladimir Fokow <[email protected]>
1 parent c44edcd commit dcf0669

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,14 @@
9191
"markdownDescription": "Configures the number of milliseconds (ms) to wait before your clipboard is restored. (Your clipboard is used to temporarily copy the selected code to be accessible by Manim)."
9292
},
9393
"manim-notebook.confirmKillingActiveSceneToStartNewOne": {
94-
"type": "boolean",
94+
"type": "boolean",
9595
"default": true,
9696
"markdownDescription": "If enabled, you will be prompted to confirm killing an old session when you want to start a new scene at the cursor while an active scene is running."
97+
},
98+
"manim-notebook.delayNewTerminal": {
99+
"type": "number",
100+
"default": 0,
101+
"markdownDescription": "Number of milliseconds (ms) to wait before executing any command in a newly opened terminal. This is useful when you have custom terminal startup commands that need to be executed before running Manim, e.g. a virtual environment activation (Python venv)."
97102
}
98103
}
99104
}

src/manimShell.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export class ManimShell {
354354
Logger.debug("🔆 User confirmed to kill active scene");
355355
await this.forceQuitActiveShell();
356356
}
357-
this.activeShell = window.createTerminal();
357+
await this.openNewTerminal();
358358
} else {
359359
Logger.debug("🔆 Executing start command that is requested for another command");
360360
}
@@ -512,7 +512,7 @@ export class ManimShell {
512512
private async retrieveOrInitActiveShell(startLine: number): Promise<Terminal> {
513513
if (!this.hasActiveShell()) {
514514
Logger.debug("🔍 No active shell found, requesting startScene");
515-
this.activeShell = window.createTerminal();
515+
await this.openNewTerminal();
516516
await startScene(startLine);
517517
Logger.debug("🔍 Started new scene to retrieve new shell");
518518
} else {
@@ -521,6 +521,44 @@ export class ManimShell {
521521
return this.activeShell as Terminal;
522522
}
523523

524+
/**
525+
* Opens a new terminal and sets it as the active shell. Afterwards, waits
526+
* for a user-defined delay before allowing the terminal to be used, which
527+
* might be useful for some activation scripts to load like virtualenvs etc.
528+
*/
529+
private async openNewTerminal() {
530+
const delay: number = await vscode.workspace
531+
.getConfiguration("manim-notebook").get("delayNewTerminal")!;
532+
533+
// We don't want to detect shell execution ends here, since commands like
534+
// `source venv/bin/activate` might on their own trigger a terminal
535+
// execution end.
536+
this.detectShellExecutionEnd = false;
537+
538+
this.activeShell = window.createTerminal();
539+
540+
if (delay > 600) {
541+
await window.withProgress({
542+
location: vscode.ProgressLocation.Notification,
543+
title: "Waiting a user-defined delay for the new terminal...",
544+
cancellable: false
545+
}, async (progress, token) => {
546+
progress.report({ increment: 0 });
547+
548+
// split user-defined timeout into 500ms chunks and show progress
549+
const numChunks = Math.ceil(delay / 500);
550+
for (let i = 0; i < numChunks; i++) {
551+
await new Promise(resolve => setTimeout(resolve, 500));
552+
progress.report({ increment: 100 / numChunks });
553+
}
554+
});
555+
} else {
556+
await new Promise(resolve => setTimeout(resolve, delay));
557+
}
558+
559+
this.detectShellExecutionEnd = true;
560+
}
561+
524562
/**
525563
* Waits until the current command has finished executing by waiting for
526564
* the start of the next IPython cell. This is used to ensure that the

0 commit comments

Comments
 (0)