Esempio n. 1
0
 public static ContainerSpan StyleNumber(this string @string)
 {
     return(new ContainerSpan(
                ForegroundColorSpan.Rgb(175, 215, 175),
                new ContentSpan(@string),
                ForegroundColorSpan.Reset()));
 }
Esempio n. 2
0
 public static ContainerSpan StyleRgb(this string @string, byte r, byte g, byte b)
 {
     return(new ContainerSpan(
                ForegroundColorSpan.Rgb(r, g, b),
                new ContentSpan(@string),
                ForegroundColorSpan.Reset()));
 }
Esempio n. 3
0
 public static ContainerSpan StyleValue(this string @string)
 {
     return(new ContainerSpan(
                ForegroundColorSpan.Rgb(255, 175, 135),
                new ContentSpan(@string),
                ForegroundColorSpan.Reset()));
 }
Esempio n. 4
0
        public override void Render(ConsoleRenderer renderer, Region region)
        {
            byte r = 0;
            byte g = 0;
            byte b = 0;

            var i = 0;

            for (var x = 0; x < region.Width; x++)
            {
                for (var y = 0; y < region.Height; y++)
                {
                    if (i >= Text.Length - 1)
                    {
                        i = 0;
                    }
                    else
                    {
                        i++;
                    }

                    var subregion = new Region(region.Left + x, region.Top + y, 1, 1);
                    unchecked
                    {
                        renderer.RenderToRegion(
                            $"{ForegroundColorSpan.Rgb(r += 2, g += 3, b += 5)}{Text[i]}{ForegroundColorSpan.Reset()}",
                            subregion
                            );
                    }
                }
            }
        }
Esempio n. 5
0
        public static ContainerSpan StyleGrade(this string @string, double performance, Interval?range)
        {
            var y = (range ?? Interval.Unit).UnitNormalize(performance);
            // poor man's color scale, red - yellow - green
            var hsv = new Hsv(h: (float)(y * 120), s: 1, v: 1);
            var rgb = (Rgb24)colorConverter.ToRgb(hsv);

            return(new ContainerSpan(
                       ForegroundColorSpan.Rgb(rgb.R, rgb.G, rgb.B),
                       new ContentSpan(@string),
                       ForegroundColorSpan.Reset()));
        }
Esempio n. 6
0
        public ProcessesView(Process[] processes)
        {
            var formatter = new TextSpanFormatter();

            formatter.AddFormatter <TimeSpan>(t => new ContentSpan(t.ToString(@"hh\:mm\:ss")));

            Add(new ContentView(""));
            Add(new ContentView("Processes"));
            Add(new ContentView(""));

            var table = new TableView <Process> {
                Items = processes
            };

            table.AddColumn(p => p.Id, new ContentView("PID".Underline()));
            table.AddColumn(p => Name(p), new ContentView("COMMAND".Underline()));
            table.AddColumn(p => p.PrivilegedProcessorTime, new ContentView("TIME".Underline()));
            table.AddColumn(p => p.Threads.Count, new ContentView("#TH".Underline()));
            table.AddColumn(
                p => p.PrivateMemorySize64.Abbreviate(),
                new ContentView("MEM".Underline())
                );
            table.AddColumn(
                p =>
            {
#pragma warning disable CS0618 // Type or member is obsolete
                var usage = p.TrackCpuUsage().First();
#pragma warning restore CS0618 // Type or member is obsolete
                return($"{usage.UsageTotal:P}");
            },
                new ContentView("CPU".Underline())
                );

            Add(table);

            FormattableString Name(Process p)
            {
                if (!p.Responding)
                {
                    return($"{ForegroundColorSpan.Rgb(180, 0, 0)}{p.ProcessName}{ForegroundColorSpan.Reset()}");
                }
                return($"{p.ProcessName}");
            }
        }
        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 void Same_input_with_or_without_ansi_codes_produce_the_same_wrap(
            int left,
            int top,
            int width,
            int height
            )
        {
            var terminalWithoutAnsiCodes = new TestTerminal();
            var rendererWithoutAnsiCodes = new ConsoleRenderer(
                terminalWithoutAnsiCodes,
                OutputMode.NonAnsi
                );

            var terminalWithAnsiCodes = new TestTerminal();
            var rendererWithAnsiCodes = new ConsoleRenderer(
                terminalWithAnsiCodes,
                OutputMode.NonAnsi
                );

            FormattableString formattableString =
                $"Call me {StyleSpan.BoldOn()}{StyleSpan.UnderlinedOn()}Ishmael{StyleSpan.UnderlinedOff()}{StyleSpan.BoldOff()}. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and {ForegroundColorSpan.Rgb(60, 0, 0)}methodically{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(90, 0, 0)}knocking{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(120, 0, 0)}people's{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(160, 0, 0)}hats{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(220, 0, 0)}off{ForegroundColorSpan.Reset()} then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.";

            var stringWithoutCodes = formattableString.ToString();

            var region = new Region(left, top, width, height);

            rendererWithAnsiCodes.RenderToRegion(formattableString, region);

            rendererWithoutAnsiCodes.RenderToRegion(stringWithoutCodes, region);

            var outputFromInputWithoutAnsiCodes = terminalWithoutAnsiCodes.Out.ToString();
            var outputFromInputWithAnsiCodes    = terminalWithAnsiCodes.Out.ToString();

            outputFromInputWithAnsiCodes.Should().Be(outputFromInputWithoutAnsiCodes);
        }
        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 ForegroundColorSpans_are_replaced_with_ANSI_codes_during_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.Ansi);

            renderer.RenderToRegion(span, new Region(0, 0, 200, 1, false));

            _terminal.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 ForegroundColorSpans_are_removed_during_file_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.PlainText);

            var expected = "red blue green or a little of each.";

            renderer.RenderToRegion(span, new Region(0, 0, expected.Length, 1, false));

            _terminal.Out.ToString().Should().Be(expected);
        }
