/// <summary> /// Checks the underlying <see cref="RowCollection"/> and list of <see cref="Column"/>s to determine if any expansion is needed. /// </summary> /// <param name="neededIndex">The index required.</param> private void ExpandIfNeeded(int neededIndex) { if (neededIndex >= columns.Count) { if (rows.Count == 0) { rows.Add(new Row()); } var max = rows.Count > 0 ? rows.Max(r => r.Count) : 0; while (neededIndex >= columns.Count) { columns.Add(new Column(rows, columns.Count)); } foreach (var r in rows) { while (r.Count <= neededIndex) { r.AddCell(new Cell()); } } } }
/// <summary> /// Checks the column to see if the underlying <see cref="RowCollection"/> needs to be expanded to accommodate the specified index. /// </summary> /// <param name="indexNeeded">The index that needs to be interacted with.</param> private void ExpandIfNeeded(int indexNeeded) { while (indexNeeded >= rows.Count) { rows.Add(new Row(rows.Max(r => r.Count))); } }
/// <summary> /// Returns a string containing this table formatted for displaying in a log or console. /// </summary> /// <param name="maxWidth">The maximum width to allow each cell to be.</param> /// <param name="withColor">A value indicating whether or not to print the cell colors with the table.</param> /// <param name="chrome">The <see cref="IChromeCollection"/> to use when printing the table.</param> /// <returns>A string containing the formatted table.</returns> public string ToString(int maxWidth, bool withColor, IChromeCollection chrome) { var sb = new StringBuilder(); var numRows = this.NumberOfRows; var numCols = this.NumberOfColumns; if (maxWidth == 0) { maxWidth = this.rows.Max(r => r.Max(c => c.GetTotalWidth())) * numCols; } var columnWidths = new int[numCols]; for (var i = 0; i < numCols; i++) { columnWidths[i] = Math.Min(rows.Max(r => r[i].GetTotalWidth()), maxWidth); if (headers != null && headers.Count > i) { columnWidths[i] = Math.Min(Math.Max(columnWidths[i], headers[i].GetTotalWidth()), maxWidth); } columnWidths[i] = Math.Max(columnWidths[i], MinimumColumnWidth); } var totalWidth = columnWidths.Sum(); totalWidth += (numCols * 2) + numCols + 1; if (headers.Count > 0) { sb.Append(this.GetHeader(chrome, maxWidth, numCols, withColor, columnWidths)); } else { sb.Append(this.GetChromeLine(chrome.BodyTopLeft, chrome.BodyTop, chrome.BodyTopJoin, chrome.BodyTopRight, columnWidths)); } sb.Append(this.GetBody(chrome, withColor, totalWidth, numRows, numCols, columnWidths)); return(sb.ToString()); }
/// <summary> /// Returns a string containing this table formatted for displaying in a log or console. /// </summary> /// <param name="maxWidth">The maximum width to allow each cell to be.</param> /// <param name="withColor">A value indicating whether or not to print the cell colors with the table.</param> /// <returns>A string containing the formatted table.</returns> public string ToString(int maxWidth, bool withColor) { var sb = new StringBuilder(); var numRows = this.NumberOfRows; var numCols = this.NumberOfColumns; if (maxWidth == 0) { maxWidth = this.rows.Max(r => r.Max(c => c.GetTotalWidth())) * numCols; } var columnWidths = new int[numCols]; for (var i = 0; i < numCols; i++) { columnWidths[i] = Math.Min(rows.Max(r => r[i].GetTotalWidth()), maxWidth); if (headers != null && headers.Count > i) { columnWidths[i] = Math.Min(Math.Max(columnWidths[i], headers[i].GetTotalWidth()), maxWidth); } columnWidths[i] = Math.Max(columnWidths[i], MinimumColumnWidth); } var totalWidth = columnWidths.Sum(); totalWidth += (numCols * 2) + numCols + 1; sb.Append(horizontalBorder, totalWidth); sb.Append(lineEnding); if (headers.Count > 0) { var numLines = headers.Max(c => c.GetNumberOfLines(maxWidth)); for (var line = 0; line < numLines; line++) { sb.Append(verticalBorder); sb.Append(' '); for (var head = 0; head < numCols; head++) { sb.Append(headers[head].GetLine(line, columnWidths[head], withColor)); sb.Append(' '); sb.Append(verticalBorder); if (head < numCols - 1) { sb.Append(' '); } } sb.Append(lineEnding); } sb.Append(verticalBorder); sb.Append(headerBottomBorder, totalWidth - 2); sb.Append(verticalBorder); sb.Append(lineEnding); } for (var row = 0; row < numRows; row++) { if (row > 0) { sb.Append(verticalBorder); sb.Append(horizontalBorder, totalWidth - 2); sb.Append(verticalBorder); sb.Append(lineEnding); } var numLines = 0; for (var i = 0; i < numCols; i++) { numLines = Math.Max(rows[row][i].GetNumberOfLines(columnWidths[i]), numLines); } for (var line = 0; line < numLines; line++) { sb.Append(verticalBorder); sb.Append(' '); for (var col = 0; col < numCols; col++) { sb.Append(rows[row][col].GetLine(line, columnWidths[col], withColor)); sb.Append(' '); sb.Append(verticalBorder); if (col < numCols - 1) { sb.Append(' '); } } sb.Append(lineEnding); } } sb.Append(horizontalBorder, totalWidth); sb.Append(lineEnding); return(sb.ToString()); }