コード例 #1
0
        public void A_ForegroundColorSpan_and_a_BackgroundColorSpan_having_the_same_name_are_not_equal()
        {
            var one = new ForegroundColorSpan("green", Ansi.Color.Foreground.Green);
            var two = new BackgroundColorSpan("green", Ansi.Color.Foreground.Green);

            one.Equals(two).Should().BeFalse();
        }
コード例 #2
0
        public void BackgroundColorSpans_with_different_names_are_not_equal()
        {
            var one = new BackgroundColorSpan("red", Ansi.Color.Foreground.Red);
            var two = new BackgroundColorSpan("green", Ansi.Color.Foreground.Green);

            one.Equals(two).Should().BeFalse();
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        public void A_ForegroundColorSpan_and_a_BackgroundColorSpan_having_the_same_name_do_not_have_the_same_hash_code()
        {
            var one = new ForegroundColorSpan("green", Ansi.Color.Foreground.Green);
            var two = new BackgroundColorSpan("green", Ansi.Color.Foreground.Green);

            one.GetHashCode().Should().NotBe(two.GetHashCode());
        }
コード例 #5
0
        public void BackgroundColorSpans_with_equivalent_content_have_the_same_hash_code()
        {
            var one = new BackgroundColorSpan("green", Ansi.Color.Foreground.Green);
            var two = new BackgroundColorSpan("green", Ansi.Color.Foreground.Green);

            one.GetHashCode().Should().Be(two.GetHashCode());
        }
コード例 #6
0
        public void BackgroundColorSpans_are_replaced_with_System_Console_calls_during_non_ANSI_rendering()
        {
            var span = _textSpanFormatter.ParseToSpan(
                $"{BackgroundColorSpan.Red()}red {BackgroundColorSpan.Blue()}blue {BackgroundColorSpan.Green()}green {BackgroundColorSpan.Reset()}or a {BackgroundColorSpan.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 BackgroundColorChanged(ConsoleColor.DarkRed),
                    new ContentWritten("red "),
                    new BackgroundColorChanged(ConsoleColor.DarkBlue),
                    new ContentWritten("blue "),
                    new BackgroundColorChanged(ConsoleColor.DarkGreen),
                    new ContentWritten("green "),
                    new ColorReset(),
                    new ForegroundColorChanged(ConsoleColor.White),
                    new ContentWritten("or a "),
                    new ColorReset(),
                    new ForegroundColorChanged(ConsoleColor.White),
                    new ContentWritten("little of each.")
                );
        }
コード例 #7
0
 public static ContainerSpan StyleHighlight(this string @string, BackgroundColorSpan color)
 {
     return(new ContainerSpan(
                color,
                new ContentSpan(@string),
                BackgroundColorSpan.Reset()));
 }
コード例 #8
0
        public void RenderToRegion(
            Span span,
            Region region)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            if (span == null)
            {
                span = Span.Empty();
            }
            else if (_resetAfterRender)
            {
                span = new ContainerSpan(
                    span,
                    ForegroundColorSpan.Reset(),
                    BackgroundColorSpan.Reset());
            }

            SpanVisitor visitor = null;

            if (_mode == OutputMode.Auto)
            {
                _mode = _terminal?.DetectOutputMode() ??
                        OutputMode.PlainText;
            }

            switch (_mode)
            {
            case OutputMode.NonAnsi:
                visitor = new NonAnsiRenderingSpanVisitor(
                    _terminal,
                    region);
                break;

            case OutputMode.Ansi:
                visitor = new AnsiRenderingSpanVisitor(
                    _console,
                    region);
                break;

            case OutputMode.PlainText:
                visitor = new FileRenderingSpanVisitor(
                    _console.Out,
                    new Region(region.Left,
                               region.Top,
                               region.Width,
                               region.Height,
                               false));
                break;

            default:
                throw new NotSupportedException();
            }

            visitor.Visit(span);
        }
コード例 #9
0
        public void BackgroundColorSpans_with_the_same_name_are_equal()
        {
            var one = new BackgroundColorSpan("green", Ansi.Color.Foreground.Green);
            var two = new BackgroundColorSpan("green", Ansi.Color.Foreground.Green);

            one.Equals(two).Should().BeTrue();

            one.Invoking(code => code.Equals(null)).Should().NotThrow <NullReferenceException>();
        }
