From 8fecb010640202773b231e0208e86151830d3117 Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Wed, 6 Mar 2024 00:12:58 -0500 Subject: [PATCH] fix: cmd prompt controls when under test (#184) * fix: cmd prompt controls when under test Signed-off-by: Chapman Pendery * fix: null access on clear command Signed-off-by: Chapman Pendery --------- Signed-off-by: Chapman Pendery --- src/isterm/commandManager.ts | 16 ++++++++-------- src/isterm/pty.ts | 3 +++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/isterm/commandManager.ts b/src/isterm/commandManager.ts index b48d8f5..389ad8d 100644 --- a/src/isterm/commandManager.ts +++ b/src/isterm/commandManager.ts @@ -125,7 +125,7 @@ export class CommandManager { } if (this.#shell == Shell.Cmd) { - return lineText.match(/^(?(\(.+\)\s)?(?:[A-Z]:\\.*>))/)?.groups?.prompt; + return lineText.match(/^(?(\(.+\)\s)?(?:[A-Z]:\\.*>)|(> ))/)?.groups?.prompt; } // Custom prompts like starship end in the common \u276f character @@ -177,19 +177,18 @@ export class CommandManager { if (this.#activeCommand.promptEndMarker == null || this.#activeCommand.promptStartMarker == null) { return; } - const promptEndMarker = this.#activeCommand.promptEndMarker; - const promptStartMarker = this.#activeCommand.promptStartMarker; const globalCursorPosition = this.#terminal.buffer.active.baseY + this.#terminal.buffer.active.cursorY; const withinPollDistance = globalCursorPosition < this.#activeCommand.promptEndMarker.line + 5; - if (globalCursorPosition < promptStartMarker.line) { + if (globalCursorPosition < this.#activeCommand.promptStartMarker.line) { this.handleClear(); + this.#activeCommand.promptEndMarker = this.#terminal.registerMarker(0); } // if we haven't fond the prompt yet, poll over the next 5 lines searching for it if (this.#activeCommand.promptText == null && withinPollDistance) { - for (let i = globalCursorPosition; i < promptEndMarker.line + maxPromptPollDistance; i++) { + for (let i = globalCursorPosition; i < this.#activeCommand.promptEndMarker!.line + maxPromptPollDistance; i++) { if (this.#previousCommandLines.has(i)) continue; const promptResult = this._getWindowsPrompt(i); if (promptResult != null) { @@ -197,18 +196,19 @@ export class CommandManager { this.#activeCommand.promptEndX = promptResult.length; this.#activeCommand.promptText = promptResult; this.#previousCommandLines.add(i); + break; } } } // if the prompt is set, now parse out the values from the terminal if (this.#activeCommand.promptText != null) { - let lineY = promptEndMarker.line; - let line = this.#terminal.buffer.active.getLine(promptEndMarker.line); + let lineY = this.#activeCommand.promptEndMarker!.line; + let line = this.#terminal.buffer.active.getLine(this.#activeCommand.promptEndMarker!.line); let command = ""; let suggestions = ""; for (;;) { - for (let i = lineY == promptEndMarker.line ? this.#activeCommand.promptText.length : 0; i < this.#terminal.cols; i++) { + for (let i = lineY == this.#activeCommand.promptEndMarker!.line ? this.#activeCommand.promptText.length : 0; i < this.#terminal.cols; i++) { const cell = line?.getCell(i); if (cell == null) continue; const chars = cell.getChars(); diff --git a/src/isterm/pty.ts b/src/isterm/pty.ts index 0ec62e8..ef54c2e 100644 --- a/src/isterm/pty.ts +++ b/src/isterm/pty.ts @@ -274,6 +274,9 @@ const convertToPtyEnv = (shell: Shell, underTest: boolean) => { }; switch (shell) { case Shell.Cmd: { + if (underTest) { + return { ...env, PROMPT: `${IstermPromptStart}$G ${IstermPromptEnd}` }; + } const prompt = process.env.PROMPT ? process.env.PROMPT : "$P$G"; return { ...env, PROMPT: `${IstermPromptStart}${prompt}${IstermPromptEnd}` }; }