Пример #1
0
        public void Render (IConsoleBufferSource buffer)
        {
            if (buffer == null)
                throw new ArgumentNullException(nameof(buffer));
            ConsoleColor currentForeColor = Console.ForegroundColor, oldForeColor = currentForeColor;
            ConsoleColor currentBackColor = Console.BackgroundColor, oldBackColor = currentBackColor;
            bool isManualWrap = buffer.Width < Console.BufferWidth;
            try {
                for (int iy = 0; iy < buffer.Height; iy++) {
                    ConsoleChar[] charsLine = buffer.GetLine(iy);

                    for (int ix = 0; ix < buffer.Width; ix++) {
                        ConsoleChar chr = charsLine[ix];
                        ConsoleColor foreColor = ColorOverride ?? chr.ForegroundColor;
                        if (foreColor != currentForeColor)
                            Console.ForegroundColor = currentForeColor = foreColor;
                        ConsoleColor backColor = BackgroundOverride ?? chr.BackgroundColor;
                        if (backColor != currentBackColor)
                            Console.BackgroundColor = currentBackColor = backColor;
                        Console.Write(chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy));
                    }
                    if (isManualWrap)
                        Console.WriteLine();
                }
            }
            finally {
                Console.ForegroundColor = oldForeColor;
                Console.BackgroundColor = oldBackColor;
            }
        }
Пример #2
0
        protected override void RenderOverride (IConsoleBufferSource buffer)
        {
            ThrowIfDisposed();
            if (buffer == null)
                throw new ArgumentNullException(nameof(buffer));

            ConsoleColor currentForeColor = (ConsoleColor)int.MaxValue;
            ConsoleColor currentBackColor = (ConsoleColor)int.MaxValue;

            Writer.Write("\x1B[0m");

            for (int iy = 0; iy < buffer.Height; iy++) {
                ConsoleChar[] charsLine = buffer.GetLine(iy);

                for (int ix = 0; ix < buffer.Width; ix++) {
                    ConsoleChar chr = charsLine[ix];
                    ConsoleColor foreColor = ColorOverride ?? chr.ForegroundColor;
                    ConsoleColor backColor = BackgroundOverride ?? chr.BackgroundColor;
                    if (foreColor != currentForeColor || backColor != currentBackColor) {
                        Writer.Write("\x1B[");
                        Writer.Write(ColorMap[(int)foreColor]);
                        Writer.Write(";");
                        Writer.Write(BackgroundColorMap[(int)backColor]);
                        Writer.Write("m");
                        currentForeColor = foreColor;
                        currentBackColor = backColor;
                    }
                    Writer.Write(chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy));
                }
                Writer.WriteLine();
            }
        }
Пример #3
0
 public void Render (IConsoleBufferSource buffer)
 {
     ThrowIfDisposed();
     if (buffer == null)
         throw new ArgumentNullException(nameof(buffer));
     RenderOverride(buffer);
 }
 public void Render(IConsoleBufferSource buffer)
 {
     ThrowIfDisposed();
     if (buffer == null)
     {
         throw new ArgumentNullException(nameof(buffer));
     }
     RenderOverride(buffer);
 }
Пример #5
0
        protected override void RenderOverride(IConsoleBufferSource buffer)
        {
            ThrowIfDisposed();
            Debug.Assert(Writer != null);
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            ConsoleColor currentForeColor = (ConsoleColor)int.MaxValue;
            ConsoleColor currentBackColor = (ConsoleColor)int.MaxValue;

            if (!BodyOnly)
            {
                Writer.Write($@"<!DOCTYPE html>
                    <html>
                    <head>
                      <meta charset=""{Writer.Encoding.WebName}"">
                      <title>{WebUtility.HtmlEncode(PageTitle)}</title>
                      <style>
                        pre {{ font: {Font} }}
                      </style>
                    </head>
                    <body>
                    ".Replace("\n                    ", "\n"));
            }

            Writer.Write("<pre><span>");

            for (int iy = 0; iy < buffer.Height; iy++)
            {
                ConsoleChar[] charsLine = buffer.GetLine(iy);

                for (int ix = 0; ix < buffer.Width; ix++)
                {
                    ConsoleChar  chr       = charsLine[ix];
                    ConsoleColor foreColor = ColorOverride ?? chr.ForegroundColor;
                    ConsoleColor backColor = BackgroundOverride ?? chr.BackgroundColor;
                    if (foreColor != currentForeColor || backColor != currentBackColor)
                    {
                        Writer.Write($"</span><span style=\"color:#{ColorMap[(int)foreColor]};background:#{ColorMap[(int)backColor]}\">");
                        currentForeColor = foreColor;
                        currentBackColor = backColor;
                    }
                    char c = chr.HasChar || chr.LineChar.IsEmpty ? chr.PrintableChar : buffer.GetLineChar(ix, iy);
                    Writer.Write(WebUtility.HtmlEncode(c.ToString()));
                }

                Writer.Write("<br/>");
            }

            Writer.Write("</pre>");
            if (!BodyOnly)
            {
                Writer.Write("\n</body>\n</html>");
            }
        }
Пример #6
0
 public override void Render (IConsoleBufferSource buffer)
 {
     var document = new FlowDocument();
     RenderToFlowDocument(buffer, document);
     var content = new TextRange(document.ContentStart, document.ContentEnd);
     content.Save(_output, DataFormats.Rtf);
     if (_leaveOpen)
         _output.Flush();
     else
         _output.Dispose();
 }
Пример #7
0
        protected void RenderToFixedDocument(IConsoleBufferSource buffer, FixedDocument document)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            WpfSize   charSize   = CharSize;
            WpfCanvas linesPanel = AddDocumentPage(document, buffer, charSize);

            RenderToCanvas(buffer, linesPanel, charSize);
        }
