public static IEnumerable <string> Capture(PSHost host, bool includeCurrentLine, TrailingSpaceBehavior behavior) { StringBuilder sb = new StringBuilder(); foreach (object obj in Capturer.StreamCapture(host, includeCurrentLine, behavior, false)) { if (ReferenceEquals(obj, null)) { yield return(sb.ToString()); sb.Clear(); } Span span = obj as Span; if (!ReferenceEquals(span, null)) { sb.Append(span.Content); } } }
public static string GetInteractiveHtml(PSHost host, bool includeCurrentLine, TrailingSpaceBehavior behavior) { return(string.Concat(GetInteractiveHtmlImpl( Capturer.StreamCapture(host, includeCurrentLine, behavior, false)))); }
/// <summary> /// Enumerates the substrings of HTML-formatted ConsoleLog. /// </summary> /// <param name="host">$Host from PowerShell.</param> /// <param name="includeCurrentLine">Whether the current line should be included. /// This can be set to $False if the command is used interactively.</param> /// <param name="behavior">Behavior of trailing space characters.</param> public static IEnumerable <string> StreamCapture(PSHost host, bool includeCurrentLine, TrailingSpaceBehavior behavior) { ConsoleColor fgDefault = ConsoleColor.White, bgDefault = ConsoleColor.Black; ConsoleColor fgLine = ConsoleColor.White, bgLine = ConsoleColor.Black; ConsoleColor fg = ConsoleColor.White, bg = ConsoleColor.Black; bool emptyLine = true; /* The loop directly walks the tree, so we do not need ** the tree to be stored in the returned objects. ** Setting "populate" to $False make it GC-efficient. */ foreach (object obj in Capturer.StreamCapture(host, includeCurrentLine, behavior, false)) { ConsoleLog console = obj as ConsoleLog; if (!ReferenceEquals(console, null)) { yield return("<pre class=\"gl-console gl-console-fg-"); yield return(Helper.ColorToName(fgDefault = console.Foreground)); yield return(" gl-console-bg-"); yield return(Helper.ColorToName(bgLine = bgDefault = console.Background)); yield return("\" data-width=\""); yield return(console.Width.ToString(CultureInfo.InvariantCulture)); yield return("\">\n"); continue; } Line line = obj as Line; if (!ReferenceEquals(line, null)) { yield return("<code class=\"gl-console-line"); if ((fgLine = line.Foreground) != fgDefault) { yield return(" gl-console-fg-"); yield return(Helper.ColorToName(fgLine)); } yield return(" gl-console-bg-"); ConsoleColor bgLine2 = line.Background; if (bgLine != bgLine2) { yield return("change gl-console-bg-"); } yield return((bgLine = bgLine2) != bgDefault ? Helper.ColorToName(bgLine2) : "none"); yield return("\">"); emptyLine = true; continue; } Span span = obj as Span; if (!ReferenceEquals(span, null)) { emptyLine = false; fg = span.Foreground; bg = span.Background; if (fg == fgLine && bg == bgLine) { yield return("<span>"); yield return(Helper.HtmlEncode(span.Content)); yield return("</span>"); } else if (fg == fgLine && bg != bgLine) { yield return("<span class=\"gl-console-bg-"); yield return(Helper.ColorToName(bg)); yield return("\">"); yield return(Helper.HtmlEncode(span.Content)); yield return("</span>"); } else if (fg != fgLine && bg == bgLine) { yield return("<span class=\"gl-console-fg-"); yield return(Helper.ColorToName(fg)); yield return("\">"); yield return(Helper.HtmlEncode(span.Content)); yield return("</span>"); } else { yield return("<span class=\"gl-console-fg-"); yield return(Helper.ColorToName(fg)); yield return(" gl-console-bg-"); yield return(Helper.ColorToName(bg)); yield return("\">"); yield return(Helper.HtmlEncode(span.Content)); yield return("</span>"); } continue; } /* A line has ended. */ yield return(emptyLine ? "<span> </span></code>\n" : "</code>\n"); } yield return("</pre>"); }