コード例 #10
0
        public void BackgroundColorSpans_are_removed_during_file_rendering()
        {
            var span = _spanFormatter.ParseToSpan(
                $"{BackgroundColorSpan.Red()}red {BackgroundColorSpan.Blue()}blue {BackgroundColorSpan.Green()}green {BackgroundColorSpan.Reset()}or a {BackgroundColorSpan.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);
        }
コード例 #11
0
        public async ValueTask DestroyProgressBar()
        {
            await context.Factory.Run(() =>
            {
                ReportProgress(1.0);
                progress = null;

                WriteLine(new ContainerSpan(
                              CursorControlSpan.Show(),
                              ForegroundColorSpan.Reset(),
                              BackgroundColorSpan.Reset()));
            });
        }
コード例 #12
0
        public void BackgroundColorSpans_are_replaced_with_ANSI_codes_during_ANSI_rendering()
        {
            var span = _spanFormatter.ParseToSpan(
                $"{BackgroundColorSpan.Red()}red {BackgroundColorSpan.Blue()}blue {BackgroundColorSpan.Green()}green {BackgroundColorSpan.Reset()}or a {BackgroundColorSpan.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.Background.Red.EscapeSequence}red {Ansi.Color.Background.Blue.EscapeSequence}blue {Ansi.Color.Background.Green.EscapeSequence}green {Ansi.Color.Background.Default.EscapeSequence}or a {Ansi.Color.Background.Rgb(12, 34, 56).EscapeSequence}little of each.");
        }
コード例 #13
0
        public void RenderToRegion(
            Span span,
            Region region)
        {
            SpanVisitor visitor;

            if (span == null)
            {
                span = Span.Empty();
            }
            else if (_resetAfterRender)
            {
                span = new ContainerSpan(
                    span,
                    ForegroundColorSpan.Reset(),
                    BackgroundColorSpan.Reset());
            }

            switch (Mode)
            {
            case OutputMode.NonAnsi:
                visitor = new NonAnsiRenderingSpanVisitor(
                    _terminal,
                    region);
                break;

            case OutputMode.Ansi:
                visitor = new AnsiRenderingSpanVisitor(
                    _console,
                    region);
                break;

            case OutputMode.File:
                visitor = new FileRenderingSpanVisitor(
                    _console.Out,
                    new Region(region.Left,
                               region.Top,
                               region.Width,
                               region.Height,
                               false));
                break;

            default:
                throw new NotSupportedException();
            }

            visitor.Visit(span);
        }
コード例 #14
0
 public void FormatSpans_do_not_have_default_string_representations()
 {
     $"{ForegroundColorSpan.DarkGray()}The {BackgroundColorSpan.Cyan()}quick{StyleSpan.BlinkOn()} brown fox jumped over the lazy dog.{StyleSpan.BoldOff()}{ForegroundColorSpan.Reset()}{BackgroundColorSpan.Reset()}"
     .Should()
     .Be("The quick brown fox jumped over the lazy dog.");
 }
コード例 #15
0
 public virtual void VisitBackgroundColorSpan(BackgroundColorSpan span)
 {
 }
コード例 #16
0
 public override void VisitBackgroundColorSpan(BackgroundColorSpan span) => VisitedSpans.Add(span);
コード例 #17
0
 public static void OutputFatal(this ITerminal terminal, string content) => terminal.OutputColor(BackgroundColorSpan.Red(), content);
コード例 #18
0
 public static void OutputColor(this ITerminal terminal, BackgroundColorSpan color, string content)
 {
     terminal.Render(color);
     terminal.Output(content);
     terminal.Render(BackgroundColorSpan.Reset());
 }
コード例 #19
0
        private static void HandleFormatRun(ISpannable spannableString, IAttributedTextRun run)
        {
            var attributes = run.Attributes;
            var start      = run.Start;
            var end        = start + run.Length;

            var fontName = attributes.GetFontName();

            if (fontName != null)
            {
                var typefaceSpan = new TypefaceSpan(fontName);
                spannableString.SetSpan(typefaceSpan, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetBold() && attributes.GetItalic())
            {
                var span = new StyleSpan(TypefaceStyle.BoldItalic);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }
            else if (attributes.GetBold())
            {
                var span = new StyleSpan(TypefaceStyle.Bold);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }
            else if (attributes.GetItalic())
            {
                var span = new StyleSpan(TypefaceStyle.Italic);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetUnderline())
            {
                var span = new UnderlineSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            var foregroundColor = attributes.GetForegroundColor()?.ParseAsInts()?.ToColor();

            if (foregroundColor != null)
            {
                var span = new ForegroundColorSpan((global::Android.Graphics.Color)foregroundColor);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            var backgroundColor = attributes.GetBackgroundColor()?.ParseAsInts()?.ToColor();

            if (backgroundColor != null)
            {
                var span = new BackgroundColorSpan((global::Android.Graphics.Color)backgroundColor);
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetSubscript())
            {
                var span = new SubscriptSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetSuperscript())
            {
                var span = new SuperscriptSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetStrikethrough())
            {
                var span = new StrikethroughSpan();
                spannableString.SetSpan(span, start, end, SpanTypes.ExclusiveExclusive);
            }

            if (attributes.GetUnorderedList())
            {
                var bulletSpan = new BulletSpan();
                spannableString.SetSpan(bulletSpan, start, end, SpanTypes.ExclusiveExclusive);
            }
        }