/// <summary> /// Render a window frame /// </summary> /// <param name="windowFrame"></param> /// <param name="output"></param> public static void Render(WindowFrame windowFrame, TextWriter output) { var width = Console.WindowWidth; var height = Console.WindowHeight; var row = new string(' ', width); var lastRow = new string(' ', width - 1); var originalBackColor = Console.BackgroundColor; ColorTracker.SetBackColor(windowFrame.Color); Console.SetCursorPosition(0, 0); output.Write(row); Console.SetCursorPosition(0, height - 1); output.Write(lastRow); // write vertical bars for (var y = 1; y < height - 1; y++) { Console.SetCursorPosition(0, y); output.Write(' '); Console.SetCursorPosition(width - 1, y); output.Write(' '); } Console.BackgroundColor = originalBackColor; }
private void RenderHelpWindow(TextWriter stdout) { var width = Console.WindowWidth; var height = Console.WindowHeight; var cursorLeft = Console.CursorLeft; var cursorTop = Console.CursorTop; var originalForeground = Console.ForegroundColor; var originalBackground = Console.BackgroundColor; var longestEntry = Configuration.HelpScreen.HelpEntries.OrderByDescending(x => x.Key.Length + x.Description.Length).FirstOrDefault(); var helpWidth = longestEntry.Key.Length + longestEntry.Description.Length + 4; var helpHeight = Configuration.HelpScreen.HelpEntries.Count; var helpStartX = width / 2 - helpWidth / 2; var helpStartY = height / 2 - helpHeight / 2; var i = 0; Console.SetCursorPosition(helpStartX, helpStartY); var foreColor = Configuration.HelpScreen.ForegroundColor ?? Configuration.ColorPalette.Get(Configuration.HelpScreen.ForegroundColorPalette) ?? Style._foreground; var backColor = Configuration.HelpScreen.BackgroundColor ?? Configuration.ColorPalette.Get(Configuration.HelpScreen.BackgroundColorPalette) ?? Style._background; var shadowColor = System.Drawing.Color.FromArgb(backColor.A, (int)Math.Max(backColor.R * 0.5, 0), (int)Math.Max(backColor.G * 0.5, 0), (int)Math.Max(backColor.B * 0.5, 0)); // if we don't have space for the shadow, just draw the background color if (ColorTracker.Count >= ColorPalette.MaxColors) { shadowColor = backColor; } ColorTracker.SetForeColor(backColor); Console.BackgroundColor = originalBackground; stdout.Write(new string('▄', helpWidth + 2)); // draw a smaller top margin ColorTracker.SetForeColor(foreColor); i++; foreach (var entry in Configuration.HelpScreen.HelpEntries) { ColorTracker.SetBackColor(backColor); Console.SetCursorPosition(helpStartX, helpStartY + i); var content = $"{entry.Key}: {entry.Description}"; var spaces = helpWidth - content.Length; stdout.Write($" {content}"); stdout.Write(new string(' ', spaces)); if (Configuration.HelpScreen.EnableDropShadow) { // draw shadow ColorTracker.SetBackColor(shadowColor); stdout.Write(" "); } i++; } Console.SetCursorPosition(helpStartX, helpStartY + i); ColorTracker.SetBackColor(backColor); stdout.Write(new string(' ', helpWidth + 2)); if (Configuration.HelpScreen.EnableDropShadow) { // draw shadow ColorTracker.SetForeColor(shadowColor); ColorTracker.SetBackColor(shadowColor); stdout.Write(" "); Console.BackgroundColor = originalBackground; Console.SetCursorPosition(helpStartX + 1, helpStartY + i + 1); stdout.Write(new string('▀', helpWidth + 2)); // draw a smaller bottom margin/shadow } Console.ForegroundColor = originalForeground; Console.BackgroundColor = originalBackground; Console.SetCursorPosition(cursorLeft, cursorTop); }
private void DrawScreen(StringBuilder infoBuilder, ICollection <ConsoleLogEntry> log, bool logHasUpdates) { ColorTracker.Clear(); // we will write any screen contents to stderr, as stdout is redirected internally var forceStaticRowDraw = false; var stdout = Console.Error; var cursorLeft = Console.CursorLeft; var cursorTop = Console.CursorTop; var width = Console.WindowWidth; Console.SetCursorPosition(0, 0); if (_clearRequested) { _clearRequested = false; logHasUpdates = true; // force the static headers to redraw forceStaticRowDraw = true; Console.Clear(); } // write the static row headers if (Configuration.StaticRows?.Any() == true) { foreach (var staticRow in Configuration.StaticRows) { infoBuilder.Clear(); if (_staticRowContentBuilder.ContainsKey(staticRow.Name)) { var content = _staticRowContentBuilder[staticRow.Name]; var rowText = _staticRowRenderer .Build(staticRow, content) .Write(stdout, forceStaticRowDraw); } else { // render a blank row _staticRowRenderer .Build(staticRow) .Write(stdout, forceStaticRowDraw); } } } if (logHasUpdates) { // clear any direct output that needs to be removed ClearDirectOutput(stdout); // draw any direct output buffers added _historyLock.Wait(); try { _directOutputEntries.RemoveAll(x => x.IsDisplayed && x.DirectOutputMode == DirectOutputMode.ClearOnChange); } finally { _historyLock.Release(); } // restore cursor var xpos = GetXPosition(Configuration.LogHistoryContainer, 0); var ypos = GetYPosition(Configuration.LogHistoryContainer, 0); // write the screen log buffer Console.SetCursorPosition(xpos, ypos); var i = 0; var linesToFade = Options.RenderOptions.HasFlag(RenderOptions.FadeHistory) ? 6 : 0; var className = string.Empty; var rowPrefix = string.Empty; var hideClassNamePrefix = Options.RenderOptions.HasFlag(RenderOptions.HideClassNamePrefix); var hideLogRowPrefix = Options.RenderOptions.HasFlag(RenderOptions.HideLogRowPrefix); var logBackgroundColor = Configuration.LogHistoryContainer.BackgroundColor ?? Configuration.ColorPalette.Get(Configuration.LogHistoryContainer.BackgroundColorPalette) ?? Style._background; ColorTracker.SetBackColor(logBackgroundColor); _disableLogProcessing = false; foreach (var logLine in log) { className = string.Empty; rowPrefix = string.Empty; // fade the long lines away var logForegroundColor = Configuration.LogHistoryContainer.ForegroundColor ?? Configuration.ColorPalette.Get(Configuration.LogHistoryContainer.ForegroundColorPalette) ?? Style._foreground; var classNameForegroundColor = Configuration.LogHistoryContainer.PrependColor ?? Configuration.ColorPalette.Get(Configuration.LogHistoryContainer.PrependColorPalette) ?? Style._className; if (logLine.OriginalLine.Contains("|WARN|")) { logForegroundColor = Style._warningText; } if (logLine.OriginalLine.Contains("|ERROR|")) { logForegroundColor = Style._errorText; } if (i < linesToFade) { var r = Math.Max(logForegroundColor.R - ((linesToFade - i) * 25), 0); var g = Math.Max(logForegroundColor.G - ((linesToFade - i) * 25), 0); var b = Math.Max(logForegroundColor.B - ((linesToFade - i) * 25), 0); logForegroundColor = System.Drawing.Color.FromArgb(255, r, g, b); var cr = Math.Max(classNameForegroundColor.R - ((linesToFade - i) * 25), 0); var cg = Math.Max(classNameForegroundColor.G - ((linesToFade - i) * 25), 0); var cb = Math.Max(classNameForegroundColor.B - ((linesToFade - i) * 25), 0); classNameForegroundColor = System.Drawing.Color.FromArgb(255, cr, cg, cb); } if (!string.IsNullOrEmpty(logLine.Prepend)) { ColorTracker.SetForeColor(classNameForegroundColor); if (!hideClassNamePrefix) { className = $"{logLine.ClassName}"; stdout.Write(className); } if (!hideLogRowPrefix) { rowPrefix = logLine.Prepend; stdout.Write(rowPrefix); } } ColorTracker.SetForeColor(logForegroundColor); var truncatedLine = logLine.GetTruncatedLine(Console.BufferWidth, rowPrefix.Length); // fixes an issue when the buffer width and window width are not the same var windowSpacing = Console.BufferWidth - Console.WindowWidth; var spaces = (Console.WindowWidth - className.Length - rowPrefix.Length - truncatedLine.Length) + windowSpacing; if (spaces < 0) { spaces = 0; } stdout.Write(truncatedLine + new string(' ', spaces)); i++; } } if (_isHelpEnabled) { RenderHelpWindow(stdout); } RenderDirectOutput(stdout); if (Configuration.WindowFrame.Size > 0) { WindowFrameRenderer.Render(Configuration.WindowFrame, stdout); } _frameDrawnComplete.Set(); }
public string Write(TextWriter output, bool forceDraw = false) { var cursorLeft = Console.CursorLeft; var cursorTop = Console.CursorTop; var width = Console.WindowWidth; var height = Console.WindowHeight; var h = Console.BufferHeight; var lh = Console.LargestWindowHeight; var builder = new StringBuilder(); var xPosition = 0; var yPosition = 0; var originalForeColor = Console.ForegroundColor; var originalBackColor = Console.BackgroundColor; if (_content?.Any() == true) { var contentHasUpdates = _renderer.ComponentRenderer.HasUpdates(_content) || forceDraw; if (contentHasUpdates) { // render the background for the row var foreColor = _row.ForegroundColor ?? _renderer.Config.ColorPalette.Get(_row.ForegroundColorPalette) ?? originalForeColor; var backColor = _row.BackgroundColor ?? _renderer.Config.ColorPalette.Get(_row.BackgroundColorPalette) ?? originalBackColor; ColorTracker.SetForeColor(foreColor); ColorTracker.SetBackColor(backColor); var y = GetYPosition(_row.Location, _row.Index, yPosition); Console.SetCursorPosition(0, y); if (y == height - 1) { width -= 1; } builder.Append(new string(' ', width)); output.Write(builder.ToString()); // reset position Console.SetCursorPosition(cursorLeft, cursorTop); builder.Clear(); foreach (var item in _content) { var itemColor = item.ForegroundColor ?? _renderer.Config.ColorPalette.Get(item.ForegroundColorPalette) ?? _row.ForegroundColor ?? _renderer.Config.ColorPalette.Get(_row.ForegroundColorPalette) ?? originalForeColor; var itemBackColor = item.BackgroundColor ?? _renderer.Config.ColorPalette.Get(item.BackgroundColorPalette) ?? _row.BackgroundColor ?? _renderer.Config.ColorPalette.Get(_row.BackgroundColorPalette) ?? originalBackColor; ColorTracker.SetForeColor(itemColor); ColorTracker.SetBackColor(itemBackColor); // render the component string renderedContent = null; if (item.ContentType == RowContent.ContentTypes.Static) { renderedContent = item.StaticContent; } else if (item.ContentType == RowContent.ContentTypes.Component) { renderedContent = $"{item.Label}{_renderer.ComponentRenderer.Render(item.Component, item.ComponentName, item.ComponentParameter)}"; } if (item.Location == ColumnLocation.Right) { renderedContent = new string(' ', _renderer.ConsoleOptions.TextSpacing) + renderedContent; } else { renderedContent = renderedContent + new string(' ', _renderer.ConsoleOptions.TextSpacing); } builder.Append(renderedContent); y = GetYPosition(_row.Location, _row.Index, yPosition); var x = GetXPosition(item.Location, y, renderedContent.Length, _leftMargin, _rightMargin); Console.SetCursorPosition(x, y); output.Write(renderedContent); xPosition += renderedContent.Length; if (item.Location == ColumnLocation.Right) { _rightMargin += renderedContent.Length; } else { _leftMargin += renderedContent.Length; } item.RenderCount++; } } } ColorTracker.SetForeColor(originalForeColor); ColorTracker.SetBackColor(originalBackColor); // return the rendered string return(builder.ToString()); }