Пример #8
0
        protected override void RenderOverride (IConsoleBufferSource buffer)
        {
            ThrowIfDisposed();
            if (buffer == null)
                throw new ArgumentNullException(nameof(buffer));

            for (int iy = 0; iy < buffer.Height; iy++) {
                ConsoleChar[] charsLine = buffer.GetLine(iy);
                for (int ix = 0; ix < buffer.Width; ix++) {
                    ConsoleChar chr = charsLine[ix];
                    Writer.Write(chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy));
                }
                Writer.WriteLine();
            }
        }
Пример #9
0
        protected static void RenderToCanvas([NotNull] IConsoleBufferSource buffer, WpfCanvas linesPanel, WpfSize charSize)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            ConsoleColor currentForeColor = (ConsoleColor)int.MaxValue;
            ConsoleColor currentBackColor = (ConsoleColor)int.MaxValue;
            TextBlock    text             = null;

            for (int iy = 0; iy < buffer.Height; iy++)
            {
                ConsoleChar[] charsLine = buffer.GetLine(iy);
                for (int ix = 0; ix < buffer.Width; ix++)
                {
                    ConsoleChar  chr       = charsLine[ix];
                    ConsoleColor foreColor = chr.ForegroundColor;
                    ConsoleColor backColor = chr.BackgroundColor;
                    if (text == null || foreColor != currentForeColor || backColor != currentBackColor)
                    {
                        currentForeColor = foreColor;
                        currentBackColor = backColor;
                        AppendTextBlockIfNeeded();
                        text = new TextBlock {
                            Foreground = ConsoleBrushes[foreColor],
                            Background = ConsoleBrushes[backColor],
                            Height     = charSize.Height,
                        };
                        WpfCanvas.SetLeft(text, ix * charSize.Width);
                        WpfCanvas.SetTop(text, iy * charSize.Height);
                    }
                    text.Text += chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy);
                }
                AppendTextBlockIfNeeded();
            }
            AppendTextBlockIfNeeded();

            void AppendTextBlockIfNeeded()
            {
                if (text == null)
                {
                    return;
                }
                text.Width = text.Text.Length * charSize.Width;
                linesPanel.Children.Add(text);
                text = null;
            }
        }
Пример #10
0
        public override void Render(IConsoleBufferSource buffer)
        {
            IDocumentPaginatorSource document;

            if (DocumentType == PresentationDocumentType.FlowDocument)
            {
                document = new FlowDocument();
                RenderToFlowDocument(buffer, (FlowDocument)document);
            }
            else
            {
                document = new FixedDocument();
                RenderToFixedDocument(buffer, (FixedDocument)document);
            }
            SaveDocument(document);
        }
        public void Render(IConsoleBufferSource buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            ConsoleColor currentForeColor = Console.ForegroundColor, oldForeColor = currentForeColor;
            ConsoleColor currentBackColor = Console.BackgroundColor, oldBackColor = currentBackColor;
            bool         isManualWrap;

            try {
                isManualWrap = buffer.Width < Console.BufferWidth;
            }
            catch (IOException) {
                isManualWrap = true;
            }
            try {
                for (int iy = 0; iy < buffer.Height; iy++)
                {
                    ConsoleChar[] charsLine = buffer.GetLine(iy);

                    for (int ix = 0; ix < buffer.Width; ix++)
                    {
                        ConsoleChar  chr       = charsLine[ix];
                        ConsoleColor foreColor = ColorOverride ?? chr.ForegroundColor;
                        if (foreColor != currentForeColor)
                        {
                            Console.ForegroundColor = currentForeColor = foreColor;
                        }
                        ConsoleColor backColor = BackgroundOverride ?? chr.BackgroundColor;
                        if (backColor != currentBackColor)
                        {
                            Console.BackgroundColor = currentBackColor = backColor;
                        }
                        Console.Write(chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy));
                    }
                    if (isManualWrap)
                    {
                        Console.WriteLine();
                    }
                }
            }
            finally {
                Console.ForegroundColor = oldForeColor;
                Console.BackgroundColor = oldBackColor;
            }
        }
