Exemplo n.º 1
0
        /// <summary>
        /// Prints a table of servers to console window
        /// using the provided collection of <see cref="Server"/>.
        /// </summary>
        /// <param name="servers"></param>
        public void Render(IEnumerable <Server> servers)
        {
            List <(string Name, int Count)> groupedServers = servers
                                                             .GroupBy(s => s.Name)
                                                             .Select(g => (g.Key, g.Count()))
                                                             .ToList();

            var grid = CreateGrid();

            grid = WithHeader(grid);
            grid = WithBody(grid, groupedServers);
            grid = WithFooter(grid, groupedServers.Sum(s => s.Count));
            var document = new Document(grid);

            if (!_renderToCommandLine)
            {
                return;
            }

            var renderRect = new Rect?(ConsoleRenderer.DefaultRenderRect);
            var buffer     = new ConsoleBuffer(renderRect.Value.Size.Width);

            ConsoleRenderer.RenderDocumentToBuffer(document, buffer, renderRect.Value);

            IRenderTarget target = new ConsoleRenderTarget();

            target.Render(buffer);

            /*
             * How to render to string:
             * TextRenderTargetBase target = new TextRenderTarget();
             * target.Render(buffer);
             * Output = target.OutputText;
             */
        }
        public void Render()
        {
            var buffer = new ConsoleBuffer(3);
            var target = new ConsoleRenderTarget();

            TextWriter oldOut = Console.Out;

            try {
                var stringOut = new StringWriter();
                Console.SetOut(stringOut);

                buffer.DrawString(0, 0, ConsoleColor.White, "Foo");
                target.Render(buffer);

                stringOut.ToString().Should().Be($"Foo{Environment.NewLine}");
            }
            finally {
                Console.SetOut(oldOut);
            }
        }