@@ -378,13 +378,22 @@ void UpdateColorsIfNecessary(string newColor)
378
378
379
379
// promptBufferCells is the number of visible characters in the prompt
380
380
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
+ }
388
397
}
389
398
}
390
399
0 commit comments