private int[] FixWidths(IEnumerable <string[]> rows, TableFormat tableFormat, int[] columnsToAutoAdjust) { var widths = tableFormat.Widths; bool autoAlign = !columnsToAutoAdjust.IsNullOrEmpty(); if (autoAlign) { var maxWidths = new int[widths.Length]; Array.Copy(widths, maxWidths, widths.Length); foreach (var row in rows) { foreach (var i in columnsToAutoAdjust) { maxWidths[i] = Math.Max((row.SafeFromIndex(i) ?? string.Empty).Length, maxWidths[i]); } } widths = maxWidths.ToArray(); } var spacerWidth = new[] { tableFormat.Spacer4Header.Length, tableFormat.Spacer4FirstRow.Length, tableFormat.Spacer4OtherRows.Length }.Max(); var screenWidth = _getScreenWidth() - tableFormat.Indent.Length - spacerWidth; var totalWidth = 0; for (int index = 0; index < widths.Length; index++) { var width = widths[index]; if (totalWidth > screenWidth) { widths[index] = 0; } else if (totalWidth + width > screenWidth) { widths[index] = screenWidth - totalWidth; totalWidth = screenWidth; } else { totalWidth += width; } } return(widths); }
public void WriteTable(string[] headers, IEnumerable<string[]> rows, TableFormat tableFormat = null) { var lines = Enumerable.Empty<string>(); if (headers != null) { lines = lines.Union(headers); } if (rows != null) { lines = lines.Union(rows.Select(cells => string.Join(" - ", cells))); } Log.Info(lines.Dump()); }
public void WriteTable(string[] headers, IEnumerable <string[]> rows, TableFormat tableFormat = null) { var lines = Enumerable.Empty <string>(); if (headers != null) { lines = lines.Union(headers); } if (rows != null) { lines = lines.Union(rows.Select(cells => string.Join(" - ", cells))); } Log.Info(lines.Dump()); }
internal static IEnumerable <string> PivotChunks(this string[] cells, TableFormat tableFormat) { if (cells.IsNullOrEmpty()) { yield break; } var widths = tableFormat.Widths; var spacer = tableFormat.Spacer4FirstRow; //break cells into lines var cellsWithLines = new List <string> [cells.Length]; var maxLineCount = 0; for (int i = 0; i < cells.Length; i++) { var width = widths.SafeFromIndex(i); var lines = cells[i].GetChunks(width).ToList(); cellsWithLines[i] = lines; maxLineCount = Math.Max(maxLineCount, lines.Count); } //construct each line var line = new StringBuilder(); for (int l = 0; l < maxLineCount; l++) { line.Append(tableFormat.Indent); for (int c = 0; c < cellsWithLines.Length; c++) { var cell = cellsWithLines[c]; var cellLine = cell.SafeFromIndex(l); var width = widths.SafeFromIndex(c); if (c > 0) { line.Append(spacer); } line.Append(cellLine == null ? "".PadRight(width) : cellLine.PadRight(width)); } yield return(line.ToString()); line.Length = 0; spacer = tableFormat.Spacer4OtherRows; } }
internal static IEnumerable<string> PivotChunks(this string[] cells, TableFormat tableFormat) { if (cells.IsNullOrEmpty()) { yield break; } var widths = tableFormat.Widths; var spacer = tableFormat.Spacer4FirstRow; //break cells into lines var cellsWithLines = new List<string>[cells.Length]; var maxLineCount = 0; for (int i = 0; i < cells.Length; i++) { var width = widths.SafeFromIndex(i); var lines = cells[i].GetChunks(width).ToList(); cellsWithLines[i] = lines; maxLineCount = Math.Max(maxLineCount, lines.Count); } //construct each line var line = new StringBuilder(); for (int l = 0; l < maxLineCount; l++) { line.Append(tableFormat.Indent); for (int c = 0; c < cellsWithLines.Length; c++) { var cell = cellsWithLines[c]; var cellLine = cell.SafeFromIndex(l); var width = widths.SafeFromIndex(c); if (c > 0) { line.Append(spacer); } line.Append(cellLine == null ? "".PadRight(width) : cellLine.PadRight(width)); } yield return line.ToString(); line.Length = 0; spacer = tableFormat.Spacer4OtherRows; } }
/// <summary></summary> public void WriteTable(string[] headers, IEnumerable <string[]> rows, TableFormat tableFormat = null) { tableFormat = tableFormat == null ? new TableFormat() : tableFormat.Clone(); var columnsToAutoAdjust = tableFormat.Widths .Select((width, index) => width >= 0 ? -1 : index) .Where(i => i >= 0) .ToArray(); //if we need to autoalign, make sure we only enumerate the rows once // in case it can't be enumerated a second time var safeRows = columnsToAutoAdjust.IsNullOrEmpty() ? rows : rows.ToList(); tableFormat.Widths = FixWidths(safeRows, tableFormat, columnsToAutoAdjust); if (!headers.IsNullOrEmpty()) { headers.PivotChunks(tableFormat) .ForEach(_writer.WriteLine); } if (safeRows.IsNullOrEmpty()) { return; } foreach (var row in safeRows) { if (row.IsNullOrEmpty()) { _writer.WriteLine(); continue; } row.PivotChunks(tableFormat) .ForEach(_writer.WriteLine); } }
public void WriteTable(string[] headers, IEnumerable <string[]> rows, TableFormat tableFormat = null) { _innerWriters.ForEach(w => w.WriteTable(headers, rows, tableFormat)); }
public void WriteTable(string[] headers, IEnumerable<string[]> rows, TableFormat tableFormat = null) { _innerWriters.ForEach(w => w.WriteTable(headers, rows, tableFormat)); }