Skip to content

Commit 43359a8

Browse files
author
GUIGHIL benjamin
committed
Add customize output color enhancement (#651)
1 parent 4716b7f commit 43359a8

File tree

2 files changed

+157
-8
lines changed

2 files changed

+157
-8
lines changed

src/PowerShellEditorServices/Session/Host/EditorServicesPSHost.cs

+123-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.PowerShell.EditorServices.Session;
77
using Microsoft.PowerShell.EditorServices.Utility;
88
using System;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.Management.Automation;
1011
using System.Management.Automation.Host;
1112
using System.Management.Automation.Runspaces;
@@ -78,14 +79,132 @@ public override string Name
7879
}
7980

8081
/// <summary>
81-
///
82+
///
83+
/// </summary>
84+
public class ConsoleColorProxy
85+
{
86+
private EditorServicesPSHostUserInterface _hostUserInterface;
87+
88+
public ConsoleColorProxy(EditorServicesPSHostUserInterface hostUserInterface)
89+
{
90+
if (hostUserInterface == null) throw new ArgumentNullException("hostUserInterface");
91+
_hostUserInterface = hostUserInterface;
92+
}
93+
94+
public ConsoleColor ErrorForegroundColor
95+
{
96+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
97+
get
98+
{ return _hostUserInterface.ErrorForegroundColor; }
99+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
100+
set
101+
{ _hostUserInterface.ErrorForegroundColor = value; }
102+
}
103+
104+
public ConsoleColor ErrorBackgroundColor
105+
{
106+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
107+
get
108+
{ return _hostUserInterface.ErrorBackgroundColor; }
109+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
110+
set
111+
{ _hostUserInterface.ErrorBackgroundColor = value; }
112+
}
113+
114+
public ConsoleColor WarningForegroundColor
115+
{
116+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
117+
get
118+
{ return _hostUserInterface.WarningForegroundColor; }
119+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
120+
set
121+
{ _hostUserInterface.WarningForegroundColor = value; }
122+
}
123+
124+
public ConsoleColor WarningBackgroundColor
125+
{
126+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
127+
get
128+
{ return _hostUserInterface.WarningBackgroundColor; }
129+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
130+
set
131+
{ _hostUserInterface.WarningBackgroundColor = value; }
132+
}
133+
134+
public ConsoleColor DebugForegroundColor
135+
{
136+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
137+
get
138+
{ return _hostUserInterface.DebugForegroundColor; }
139+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
140+
set
141+
{ _hostUserInterface.DebugForegroundColor = value; }
142+
}
143+
144+
public ConsoleColor DebugBackgroundColor
145+
{
146+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
147+
get
148+
{ return _hostUserInterface.DebugBackgroundColor; }
149+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
150+
set
151+
{ _hostUserInterface.DebugBackgroundColor = value; }
152+
}
153+
154+
public ConsoleColor VerboseForegroundColor
155+
{
156+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
157+
get
158+
{ return _hostUserInterface.VerboseForegroundColor; }
159+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
160+
set
161+
{ _hostUserInterface.VerboseForegroundColor = value; }
162+
}
163+
164+
public ConsoleColor VerboseBackgroundColor
165+
{
166+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
167+
get
168+
{ return _hostUserInterface.VerboseBackgroundColor; }
169+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
170+
set
171+
{ _hostUserInterface.VerboseBackgroundColor = value; }
172+
}
173+
174+
public ConsoleColor ProgressForegroundColor
175+
{
176+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
177+
get
178+
{ return _hostUserInterface.ProgressForegroundColor; }
179+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
180+
set
181+
{ _hostUserInterface.ProgressForegroundColor = value; }
182+
}
183+
184+
public ConsoleColor ProgressBackgroundColor
185+
{
186+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
187+
get
188+
{ return _hostUserInterface.ProgressBackgroundColor; }
189+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
190+
set
191+
{ _hostUserInterface.ProgressBackgroundColor = value; }
192+
}
193+
}
194+
195+
/// <summary>
196+
/// Return the actual console host object so that the user can get at
197+
/// the unproxied methods.
82198
/// </summary>
83199
public override PSObject PrivateData
84200
{
85-
// There is no PrivateData yet but by returning an empty object we can get past PowerShell's
86-
// check for $host.PrivateData["window"] which errors on the null returned by default.
87-
get { return new PSObject(); }
201+
get
202+
{
203+
if (hostUserInterface == null) return null;
204+
return _consoleColorProxy ?? (_consoleColorProxy = PSObject.AsPSObject(new ConsoleColorProxy(hostUserInterface)));
205+
}
88206
}
207+
private PSObject _consoleColorProxy;
89208

