示例#1
0
        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]);
        }
示例#2
0
        public void Parse_ValidSupportedOrUnsupportedCodesInMessage_MessageParsedSuccessfully(string messageWithUnsupportedCode, params string[] output)
        {
            // Arrange
            var message  = messageWithUnsupportedCode;
            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(messageWithUnsupportedCode);

            // Assert
            Assert.Equal(output.Length, segments.Count);
            for (int i = 0; i < output.Length; i++)
            {
                Assert.Equal(output[i], segments[i].Message);
            }
        }
示例#3
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]);
        }
示例#4
0
        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]);
        }
示例#5
0
        public void Parse_CheckTimesWrittenToConsole(int numSegments, string message, string lastSegment)
        {
            // Arrange
            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(numSegments, segments.Count);
            Assert.Equal(lastSegment, segments.Last().Message);
        }
示例#6
0
        private Task ConsoleOutputAsync(Stream stream)
        {
            return(Task.Run(async delegate
            {
                var ansiParser = new AnsiParser();
                var sr = new StreamReader(stream, Encoding.UTF8);
                do
                {
                    int offset = 0;
                    var buffer = new char[1024];
                    int readChars = await sr.ReadAsync(buffer, offset, buffer.Length - offset);
                    File.WriteAllText("dbg.txt", new string(buffer));
                    if (readChars > 0)
                    {
                        var reader = new ArrayReader <char>(buffer, 0, readChars);
                        var codes = ansiParser.Parse(reader);
                        ReceiveOutput(codes);
                    }
                } while (!sr.EndOfStream);

                ;
            }));
        }
示例#7
0
 public void Write(string message)
 {
     _parser.Parse(message);
 }