Skip to content

Commit 597ab9b

Browse files
committed
Fix one variant of the OutOfRange exception
1 parent 22ceb03 commit 597ab9b

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

PSReadLine/Render.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,22 @@ void UpdateColorsIfNecessary(string newColor)
378378

379379
// promptBufferCells is the number of visible characters in the prompt
380380
var promptBufferCells = LengthInBufferCells(promptText);
381-
_console.CursorLeft -= promptBufferCells;
382-
var color = renderData.errorPrompt ? _options._errorColor : defaultColor;
383-
if (renderData.errorPrompt && promptBufferCells != promptText.Length)
384-
promptText = promptText.Substring(promptText.Length - promptBufferCells);
385-
UpdateColorsIfNecessary(color);
386-
_console.Write(promptText);
387-
_console.Write("\x1b[0m");
381+
382+
// The 'CursorLeft' could be less than error-prompt-cell-length when
383+
// 1. console buffer was resized, which causes the initial cursor to appear on the next line;
384+
// 2. prompt string gets longer (e.g. by 'cd' into nested folders), which causes the line to be wrapped to the next line;
385+
// 3. the prompt function was changed, which causes the new prompt string is shorter than the error prompt.
386+
// when this happens, we skip changing the color of the prompt.
387+
if (_console.CursorLeft >= promptBufferCells)
388+
{
389+
_console.CursorLeft -= promptBufferCells;
390+
var color = renderData.errorPrompt ? _options._errorColor : defaultColor;
391+
if (renderData.errorPrompt && promptBufferCells != promptText.Length)
392+
promptText = promptText.Substring(promptText.Length - promptBufferCells);
393+
UpdateColorsIfNecessary(color);
394+
_console.Write(promptText);
395+
_console.Write("\x1b[0m");
396+
}
388397
}
389398
}
390399

0 commit comments

Comments
 (0)