Skip to content

Commit 599e178

Browse files
lzybkrdaxian-dbw
authored andcommitted
Improve handling of color in prompts (#1180)
1 parent b995497 commit 599e178

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public object ContinuationPromptColor
321321
/// If the prompt function is pure, this value can be inferred, e.g.
322322
/// the default prompt will use "> " for this value.
323323
/// </summary>
324-
public string PromptText { get; set; }
324+
public string[] PromptText { get; set; }
325325

326326
public object DefaultTokenColor
327327
{
@@ -667,7 +667,7 @@ public int AnsiEscapeTimeout
667667

668668
[Parameter]
669669
[ValidateNotNull]
670-
public string PromptText { get; set; }
670+
public string[] PromptText { get; set; }
671671

672672
[Parameter]
673673
public ViModeStyle ViModeIndicator

PSReadLine/ReadLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ private void DelayedOneTimeInitialize()
774774

775775
if (i >= 0)
776776
{
777-
_options.PromptText = evaluatedPrompt.Substring(i);
777+
_options.PromptText = new [] { evaluatedPrompt.Substring(i) };
778778
}
779779
}
780780
}

PSReadLine/Render.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,18 @@ void MaybeEmphasize(int i, string currColor)
366366
/// </returns>
367367
private bool RenderErrorPrompt(RenderData renderData, string defaultColor)
368368
{
369-
// We may need to flip the color on the prompt if the error state changed.
370-
371-
int bufferWidth = _console.BufferWidth;
372-
string promptText = _options.PromptText;
373-
374-
if (string.IsNullOrEmpty(promptText) || _initialY < 0)
369+
if (_initialY < 0
370+
|| _options.PromptText == null
371+
|| _options.PromptText.Length == 0
372+
|| String.IsNullOrEmpty(_options.PromptText[0]))
375373
{
376374
// No need to flip the prompt color if either the error prompt is not defined
377375
// or the initial cursor point has already been scrolled off the buffer.
378376
return false;
379377
}
380378

379+
// We may need to flip the color on the prompt if the error state changed.
380+
381381
renderData.errorPrompt = (_parseErrors != null && _parseErrors.Length > 0);
382382
if (renderData.errorPrompt == _previousRender.errorPrompt)
383383
{
@@ -388,9 +388,15 @@ private bool RenderErrorPrompt(RenderData renderData, string defaultColor)
388388
// We need to update the prompt
389389
_console.SetCursorPosition(_initialX, _initialY);
390390

391+
string promptText =
392+
(renderData.errorPrompt && _options.PromptText.Length == 2)
393+
? _options.PromptText[1]
394+
: _options.PromptText[0];
395+
391396
// promptBufferCells is the number of visible characters in the prompt
392397
int promptBufferCells = LengthInBufferCells(promptText);
393398
bool renderErrorPrompt = false;
399+
int bufferWidth = _console.BufferWidth;
394400

395401
if (_console.CursorLeft >= promptBufferCells)
396402
{
@@ -421,12 +427,15 @@ private bool RenderErrorPrompt(RenderData renderData, string defaultColor)
421427

422428
if (renderErrorPrompt)
423429
{
424-
string color = renderData.errorPrompt ? _options._errorColor : defaultColor;
425-
if (renderData.errorPrompt && promptBufferCells != promptText.Length)
430+
if (!promptText.Contains('\x1b'))
426431
{
427-
promptText = promptText.Substring(promptText.Length - promptBufferCells);
432+
string color = renderData.errorPrompt ? _options._errorColor : defaultColor;
433+
if (renderData.errorPrompt && promptBufferCells != promptText.Length)
434+
{
435+
promptText = promptText.Substring(promptText.Length - promptBufferCells);
436+
}
437+
_console.Write(color);
428438
}
429-
_console.Write(color);
430439
_console.Write(promptText);
431440
_console.Write("\x1b[0m");
432441
}

test/UnitTestReadLine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private void SetPrompt(string prompt)
354354
var options = new SetPSReadLineOption {ExtraPromptLineCount = 0};
355355
if (string.IsNullOrEmpty(prompt))
356356
{
357-
options.PromptText = "";
357+
options.PromptText = new [] {""};
358358
PSConsoleReadLine.SetOptions(options);
359359
return;
360360
}
@@ -365,7 +365,7 @@ private void SetPrompt(string prompt)
365365
if (!char.IsWhiteSpace(prompt[i])) break;
366366
}
367367

368-
options.PromptText = prompt.Substring(i);
368+
options.PromptText = new [] { prompt.Substring(i) };
369369

370370
var lineCount = 1 + prompt.Count(c => c == '\n');
371371
if (lineCount > 1)
@@ -474,7 +474,7 @@ private void TestSetup(KeyMode keyMode, params KeyHandler[] keyHandlers)
474474
MaximumKillRingCount = PSConsoleReadLineOptions.DefaultMaximumKillRingCount,
475475
ShowToolTips = PSConsoleReadLineOptions.DefaultShowToolTips,
476476
WordDelimiters = PSConsoleReadLineOptions.DefaultWordDelimiters,
477-
PromptText = "",
477+
PromptText = new [] {""},
478478
Colors = new Hashtable {
479479
{ "ContinuationPrompt", MakeCombinedColor(_console.ForegroundColor, _console.BackgroundColor) },
480480
{ "Emphasis", MakeCombinedColor(PSConsoleReadLineOptions.DefaultEmphasisColor, _console.BackgroundColor) },

0 commit comments

Comments
 (0)