Skip to content

Commit 657c76b

Browse files
authored
Merge pull request #388 from daviwil/fix-387
Fix #387: Add foreground colors and prefixes to Write-* command output
2 parents f04fc5f + 2197548 commit 657c76b

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/PowerShellEditorServices/Properties/AssemblyInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
using System.Runtime.CompilerServices;
77

8+
[assembly: InternalsVisibleTo("Microsoft.PowerShell.EditorServices.Test")]
89
[assembly: InternalsVisibleTo("Microsoft.PowerShell.EditorServices.Test.Shared")]
910

src/PowerShellEditorServices/Session/SessionPSHostUserInterface.cs

+20-9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ internal class ConsoleServicePSHostUserInterface : PSHostUserInterface, IHostUIS
3030

3131
#endregion
3232

33+
#region Public Constants
34+
35+
public const string DebugMessagePrefix = "DEBUG: ";
36+
public const string WarningMessagePrefix = "WARNING: ";
37+
public const string VerboseMessagePrefix = "VERBOSE: ";
38+
39+
#endregion
40+
3341
#region Properties
3442

3543
internal IConsoleHost ConsoleHost
@@ -57,9 +65,9 @@ public ConsoleServicePSHostUserInterface(bool enableConsoleRepl)
5765
{
5866
if (enableConsoleRepl)
5967
{
60-
// Set the output encoding to Unicode so that all
61-
// Unicode characters are written correctly
62-
System.Console.OutputEncoding = System.Text.Encoding.Unicode;
68+
// Set the output encoding to UTF-8 so that special
69+
// characters are written to the console correctly
70+
System.Console.OutputEncoding = System.Text.Encoding.UTF8;
6371
}
6472

6573
this.rawUserInterface =
@@ -338,9 +346,10 @@ public override void WriteDebugLine(string message)
338346
if (this.consoleHost != null)
339347
{
340348
this.consoleHost.WriteOutput(
341-
message,
349+
DebugMessagePrefix + message,
342350
true,
343-
OutputType.Debug);
351+
OutputType.Debug,
352+
foregroundColor: ConsoleColor.Yellow);
344353
}
345354
}
346355

@@ -349,9 +358,10 @@ public override void WriteVerboseLine(string message)
349358
if (this.consoleHost != null)
350359
{
351360
this.consoleHost.WriteOutput(
352-
message,
361+
VerboseMessagePrefix + message,
353362
true,
354-
OutputType.Verbose);
363+
OutputType.Verbose,
364+
foregroundColor: ConsoleColor.Blue);
355365
}
356366
}
357367

@@ -360,9 +370,10 @@ public override void WriteWarningLine(string message)
360370
if (this.consoleHost != null)
361371
{
362372
this.consoleHost.WriteOutput(
363-
message,
373+
WarningMessagePrefix + message,
364374
true,
365-
OutputType.Warning);
375+
OutputType.Warning,
376+
foregroundColor: ConsoleColor.Yellow);
366377
}
367378
}
368379

test/PowerShellEditorServices.Test/Console/ConsoleServiceTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ await this.powerShellContext.ExecuteScriptString(
119119
TestOutputString));
120120

121121
Assert.Equal(
122-
TestOutputString + Environment.NewLine,
122+
ConsoleServicePSHostUserInterface.VerboseMessagePrefix + TestOutputString + Environment.NewLine,
123123
this.GetOutputForType(OutputType.Verbose));
124124
}
125125

@@ -137,7 +137,7 @@ await this.powerShellContext.ExecuteScriptString(
137137
TestOutputString));
138138

139139
Assert.Equal(
140-
TestOutputString + Environment.NewLine,
140+
ConsoleServicePSHostUserInterface.DebugMessagePrefix + TestOutputString + Environment.NewLine,
141141
this.GetOutputForType(OutputType.Debug));
142142
}
143143

@@ -150,7 +150,7 @@ await this.powerShellContext.ExecuteScriptString(
150150
TestOutputString));
151151

152152
Assert.Equal(
153-
TestOutputString + Environment.NewLine,
153+
ConsoleServicePSHostUserInterface.WarningMessagePrefix + TestOutputString + Environment.NewLine,
154154
this.GetOutputForType(OutputType.Warning));
155155
}
156156

0 commit comments

Comments
 (0)