Skip to content

Commit 8e42ad4

Browse files
committed
Make the prediction color configurable via 'Set-PSReadLineOptions'
1 parent 4fb5189 commit 8e42ad4

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public class PSConsoleReadLineOptions
7878
public const ConsoleColor DefaultEmphasisColor = ConsoleColor.Cyan;
7979
public const ConsoleColor DefaultErrorColor = ConsoleColor.Red;
8080

81+
// Use dark black by default for the suggestion text.
82+
// Find the most suitable color using https://stackoverflow.com/a/33206814
83+
public const string DefaultPredictionColor = "\x1b[38;5;238m";
84+
8185
public static EditMode DefaultEditMode = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
8286
? EditMode.Windows
8387
: EditMode.Emacs;
@@ -407,6 +411,12 @@ public object SelectionColor
407411
set => _selectionColor = VTColorUtils.AsEscapeSequence(value);
408412
}
409413

414+
public object PredictionColor
415+
{
416+
get => _predictionColor;
417+
set => _predictionColor = VTColorUtils.AsEscapeSequence(value);
418+
}
419+
410420
internal string _defaultTokenColor;
411421
internal string _commentColor;
412422
internal string _keywordColor;
@@ -421,6 +431,7 @@ public object SelectionColor
421431
internal string _emphasisColor;
422432
internal string _errorColor;
423433
internal string _selectionColor;
434+
internal string _predictionColor;
424435

425436
internal void ResetColors()
426437
{
@@ -438,6 +449,7 @@ internal void ResetColors()
438449
MemberColor = DefaultNumberColor;
439450
EmphasisColor = DefaultEmphasisColor;
440451
ErrorColor = DefaultErrorColor;
452+
PredictionColor = DefaultPredictionColor;
441453

442454
var bg = Console.BackgroundColor;
443455
if (fg == VTColorUtils.UnknownColor || bg == VTColorUtils.UnknownColor)
@@ -473,7 +485,8 @@ internal void SetColor(string property, object value)
473485
{"Type", (o, v) => o.TypeColor = v},
474486
{"Number", (o, v) => o.NumberColor = v},
475487
{"Member", (o, v) => o.MemberColor = v},
476-
{"Selection", (o, v) => o.SelectionColor = v },
488+
{"Selection", (o, v) => o.SelectionColor = v},
489+
{"Prediction", (o, v) => o.PredictionColor = v},
477490
};
478491

479492
Interlocked.CompareExchange(ref ColorSetters, setters, null);

PSReadLine/PSReadLine.format.ps1xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ $d = [Microsoft.PowerShell.KeyHandler]::GetGroupingDescription($_.Group)
202202
<Label>ParameterColor</Label>
203203
<ScriptBlock>[Microsoft.PowerShell.VTColorUtils]::FormatColor($_.ParameterColor)</ScriptBlock>
204204
</ListItem>
205+
<ListItem>
206+
<Label>PredictionColor</Label>
207+
<ScriptBlock>[Microsoft.PowerShell.VTColorUtils]::FormatColor($_.PredictionColor)</ScriptBlock>
208+
</ListItem>
205209
<ListItem>
206210
<Label>SelectionColor</Label>
207211
<ScriptBlock>[Microsoft.PowerShell.VTColorUtils]::FormatColor($_.SelectionColor)</ScriptBlock>

PSReadLine/Render.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,7 @@ void MaybeEmphasize(int i, string currColor)
373373
inSelectedRegion = false;
374374
if (suggestion != null)
375375
{
376-
// TODO: write suggestion out in dark black by default, but it needs to be configurable.
377-
// Find the most suitable color using https://stackoverflow.com/a/33206814
378-
color = "\x1b[38;5;238m";
376+
color = _options._predictionColor;
379377
UpdateColorsIfNecessary(color);
380378

381379
foreach (char charToRender in suggestion)

docs/Set-PSReadLineOption.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ The valid keys include:
389389

390390
-- Member: The member name token color.
391391

392+
-- Prediction: The color for the suggestion text that comes from the prediction API.
393+
392394
```yaml
393395
Type: Hashtable
394396
Parameter Sets: (All)

0 commit comments

Comments
 (0)