Esempio n. 12
0
 public static TextSpan DarkGrey(this string value) =>
 new ContainerSpan(ForegroundColorSpan.Rgb(100, 100, 100),
                   new ContentSpan(value),
                   ForegroundColorSpan.Reset());
Esempio n. 13
0
 public static TextSpan UnderlineRed(this string value) =>
 new ContainerSpan(StyleSpan.UnderlinedOn(),
                   ForegroundColorSpan.Rgb(237, 90, 90),
                   new ContentSpan(value),
                   ForegroundColorSpan.Reset(),
                   StyleSpan.UnderlinedOff());
Esempio n. 14
0
 public static TextSpan UnderlineRgb(this string value, byte r, byte g, byte b) =>
 new ContainerSpan(StyleSpan.UnderlinedOn(),
                   ForegroundColorSpan.Rgb(r, g, b),
                   new ContentSpan(value),
                   ForegroundColorSpan.Reset(),
                   StyleSpan.UnderlinedOff());
Esempio n. 15
0
 public static Span Rgb(this string value, byte r, byte g, byte b) =>
 new ContainerSpan(ForegroundColorSpan.Rgb(r, g, b),
                   new ContentSpan(value),
                   ForegroundColorSpan.Reset());
Esempio n. 16
0
        /// <summary>
        /// Demonstrates various rendering capabilities.
        /// </summary>
        /// <param name="invocationContext"></param>
        /// <param name="sample">Renders a specified sample</param>
        /// <param name="height">The height of the rendering area</param>
        /// <param name="width">The width of the rendering area</param>
        /// <param name="top">The top position of the render area</param>
        /// <param name="left">The left position of the render area</param>
        /// <param name="text">The text to render</param>
        /// <param name="overwrite">Overwrite the specified region. (If not, scroll.)</param>
        public static void Main(
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
            InvocationContext invocationContext,
            SampleName sample = SampleName.Dir,
            int?height        = null,
            int?width         = null,
            int top           = 0,
            int left          = 0,
            string text       = null,
            bool overwrite    = true)