90209
/// <summary>
91210
///

src/PowerShellEditorServices/Session/Host/EditorServicesPSHostUserInterface.cs

+34-4
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ public override void WriteDebugLine(string message)
529529
DebugMessagePrefix + message,
530530
true,
531531
OutputType.Debug,
532-
foregroundColor: ConsoleColor.Yellow);
532+
foregroundColor: this.DebugForegroundColor,
533+
backgroundColor: this.DebugBackgroundColor);
533534
}
534535

535536
/// <summary>
@@ -542,7 +543,8 @@ public override void WriteVerboseLine(string message)
542543
VerboseMessagePrefix + message,
543544
true,
544545
OutputType.Verbose,
545-
foregroundColor: ConsoleColor.Blue);
546+
foregroundColor: this.VerboseForegroundColor,
547+
backgroundColor: this.VerboseBackgroundColor);
546548
}
547549

548550
/// <summary>
@@ -555,7 +557,8 @@ public override void WriteWarningLine(string message)
555557
WarningMessagePrefix + message,
556558
true,
557559
OutputType.Warning,
558-
foregroundColor: ConsoleColor.Yellow);
560+
foregroundColor: this.WarningForegroundColor,
561+
backgroundColor: this.WarningBackgroundColor);
559562
}
560563

561564
/// <summary>
@@ -568,7 +571,8 @@ public override void WriteErrorLine(string value)
568571
value,
569572
true,
570573
OutputType.Error,
571-
ConsoleColor.Red);
574+
foregroundColor: this.ErrorForegroundColor,
575+
backgroundColor: this.ErrorBackgroundColor);
572576
}
573577

574578
/// <summary>
@@ -684,6 +688,32 @@ private void WriteDebuggerBanner(DebuggerStopEventArgs eventArgs)
684688
}
685689
}
686690

691+
public static ConsoleColor BackgroundColor
692+
{
693+
get;
694+
set;
695+
}
696+
697+
// Error colors
698+
public ConsoleColor ErrorForegroundColor { get; set; } = ConsoleColor.Red;
699+
public ConsoleColor ErrorBackgroundColor { get; set; } = BackgroundColor;
700+
701+
// Warning colors
702+
public ConsoleColor WarningForegroundColor { get; set; } = ConsoleColor.Yellow;
703+
public ConsoleColor WarningBackgroundColor { get; set; } = BackgroundColor;
704+
705+
// Debug colors
706+
public ConsoleColor DebugForegroundColor { get; set; } = ConsoleColor.Yellow;
707+
public ConsoleColor DebugBackgroundColor { get; set; } = BackgroundColor;
708+
709+
// Verbose colors
710+
public ConsoleColor VerboseForegroundColor { get; set; } = ConsoleColor.Yellow;
711+
public ConsoleColor VerboseBackgroundColor { get; set; } = BackgroundColor;
712+
713+
// Progress colors
714+
public ConsoleColor ProgressForegroundColor { get; set; } = ConsoleColor.Yellow;
715+
public ConsoleColor ProgressBackgroundColor { get; set; } = ConsoleColor.DarkCyan;
716+
687717
private async Task StartReplLoop(CancellationToken cancellationToken)
688718
{
689719
do

0 commit comments

Comments
 (0)