Path: redb/redb.Examples/Output/TablePrinter.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/Output/TablePrinter.cs ---- namespace redb.Examples.Output; /// /// Cell with optional color highlighting. /// public record TableCell(string Text, ConsoleColor? Color = null); /// /// Simple table printer with Unicode box-drawing and color support. /// public class TablePrinter { private readonly int[] _widths; private readonly List _rows = []; private readonly List _separatorAfter = []; // row indices after which to add separator public TablePrinter(params int[] columnWidths) { _widths = columnWidths; } /// /// Add header row. /// public TablePrinter Header(params string[] cells) { _rows.Add(cells.Select(c => new TableCell(c)).ToArray()); _separatorAfter.Add(0); // separator after header return this; } /// /// Add data row with plain strings. /// public TablePrinter Row(params string[] cells) { _rows.Add(cells.Select(c => new TableCell(c)).ToArray()); return this; } /// /// Add data row with colored cells. /// public TablePrinter Row(params TableCell[] cells) { _rows.Add(cells); return this; } /// /// Add row and mark separator after it. /// public TablePrinter RowWithSeparator(params string[] cells) { _rows.Add(cells.Select(c => new TableCell(c)).ToArray()); _separatorAfter.Add(_rows.Count - 1); return this; } /// /// Add separator after last row. /// public TablePrinter Separator() { if (_rows.Count > 0) _separatorAfter.Add(_rows.Count - 1); return this; } /// /// Print table to console with colors. /// public void Print() { Console.WriteLine(TopLine()); for (int i = 0; i < _rows.Count; i++) { PrintDataLine(_rows[i]); if (_separatorAfter.Contains(i) && i < _rows.Count - 1) Console.WriteLine(MidLine()); } Console.WriteLine(BottomLine()); } private string TopLine() => Line('┌', '┬', '┐'); private string MidLine() => Line('├', '┼', '┤'); private string BottomLine() => Line('└', '┴', '┘'); private string Line(char left, char mid, char right) { var parts = _widths.Select(w => new string('─', w + 2)); return $"{left}{string.Join(mid, parts)}{right}"; } private void PrintDataLine(TableCell[] cells) { Console.Write('│'); for (int i = 0; i < _widths.Length; i++) { var cell = i < cells.Length ? cells[i] : new TableCell(""); var width = _widths[i]; var text = cell.Text; // Truncate if needed if (text.Length > width) text = text[..(width - 2)] + ".."; Console.Write(' '); // Apply color if specified if (cell.Color.HasValue) { var prev = Console.ForegroundColor; Console.ForegroundColor = cell.Color.Value; Console.Write(text.PadRight(width)); Console.ForegroundColor = prev; } else { Console.Write(text.PadRight(width)); } Console.Write(" │"); } Console.WriteLine(); } }