Пример #12
0
        public override void Render(IConsoleBufferSource buffer)
        {
            var document = new FlowDocument();

            RenderToFlowDocument(buffer, document);
            var content = new TextRange(document.ContentStart, document.ContentEnd);

            content.Save(_output, DataFormats.Rtf);
            if (_leaveOpen)
            {
                _output.Flush();
            }
            else
            {
                _output.Dispose();
            }
        }
Пример #13
0
        protected override void RenderOverride (IConsoleBufferSource buffer)
        {
            ThrowIfDisposed();
            if (buffer == null)
                throw new ArgumentNullException(nameof(buffer));

            ConsoleColor currentForeColor = (ConsoleColor)int.MaxValue;
            ConsoleColor currentBackColor = (ConsoleColor)int.MaxValue;

            if (!BodyOnly) {
                Writer.Write("<!DOCTYPE html>\n<html>\n<head>\n  <meta charset=\"");
                Writer.Write(Writer.Encoding.WebName);
                Writer.Write("\">\n  <title>");
                Writer.Write(WebUtility.HtmlEncode(PageTitle));
                Writer.Write("</title>\n  <style>\n    pre { font: ");
                Writer.Write(Font);
                Writer.Write(" }\n  </style>\n</head>\n<body>\n");
            }

            Writer.Write("<pre><span>");

            for (int iy = 0; iy < buffer.Height; iy++) {
                ConsoleChar[] charsLine = buffer.GetLine(iy);

                for (int ix = 0; ix < buffer.Width; ix++) {
                    ConsoleChar chr = charsLine[ix];
                    ConsoleColor foreColor = ColorOverride ?? chr.ForegroundColor;
                    ConsoleColor backColor = BackgroundOverride ?? chr.BackgroundColor;
                    if (foreColor != currentForeColor || backColor != currentBackColor) {
                        Writer.Write($"</span><span style=\"color:#{ColorMap[(int)foreColor]};background:#{ColorMap[(int)backColor]}\">");
                        currentForeColor = foreColor;
                        currentBackColor = backColor;
                    }
                    char c = chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy);
                    Writer.Write(WebUtility.HtmlEncode(c.ToString()));
                }

                Writer.Write("<br/>");
            }

            Writer.Write("</pre>");
            if (!BodyOnly)
                Writer.Write("\n</body>\n</html>");
        }
Пример #14
0
        protected WpfCanvas CreateLinePanel([NotNull] IConsoleBufferSource buffer, WpfSize charSize)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            var linesPanel = new WpfCanvas {
                Width      = buffer.Width * charSize.Width,
                Height     = buffer.Height * charSize.Height,
                Background = Background,
            };

            TextBlock.SetFontFamily(linesPanel, FontFamily);
            TextBlock.SetFontSize(linesPanel, FontSize);
            TextBlock.SetFontStretch(linesPanel, FontStretch);
            TextBlock.SetFontStyle(linesPanel, FontStyle);
            TextBlock.SetFontWeight(linesPanel, FontWeight);
            return(linesPanel);
        }
Пример #15
0
        protected override void RenderOverride(IConsoleBufferSource buffer)
        {
            ThrowIfDisposed();
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            for (int iy = 0; iy < buffer.Height; iy++)
            {
                ConsoleChar[] charsLine = buffer.GetLine(iy);
                for (int ix = 0; ix < buffer.Width; ix++)
                {
                    ConsoleChar chr = charsLine[ix];
                    Writer.Write(chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy));
                }
                Writer.WriteLine();
            }
        }
