public void Parse_SetBackgroundForegroundAndMessageThenReset_Success(ConsoleColor background, ConsoleColor foreground) { // Arrange var message = AnsiParser.GetBackgroundColorEscapeCode(background) + AnsiParser.GetForegroundColorEscapeCode(foreground) + "Request received" + AnsiParser.DefaultForegroundColor //resets foreground color + AnsiParser.DefaultBackgroundColor; //resets background color var segments = new List <ConsoleContext>(); Action <string, int, int, ConsoleColor?, ConsoleColor?> onParseWrite = (message, startIndex, length, bg, fg) => { segments.Add(new ConsoleContext() { BackgroundColor = bg, ForegroundColor = fg, Message = message.AsSpan().Slice(startIndex, length).ToString() }); }; var parser = new AnsiParser(onParseWrite); // Act parser.Parse(message); // Assert Assert.Equal(1, segments.Count); Assert.Equal("Request received", segments[0].Message); VerifyForeground(foreground, segments[0]); VerifyBackground(background, segments[0]); }
public void Parse_RepeatedColorChange_PicksLastSet() { // Arrange var message = AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.DarkRed) + AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.DarkGreen) + AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.DarkBlue) + AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.Gray) + AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.Cyan) + AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.Yellow) + "Request received" + AnsiParser.DefaultForegroundColor //resets foreground color + AnsiParser.DefaultBackgroundColor; //resets background color var segments = new List <ConsoleContext>(); Action <string, int, int, ConsoleColor?, ConsoleColor?> onParseWrite = (message, startIndex, length, bg, fg) => { segments.Add(new ConsoleContext() { BackgroundColor = bg, ForegroundColor = fg, Message = message.AsSpan().Slice(startIndex, length).ToString() }); }; var parser = new AnsiParser(onParseWrite); // Act parser.Parse(message); // Assert Assert.Equal(1, segments.Count); Assert.Equal("Request received", segments[0].Message); VerifyBackground(ConsoleColor.DarkBlue, segments[0]); VerifyForeground(ConsoleColor.Yellow, segments[0]); }
public void WriteColoredMessage_WithForegroundEscapeCode_AndNoBackgroundColorSpecified() { // Arrange var message = "Request received"; var expectedMessage = AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.DarkGreen) + message + "\x1B[39m\x1B[22m"; //resets foreground color var textWriter = new StringWriter(); // Act textWriter.WriteColoredMessage(message, background: null, foreground: ConsoleColor.DarkGreen); // Assert Assert.Equal(expectedMessage, textWriter.ToString()); }
public void Parse_MessageWithMultipleColors_ParsedIntoMultipleSegments() { // Arrange var message = AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.DarkRed) + AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.Gray) + "Message1" + AnsiParser.DefaultForegroundColor + AnsiParser.DefaultBackgroundColor + "NoColor" + AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.DarkGreen) + AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.Cyan) + "Message2" + AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.DarkBlue) + AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.Yellow) + "Message3" + AnsiParser.DefaultForegroundColor + AnsiParser.DefaultBackgroundColor; var segments = new List <ConsoleContext>(); Action <string, int, int, ConsoleColor?, ConsoleColor?> onParseWrite = (message, startIndex, length, bg, fg) => { segments.Add(new ConsoleContext() { BackgroundColor = bg, ForegroundColor = fg, Message = message.AsSpan().Slice(startIndex, length).ToString() }); }; var parser = new AnsiParser(onParseWrite); // Act parser.Parse(message); // Assert Assert.Equal(4, segments.Count); Assert.Equal("NoColor", segments[1].Message); Assert.Null(segments[1].ForegroundColor); Assert.Null(segments[1].BackgroundColor); Assert.Equal("Message1", segments[0].Message); Assert.Equal("Message2", segments[2].Message); Assert.Equal("Message3", segments[3].Message); VerifyBackground(ConsoleColor.DarkRed, segments[0]); VerifyBackground(ConsoleColor.DarkGreen, segments[2]); VerifyBackground(ConsoleColor.DarkBlue, segments[3]); VerifyForeground(ConsoleColor.Gray, segments[0]); VerifyForeground(ConsoleColor.Cyan, segments[2]); VerifyForeground(ConsoleColor.Yellow, segments[3]); }
public void WriteColoredMessage_InOrder_WhenBothForegroundOrBackgroundColorsSpecified() { // Arrange var message = "Request received"; var expectedMessage = AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.Red) + AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.DarkGreen) + "Request received" + "\x1B[39m\x1B[22m" //resets foreground color + "\x1B[49m"; //resets background color var textWriter = new StringWriter(); // Act textWriter.WriteColoredMessage(message, ConsoleColor.Red, ConsoleColor.DarkGreen); // Assert Assert.Equal(expectedMessage, textWriter.ToString()); }
private static bool IsForegroundColorNotSupported(ConsoleColor color) { return(AnsiParser.GetForegroundColorEscapeCode(color).Equals( AnsiParser.DefaultForegroundColor, StringComparison.OrdinalIgnoreCase)); }