@@ -377,16 +377,38 @@ void UpdateColorsIfNecessary(string newColor)
377
377
// We need to update the prompt
378
378
379
379
// promptBufferCells is the number of visible characters in the prompt
380
- var promptBufferCells = LengthInBufferCells ( promptText ) ;
380
+ int promptBufferCells = LengthInBufferCells ( promptText ) ;
381
+ bool renderErrorPrompt = false ;
381
382
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
383
if ( _console . CursorLeft >= promptBufferCells )
388
384
{
385
+ renderErrorPrompt = true ;
389
386
_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
+ {
390
412
var color = renderData . errorPrompt ? _options . _errorColor : defaultColor ;
391
413
if ( renderData . errorPrompt && promptBufferCells != promptText . Length )
392
414
promptText = promptText . Substring ( promptText . Length - promptBufferCells ) ;
0 commit comments