#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
        {
            var region = new Region(left,
                                    top,
                                    width ?? Console.WindowWidth,
                                    height ?? Console.WindowHeight,
                                    overwrite);

            if (overwrite &&
                invocationContext.Console is ITerminal terminal)
            {
                terminal.Clear();
            }

            var consoleRenderer = new ConsoleRenderer(
                invocationContext.Console,
                mode: invocationContext.BindingContext.OutputMode(),
                resetAfterRender: true);

            switch (sample)
            {
            case SampleName.Colors:
            {
                var screen = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
                screen.Child = new ColorsView(text ?? "*");

                screen.Render(region);
            }
            break;

            case SampleName.Dir:
                var directoryTableView = new DirectoryTableView(new DirectoryInfo(Directory.GetCurrentDirectory()));
                directoryTableView.Render(consoleRenderer, region);

                break;

            case SampleName.Moby:
                consoleRenderer.RenderToRegion(
                    $"Call me {StyleSpan.BoldOn()}{StyleSpan.UnderlinedOn()}Ishmael{StyleSpan.UnderlinedOff()}{StyleSpan.BoldOff()}. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and {ForegroundColorSpan.Rgb(60, 0, 0)}methodically{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(90, 0, 0)}knocking{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(120, 0, 0)}people's{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(160, 0, 0)}hats{ForegroundColorSpan.Reset()} {ForegroundColorSpan.Rgb(220, 0, 0)}off{ForegroundColorSpan.Reset()} then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.",
                    region);
                break;

            case SampleName.Processes:
            {
                var view = new ProcessesView(Process.GetProcesses());
                view.Render(consoleRenderer, region);
            }

            break;

            case SampleName.TableView:
            {
                var table = new TableView <Process>
                {
                    Items = Process.GetProcesses().Where(x => !string.IsNullOrEmpty(x.MainWindowTitle)).OrderBy(p => p.ProcessName).ToList()
                };
                table.AddColumn(process => $"{process.ProcessName} ", "Name");
                table.AddColumn(process => ContentView.FromObservable(process.TrackCpuUsage(), x => $"{x.UsageTotal:P}"), "CPU", ColumnDefinition.Star(1));

                var screen = new ScreenView(renderer: consoleRenderer, invocationContext.Console)
                {
                    Child = table
                };
                screen.Render();
            }
            break;

            case SampleName.Clock:
            {
                var screen          = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
                var lastTime        = DateTime.Now;
                var clockObservable = new BehaviorSubject <DateTime>(lastTime);
                var clockView       = ContentView.FromObservable(clockObservable, x => $"{x:T}");
                screen.Child = clockView;
                screen.Render();

                while (!Console.KeyAvailable)
                {
                    if (DateTime.Now - lastTime > TimeSpan.FromSeconds(1))
                    {
                        lastTime = DateTime.Now;
                        clockObservable.OnNext(lastTime);
                    }
                }
            }
            break;

            case SampleName.GridLayout:
            {
                var screen  = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
                var content = new ContentView(
                    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum for Kevin.");
                var smallContent = new ContentView("Kevin Bost");
                var longContent  = new ContentView("Hacking on System.CommandLine");

                var gridView = new GridView();
                gridView.SetColumns(
                    ColumnDefinition.SizeToContent(),
                    ColumnDefinition.Star(1),
                    ColumnDefinition.Star(0.5)
                    );
                gridView.SetRows(
                    RowDefinition.Star(0.5),
                    RowDefinition.Star(0.5)
                    );

                gridView.SetChild(smallContent, 0, 0);
                gridView.SetChild(longContent, 0, 1);
                //gridView.SetChild(content, 0, 0);
                gridView.SetChild(content, 1, 1);
                gridView.SetChild(content, 2, 0);

                screen.Child = gridView;

                screen.Render();
            }
            break;

            default:
                if (!string.IsNullOrWhiteSpace(text))
                {
                    consoleRenderer.RenderToRegion(
                        text,
                        region);
                }
                else
                {
                    var screen      = new ScreenView(renderer: consoleRenderer, invocationContext.Console);
                    var stackLayout = new StackLayoutView();
                    var content1    = new ContentView("Hello World!");
                    var content2    = new ContentView(
                        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum for Kevin.");
                    stackLayout.Add(content2);
                    stackLayout.Add(content1);
                    stackLayout.Add(content2);
                    screen.Child = stackLayout;
                    screen.Render(new Region(0, 0, 50, Size.MaxValue));
                    //screen.Render(writer);
                }

                break;
            }

            if (!Console.IsOutputRedirected)
            {
                Console.ReadKey();
            }
        }
Esempio n. 17
0
 public static TextSpan DarkOrange(this string value) =>
 new ContainerSpan(ForegroundColorSpan.Rgb(128, 64, 0),
                   new ContentSpan(value),
                   ForegroundColorSpan.Reset());