Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix one variant of the ArgumentOutOfRangeException issues #967

Merged
merged 2 commits into from
Jul 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions PSReadLine/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,45 @@ void UpdateColorsIfNecessary(string newColor)
// We need to update the prompt

// promptBufferCells is the number of visible characters in the prompt
var promptBufferCells = LengthInBufferCells(promptText);
_console.CursorLeft -= promptBufferCells;
var color = renderData.errorPrompt ? _options._errorColor : defaultColor;
if (renderData.errorPrompt && promptBufferCells != promptText.Length)
promptText = promptText.Substring(promptText.Length - promptBufferCells);
UpdateColorsIfNecessary(color);
_console.Write(promptText);
_console.Write("\x1b[0m");
int promptBufferCells = LengthInBufferCells(promptText);
bool renderErrorPrompt = false;

if (_console.CursorLeft >= promptBufferCells)
{
renderErrorPrompt = true;
_console.CursorLeft -= promptBufferCells;
}
else
{
// The 'CursorLeft' could be less than error-prompt-cell-length in one of the following 3 cases:
// 1. console buffer was resized, which causes the initial cursor to appear on the next line;
// 2. prompt string gets longer (e.g. by 'cd' into nested folders), which causes the line to be wrapped to the next line;
// 3. the prompt function was changed, which causes the new prompt string is shorter than the error prompt.
// Here, we always assume it's the case 1 or 2, and wrap back to the previous line to change the error prompt color.
// In case of case 3, the rendering would be off, but it's more of a user error because the prompt is changed without
// updating 'PromptText' with 'Set-PSReadLineOption'.

int diffs = promptBufferCells - _console.CursorLeft;
int newX = bufferWidth - diffs % bufferWidth;
int newY = _initialY - diffs / bufferWidth - 1;

// newY could be less than 0 if 'PromptText' is manually set to be a long string.
if (newY >= 0)
{
renderErrorPrompt = true;
_console.SetCursorPosition(newX, newY);
}
}

if (renderErrorPrompt)
{
var color = renderData.errorPrompt ? _options._errorColor : defaultColor;
if (renderData.errorPrompt && promptBufferCells != promptText.Length)
promptText = promptText.Substring(promptText.Length - promptBufferCells);
UpdateColorsIfNecessary(color);
_console.Write(promptText);
_console.Write("\x1b[0m");
}
}
}

Expand Down