Skip to content

Commit bae1556

Browse files
committed
Wrap to the previous line to update the error prompt
1 parent 597ab9b commit bae1556

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

PSReadLine/Render.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,38 @@ void UpdateColorsIfNecessary(string newColor)
377377
// We need to update the prompt
378378

379379
// promptBufferCells is the number of visible characters in the prompt
380-
var promptBufferCells = LengthInBufferCells(promptText);
380+
int promptBufferCells = LengthInBufferCells(promptText);
381+
bool renderErrorPrompt = false;
381382

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.
387383
if (_console.CursorLeft >= promptBufferCells)
388384
{
385+
renderErrorPrompt = true;
389386
_console.CursorLeft -= promptBufferCells;
387+
}
388+
else
389+
{
390+
// The 'CursorLeft' could be less than error-prompt-cell-length in one of the following 3 cases:
391+
// 1. console buffer was resized, which causes the initial cursor to appear on the next line;
392+
// 2. prompt string gets longer (e.g. by 'cd' into nested folders), which causes the line to be wrapped to the next line;
393+
// 3. the prompt function was changed, which causes the new prompt string is shorter than the error prompt.
394+
// Here, we always assume it's the case 1 or 2, and wrap back to the previous line to change the error prompt color.
395+
// In case of case 3, the rendering would be off, but it's more of a user error because the prompt is changed without
396+
// updating 'PromptText' with 'Set-PSReadLineOption'.
397+
398+
int diffs = promptBufferCells - _console.CursorLeft;
399+
int newX = bufferWidth - diffs % bufferWidth;
400+
int newY = _initialY - diffs / bufferWidth - 1;
401+
402+
// newY could be less than 0 if 'PromptText' is manually set to be a long string.
403+
if (newY >= 0)
404+
{
405+
renderErrorPrompt = true;
406+
_console.SetCursorPosition(newX, newY);
407+
}
408+
}
409+
410+
if (renderErrorPrompt)
411+
{
390412
var color = renderData.errorPrompt ? _options._errorColor : defaultColor;
391413
if (renderData.errorPrompt && promptBufferCells != promptText.Length)
392414
promptText = promptText.Substring(promptText.Length - promptBufferCells);

0 commit comments

Comments
 (0)