Skip to content

Commit 0e84a68

Browse files
authored
Update console logs rendering to skip default background color (#8762)
* Update console logs rendering to skip default background color * Update * Fix tests
1 parent 238fb05 commit 0e84a68

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ private void LoadLogs(ConsoleLogsSubscription newConsoleLogsSubscription)
471471
// Console logs are filtered in the UI by the timestamp of the log entry.
472472
var timestampFilterDate = GetFilteredDateFromRemove();
473473

474-
var logParser = new LogParser();
474+
var logParser = new LogParser(ConsoleColor.Black);
475475
await foreach (var batch in subscription.ConfigureAwait(true))
476476
{
477477
if (batch.Count is 0)

src/Aspire.Dashboard/ConsoleLogs/AnsiParser.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static string StripControlSequences(string text)
6464
return outputBuilder?.ToString() ?? text;
6565
}
6666

67-
public static ConversionResult ConvertToHtml(string? text, ParserState? priorResidualState = null)
67+
public static ConversionResult ConvertToHtml(string? text, ParserState? priorResidualState = null, ConsoleColor? defaultBackgroundColor = null)
6868
{
6969
var textStartIndex = -1;
7070
var textLength = 0;
@@ -147,7 +147,7 @@ public static ConversionResult ConvertToHtml(string? text, ParserState? priorRes
147147
// Ignore everything else and don't write sequence to the output.
148148
if (finalByte == DisplayAttributesFinalByte)
149149
{
150-
ProcessParameters(ref newState, parameters);
150+
ProcessParameters(defaultBackgroundColor, ref newState, parameters);
151151
}
152152

153153
continue;
@@ -197,7 +197,7 @@ public static ConversionResult ConvertToHtml(string? text, ParserState? priorRes
197197
return new(outputBuilder.ToString(), currentState);
198198
}
199199

200-
private static void ProcessParameters(ref ParserState newState, int[] parameters)
200+
private static void ProcessParameters(ConsoleColor? defaultBackgroundColor, ref ParserState newState, int[] parameters)
201201
{
202202
for (var i = 0; i < parameters.Length; i++)
203203
{
@@ -228,7 +228,9 @@ private static void ProcessParameters(ref ParserState newState, int[] parameters
228228
}
229229
else if (TryGetBackgroundColor(parameter, out color))
230230
{
231-
newState.BackgroundColor = color;
231+
// Don't set the background color if it matches the default background color.
232+
// Skipping setting it improves appearance when row mouseover slightly changes color.
233+
newState.BackgroundColor = (color != defaultBackgroundColor) ? color : null;
232234
}
233235
else if (parameter == DefaultBackgroundCode)
234236
{
@@ -516,14 +518,14 @@ private static string ProcessStateChange(ParserState currentState, ParserState n
516518
{
517519
return state.ForegroundColor switch
518520
{
519-
ConsoleColor.Black => state.Bright ? "ansi-fg-brightblack" : "ansi-fg-black",
520-
ConsoleColor.Blue => state.Bright ? "ansi-fg-brightblue" : "ansi-fg-blue",
521-
ConsoleColor.Cyan => state.Bright ? "ansi-fg-brightcyan" : "ansi-fg-cyan",
522-
ConsoleColor.Green => state.Bright ? "ansi-fg-brightgreen" : "ansi-fg-green",
521+
ConsoleColor.Black => state.Bright ? "ansi-fg-brightblack" : "ansi-fg-black",
522+
ConsoleColor.Blue => state.Bright ? "ansi-fg-brightblue" : "ansi-fg-blue",
523+
ConsoleColor.Cyan => state.Bright ? "ansi-fg-brightcyan" : "ansi-fg-cyan",
524+
ConsoleColor.Green => state.Bright ? "ansi-fg-brightgreen" : "ansi-fg-green",
523525
ConsoleColor.Magenta => state.Bright ? "ansi-fg-brightmagenta" : "ansi-fg-magenta",
524-
ConsoleColor.Red => state.Bright ? "ansi-fg-brightred" : "ansi-fg-red",
525-
ConsoleColor.White => state.Bright ? "ansi-fg-brightwhite" : "ansi-fg-white",
526-
ConsoleColor.Yellow => state.Bright ? "ansi-fg-brightyellow" : "ansi-fg-yellow",
526+
ConsoleColor.Red => state.Bright ? "ansi-fg-brightred" : "ansi-fg-red",
527+
ConsoleColor.White => state.Bright ? "ansi-fg-brightwhite" : "ansi-fg-white",
528+
ConsoleColor.Yellow => state.Bright ? "ansi-fg-brightyellow" : "ansi-fg-yellow",
527529
_ => ""
528530
};
529531
}
@@ -532,14 +534,14 @@ private static string ProcessStateChange(ParserState currentState, ParserState n
532534
{
533535
return state.BackgroundColor switch
534536
{
535-
ConsoleColor.Black => "ansi-bg-black",
536-
ConsoleColor.Blue => "ansi-bg-blue",
537-
ConsoleColor.Cyan => "ansi-bg-cyan",
538-
ConsoleColor.Green => "ansi-bg-green",
537+
ConsoleColor.Black => "ansi-bg-black",
538+
ConsoleColor.Blue => "ansi-bg-blue",
539+
ConsoleColor.Cyan => "ansi-bg-cyan",
540+
ConsoleColor.Green => "ansi-bg-green",
539541
ConsoleColor.Magenta => "ansi-bg-magenta",
540-
ConsoleColor.Red => "ansi-bg-red",
541-
ConsoleColor.White => "ansi-bg-white",
542-
ConsoleColor.Yellow => "ansi-bg-yellow",
542+
ConsoleColor.Red => "ansi-bg-red",
543+
ConsoleColor.White => "ansi-bg-white",
544+
ConsoleColor.Yellow => "ansi-bg-yellow",
543545
_ => ""
544546
};
545547
}

src/Aspire.Dashboard/ConsoleLogs/LogParser.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ namespace Aspire.Dashboard.ConsoleLogs;
88

99
internal sealed class LogParser
1010
{
11+
private readonly ConsoleColor _defaultBackgroundColor;
1112
private AnsiParser.ParserState? _residualState;
1213

14+
public LogParser(ConsoleColor defaultBackgroundColor)
15+
{
16+
_defaultBackgroundColor = defaultBackgroundColor;
17+
}
18+
1319
public LogEntry CreateLogEntry(string rawText, bool isErrorOutput)
1420
{
1521
// Several steps to do here:
@@ -39,7 +45,7 @@ public LogEntry CreateLogEntry(string rawText, bool isErrorOutput)
3945
var updatedText = WebUtility.HtmlEncode(s);
4046

4147
// 3b. Parse the content to look for ANSI Control Sequences and color them if possible
42-
var conversionResult = AnsiParser.ConvertToHtml(updatedText, _residualState);
48+
var conversionResult = AnsiParser.ConvertToHtml(updatedText, _residualState, _defaultBackgroundColor);
4349
updatedText = conversionResult.ConvertedText;
4450
_residualState = conversionResult.ResidualState;
4551

tests/Aspire.Dashboard.Tests/ConsoleLogsTests/LogEntriesTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private static LogEntries CreateLogEntries(int? maximumEntryCount = null, int? b
1818

1919
private static void AddLogLine(LogEntries logEntries, string content, bool isError)
2020
{
21-
var logParser = new LogParser();
21+
var logParser = new LogParser(ConsoleColor.Black);
2222
var logEntry = logParser.CreateLogEntry(content, isError);
2323
logEntries.InsertSorted(logEntry);
2424
}
@@ -268,12 +268,27 @@ public void InsertSorted_TrimsToMaximumEntryCount_OutOfOrder()
268268
public void CreateLogEntry_AnsiAndUrl_HasUrlAnchor()
269269
{
270270
// Arrange
271-
var parser = new LogParser();
271+
var parser = new LogParser(ConsoleColor.Black);
272272

273273
// Act
274274
var entry = parser.CreateLogEntry("\x1b[36mhttps://www.example.com\u001b[0m", isErrorOutput: false);
275275

276276
// Assert
277277
Assert.Equal("<span class=\"ansi-fg-cyan\"></span><a target=\"_blank\" href=\"https://www.example.com\" rel=\"noopener noreferrer nofollow\">https://www.example.com</a>", entry.Content);
278278
}
279+
280+
[Theory]
281+
[InlineData(ConsoleColor.Black, @"<span class=""ansi-fg-green"">info</span>: LoggerName")]
282+
[InlineData(ConsoleColor.Blue, @"<span class=""ansi-fg-green ansi-bg-black"">info</span>: LoggerName")]
283+
public void CreateLogEntry_DefaultBackgroundColor_SkipMatchingColor(ConsoleColor defaultBackgroundColor, string output)
284+
{
285+
// Arrange
286+
var parser = new LogParser(defaultBackgroundColor);
287+
288+
// Act
289+
var entry = parser.CreateLogEntry("\u001b[40m\u001b[32minfo\u001b[39m\u001b[22m\u001b[49m: LoggerName", isErrorOutput: false);
290+
291+
// Assert
292+
Assert.Equal(output, entry.Content);
293+
}
279294
}

0 commit comments

Comments
 (0)