public void SpanVisitor_visits_child_spans_in_depth_first_order() { var outerContainer = new ContainerSpan( BackgroundColorSpan.Green(), new ContainerSpan( ForegroundColorSpan.Red(), new ContentSpan("the content"), ForegroundColorSpan.Reset()), BackgroundColorSpan.Reset()); var visitor = new RecordingSpanVisitor(); visitor.Visit(outerContainer); visitor.VisitedSpans .Select(s => s.GetType()) .Should() .BeEquivalentSequenceTo( typeof(ContainerSpan), typeof(BackgroundColorSpan), typeof(ContainerSpan), typeof(ForegroundColorSpan), typeof(ContentSpan), typeof(ForegroundColorSpan), typeof(BackgroundColorSpan)); }
public void ForegroundColorSpans_are_replaced_with_System_Console_calls_during_non_ANSI_rendering() { var span = _textSpanFormatter.ParseToSpan( $"{ForegroundColorSpan.Red()}red {ForegroundColorSpan.Blue()}blue {ForegroundColorSpan.Green()}green {ForegroundColorSpan.Reset()}or a {ForegroundColorSpan.Rgb(12, 34, 56)}little of each." ); var renderer = new ConsoleRenderer(_terminal, OutputMode.NonAnsi); renderer.RenderToRegion(span, new Region(0, 0, 200, 1, false)); _terminal.Events .Should() .BeEquivalentSequenceTo( new CursorPositionChanged(new Point(0, 0)), new ForegroundColorChanged(ConsoleColor.DarkRed), new ContentWritten("red "), new ForegroundColorChanged(ConsoleColor.DarkBlue), new ContentWritten("blue "), new ForegroundColorChanged(ConsoleColor.DarkGreen), new ContentWritten("green "), new ColorReset(), new BackgroundColorChanged(ConsoleColor.Black), new ContentWritten("or a "), new ColorReset(), new BackgroundColorChanged(ConsoleColor.Black), new ContentWritten("little of each.") ); }
public void ToString_with_non_ansi_omits_ANSI_codes() { var span = new TextSpanFormatter().ParseToSpan( $"one{ForegroundColorSpan.Red()}two{ForegroundColorSpan.Reset()}three" ); span.ToString(OutputMode.NonAnsi).Should().Be("onetwothree"); }
public void ToString_with_ansi_includes_ANSI_codes() { var span = new TextSpanFormatter() .ParseToSpan($"one{ForegroundColorSpan.Red()}two{ForegroundColorSpan.Reset()}three"); span.ToString(OutputMode.Ansi) .Should() .Be($"one{Ansi.Color.Foreground.Red.EscapeSequence}two{Ansi.Color.Foreground.Default.EscapeSequence}three"); }
public void When_spans_are_nested_then_content_length_can_be_calculated() { var span = new ContainerSpan( ForegroundColorSpan.Red(), new ContentSpan("content"), ForegroundColorSpan.Reset()); span.ContentLength.Should().Be("content".Length); }
public void ForegroundColorSpans_are_removed_during_file_rendering() { var span = _spanFormatter.ParseToSpan( $"{ForegroundColorSpan.Red()}red {ForegroundColorSpan.Blue()}blue {ForegroundColorSpan.Green()}green {ForegroundColorSpan.Reset()}or a {ForegroundColorSpan.Rgb(12, 34, 56)}little of each."); var renderer = new ConsoleRenderer(_console, OutputMode.File); var expected = "red blue green or a little of each."; renderer.RenderToRegion(span, new Region(0, 0, expected.Length, 1, false)); _console.Out.ToString().Should().Be(expected); }
public void When_in_ANSI_mode_then_ContentWritten_events_do_not_include_escape_sequences() { var terminal = (TestTerminal)GetTerminal(); var renderer = new ConsoleRenderer(terminal, OutputMode.Ansi); var region = new Region(0, 0, 4, 1); renderer.RenderToRegion($"{ForegroundColorSpan.Red()}text{ForegroundColorSpan.Reset()}", region); terminal.Events .Should() .Contain(e => e is TestTerminal.ContentWritten && ((TestTerminal.ContentWritten)e).Content == "text"); }
public void ForegroundColorSpans_are_replaced_with_ANSI_codes_during_ANSI_rendering() { var span = _spanFormatter.ParseToSpan( $"{ForegroundColorSpan.Red()}red {ForegroundColorSpan.Blue()}blue {ForegroundColorSpan.Green()}green {ForegroundColorSpan.Reset()}or a {ForegroundColorSpan.Rgb(12, 34, 56)}little of each."); var renderer = new ConsoleRenderer(_console, OutputMode.Ansi); renderer.RenderToRegion(span, new Region(0, 0, 200, 1, false)); _console.Out .ToString() .Should() .Contain( $"{Ansi.Color.Foreground.Red.EscapeSequence}red {Ansi.Color.Foreground.Blue.EscapeSequence}blue {Ansi.Color.Foreground.Green.EscapeSequence}green {Ansi.Color.Foreground.Default.EscapeSequence}or a {Ansi.Color.Foreground.Rgb(12, 34, 56).EscapeSequence}little of each."); }
public void ContentWritten_events_do_not_include_escape_sequences() { var console = new TestConsole(); var renderer = new ConsoleRenderer(console, OutputMode.Ansi); var region = new Region(0, 0, 4, 1); renderer.RenderToRegion($"{ForegroundColorSpan.Red()}text{ForegroundColorSpan.Reset()}", region); console.Events .Should() .Contain(e => e is ContentWritten && ((ContentWritten)e).Content == "text"); }
public void Spans_have_a_start_relative_to_the_parent_span() { var span = new ContainerSpan( ForegroundColorSpan.Red(), new ContentSpan("first"), ForegroundColorSpan.Blue(), new ContentSpan("second"), ForegroundColorSpan.Reset()); span[0].Start.Should().Be(0); span[1].Start.Should().Be(0); span[2].Start.Should().Be("first".Length); span[3].Start.Should().Be("first".Length); span[4].Start.Should().Be("firstsecond".Length); }
public void Span_starts_update_when_parent_is_added_to_another_parent_span() { var innerContainerSpan = new ContainerSpan( ForegroundColorSpan.Red(), new ContentSpan("second"), ForegroundColorSpan.Blue(), new ContentSpan("third"), ForegroundColorSpan.Reset() ); var outerContainer = new ContainerSpan(new ContentSpan("first"), innerContainerSpan); innerContainerSpan[0].Start.Should().Be("first".Length); innerContainerSpan[1].Start.Should().Be("first".Length); innerContainerSpan[2].Start.Should().Be("firstsecond".Length); innerContainerSpan[3].Start.Should().Be("firstsecond".Length); innerContainerSpan[4].Start.Should().Be("firstsecondthird".Length); }
public static TextSpan Red(this string value) => new ContainerSpan(ForegroundColorSpan.Red(), new ContentSpan(value), ForegroundColorSpan.Reset());
public static IEnumerable <object[]> TestCases() { return(TestCases().Select(t => new object[] { t }).ToArray()); IEnumerable <RenderingTestCase> TestCases() { var testCaseName = $"{nameof(ContentSpan)} only"; yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick brown fox jumps over the lazy dog.", inRegion: new Region(0, 0, 3, 4), Line(0, 0, "The"), Line(0, 1, "qui"), Line(0, 2, "bro"), Line(0, 3, "fox") )); yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick brown fox jumps over the lazy dog.", inRegion: new Region(12, 12, 3, 4), Line(12, 12, "The"), Line(12, 13, "qui"), Line(12, 14, "bro"), Line(12, 15, "fox") )); testCaseName = $"{nameof(ControlSpan)} at start of {nameof(ContentSpan)}"; yield return(new RenderingTestCase( name: testCaseName, rendering: $"{ForegroundColorSpan.Red()}The quick brown fox jumps over the lazy dog.", inRegion: new Region(0, 0, 3, 4), Line(0, 0, $"{Ansi.Color.Foreground.Red.EscapeSequence}The"), Line(0, 1, $"qui"), Line(0, 2, $"bro"), Line(0, 3, $"fox") )); yield return(new RenderingTestCase( name: testCaseName, rendering: $"{ForegroundColorSpan.Red()}The quick brown fox jumps over the lazy dog.", inRegion: new Region(12, 12, 3, 4), Line(12, 12, $"{Ansi.Color.Foreground.Red.EscapeSequence}The"), Line(12, 13, $"qui"), Line(12, 14, $"bro"), Line(12, 15, $"fox") )); testCaseName = $"{nameof(ControlSpan)} at end of {nameof(ContentSpan)}"; yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick brown fox jumps over the lazy dog.{ForegroundColorSpan.Reset()}", inRegion: new Region(0, 0, 3, 4), Line(0, 0, $"The"), Line(0, 1, $"qui"), Line(0, 2, $"bro"), Line(0, 3, $"fox{Ansi.Color.Foreground.Default.EscapeSequence}") )); yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick brown fox jumps over the lazy dog.{ForegroundColorSpan.Reset()}", inRegion: new Region(12, 12, 3, 4), Line(12, 12, $"The"), Line(12, 13, $"qui"), Line(12, 14, $"bro"), Line(12, 15, $"fox{Ansi.Color.Foreground.Default.EscapeSequence}") )); testCaseName = $"{nameof(ControlSpan)}s around a word inside a {nameof(ContentSpan)}"; yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick {ForegroundColorSpan.Rgb(139, 69, 19)}brown{ForegroundColorSpan.Reset()} fox jumps over the lazy dog.", inRegion: new Region(0, 0, 3, 4), Line(0, 0, $"The"), Line(0, 1, $"qui{Ansi.Color.Foreground.Rgb(139, 69, 19).EscapeSequence}"), Line(0, 2, $"bro{Ansi.Color.Foreground.Default.EscapeSequence}"), Line(0, 3, $"fox") )); yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick {ForegroundColorSpan.Rgb(139, 69, 19)}brown{ForegroundColorSpan.Reset()} fox jumps over the lazy dog.", inRegion: new Region(12, 12, 3, 4), Line(12, 12, $"The"), Line(12, 13, $"qui{Ansi.Color.Foreground.Rgb(139, 69, 19).EscapeSequence}"), Line(12, 14, $"bro{Ansi.Color.Foreground.Default.EscapeSequence}"), Line(12, 15, $"fox") )); testCaseName = $"{nameof(ControlSpan)}s around a word inside a {nameof(ContentSpan)}"; yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick {ForegroundColorSpan.Rgb(139, 69, 19)}brown{ForegroundColorSpan.Reset()} fox jumps over the lazy dog.", inRegion: new Region( 0, 0, "The quick brown fox jumps over the lazy dog.".Length, 1 ), expectOutput: Line( 0, 0, $"The quick {Ansi.Color.Foreground.Rgb(139, 69, 19).EscapeSequence}brown{Ansi.Color.Foreground.Default.EscapeSequence} fox jumps over the lazy dog." ) )); yield return(new RenderingTestCase( name: testCaseName, rendering: $"The quick {ForegroundColorSpan.Rgb(139, 69, 19)}brown{ForegroundColorSpan.Reset()} fox jumps over the lazy dog.", inRegion: new Region( 12, 12, "The quick brown fox jumps over the lazy dog.".Length, 1 ), expectOutput: Line( 12, 12, $"The quick {Ansi.Color.Foreground.Rgb(139, 69, 19).EscapeSequence}brown{Ansi.Color.Foreground.Default.EscapeSequence} fox jumps over the lazy dog." ) )); } }
public static void OutputError(this ITerminal terminal, string content) => terminal.OutputColor(ForegroundColorSpan.Red(), content);
internal static FormattableString Red(this string message) { return($"{ForegroundColorSpan.Red()}{message}{ForegroundColorSpan.Reset()}"); }
public void WriteFatal(string content) => WriteColor(ForegroundColorSpan.Red(), content);