Пример #16
0
        protected override void RenderOverride(IConsoleBufferSource buffer)
        {
            ThrowIfDisposed();
            Debug.Assert(Writer != null);
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            ConsoleColor currentForeColor = (ConsoleColor)int.MaxValue;
            ConsoleColor currentBackColor = (ConsoleColor)int.MaxValue;

            Writer.Write("\x1B[0m");

            for (int iy = 0; iy < buffer.Height; iy++)
            {
                ConsoleChar[] charsLine = buffer.GetLine(iy);

                for (int ix = 0; ix < buffer.Width; ix++)
                {
                    ConsoleChar  chr       = charsLine[ix];
                    ConsoleColor foreColor = ColorOverride ?? chr.ForegroundColor;
                    ConsoleColor backColor = BackgroundOverride ?? chr.BackgroundColor;
                    if (foreColor != currentForeColor || backColor != currentBackColor)
                    {
                        Writer.Write("\x1B[");
                        Writer.Write(ColorMap[(int)foreColor]);
                        Writer.Write(";");
                        Writer.Write(BackgroundColorMap[(int)backColor]);
                        Writer.Write("m");
                        currentForeColor = foreColor;
                        currentBackColor = backColor;
                    }
                    Writer.Write(chr.HasChar || chr.LineChar.IsEmpty ? chr.PrintableChar : buffer.GetLineChar(ix, iy));
                }
                Writer.WriteLine();
            }
        }
Пример #17
0
        protected WpfCanvas AddDocumentPage([NotNull] FixedDocument document, [NotNull] IConsoleBufferSource buffer, WpfSize charSize)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            WpfCanvas linesPanel = CreateLinePanel(buffer, charSize);

            document.Pages.Add(
                new PageContent {
                Child = new FixedPage {
                    Width      = buffer.Width * charSize.Width,
                    Height     = buffer.Height * charSize.Height,
                    Background = Background,
                    Children   = { linesPanel },
                }
            });
            return(linesPanel);
        }
Пример #18
0
        protected void RenderToFlowDocument(IConsoleBufferSource buffer, [NotNull] FlowDocument document)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var par = new Paragraph {
                Background    = Background,
                FontFamily    = FontFamily,
                FontSize      = FontSize,
                FontStretch   = FontStretch,
                FontStyle     = FontStyle,
                FontWeight    = FontWeight,
                TextAlignment = System.Windows.TextAlignment.Left,
            };

            document.Blocks.Add(par);

            ConsoleColor currentForeColor = (ConsoleColor)int.MaxValue;
            ConsoleColor currentBackColor = (ConsoleColor)int.MaxValue;
            Run          text             = null;

            for (int iy = 0; iy < buffer.Height; iy++)
            {
                ConsoleChar[] charsLine = buffer.GetLine(iy);
                for (int ix = 0; ix < buffer.Width; ix++)
                {
                    ConsoleChar  chr       = charsLine[ix];
                    ConsoleColor foreColor = chr.ForegroundColor;
                    ConsoleColor backColor = chr.BackgroundColor;
                    if (text == null || foreColor != currentForeColor || backColor != currentBackColor)
                    {
                        currentForeColor = foreColor;
                        currentBackColor = backColor;
                        AppendRunIfNeeded();
                        text = new Run {
                            Foreground = ConsoleBrushes[foreColor],
                            Background = ConsoleBrushes[backColor],
                        };
                    }
                    text.Text += chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy);
                }
                AppendRunIfNeeded();
                if (iy + 1 < buffer.Height)
                {
                    par.Inlines.Add(new LineBreak());
                }
            }
            AppendRunIfNeeded();

            void AppendRunIfNeeded()
            {
                if (text == null)
                {
                    return;
                }
                par.Inlines.Add(text);
                text = null;
            }
        }
Пример #19
0
 public abstract void Render(IConsoleBufferSource buffer);
 protected abstract void RenderOverride([NotNull] IConsoleBufferSource buffer);
 protected override void RenderOverride(IConsoleBufferSource buffer)
 {
     ThrowIfDisposed();
     Debug.Assert(Writer != null);
     Writer.Write("Foo");
 }
 public override void Render(IConsoleBufferSource buffer)
 {
     RenderToFlowDocument(buffer, Document);
 }
 protected override void RenderOverride(IConsoleBufferSource buffer)
 {
     Writer.Write("Foo");
 }
 public override void Render (IConsoleBufferSource buffer)
 {
     RenderToFlowDocument(buffer, Document);
 }
Пример #25
0
 public override void Render (IConsoleBufferSource buffer)
 {
     WpfSize charSize = CharSize;
     Canvas = CreateLinePanel(buffer, charSize);
     RenderToCanvas(buffer, Canvas, charSize);
 }