mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
feat: TableBuilder supports multi-line content
This commit is contained in:
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
- Added ToCollection extension method for copying an enumerable to a new collection
|
- Added ToCollection extension method for copying an enumerable to a new collection
|
||||||
|
- TableBuilder supports multi-line content
|
||||||
|
|
||||||
## [11.0.0] - 2020-03-20
|
## [11.0.0] - 2020-03-20
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
using ICD.Common.Utils.Extensions;
|
using ICD.Common.Utils.Extensions;
|
||||||
|
|
||||||
@@ -38,11 +39,13 @@ namespace ICD.Common.Utils
|
|||||||
private readonly List<string[]> m_Rows;
|
private readonly List<string[]> m_Rows;
|
||||||
private readonly string[] m_Columns;
|
private readonly string[] m_Columns;
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the columns.
|
/// Gets the columns.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public string[] Columns { get { return m_Columns; } }
|
public string[] Columns { get { return m_Columns.ToArray(); } }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the number of columns.
|
/// Gets the number of columns.
|
||||||
@@ -50,6 +53,8 @@ namespace ICD.Common.Utils
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public int ColumnsCount { get { return m_Columns.Length; } }
|
public int ColumnsCount { get { return m_Columns.Length; } }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructor.
|
/// Constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -91,10 +96,37 @@ namespace ICD.Common.Utils
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public TableBuilder AddRow(params string[] row)
|
public TableBuilder AddRow(params string[] row)
|
||||||
{
|
{
|
||||||
if (row != null && row.Length != m_Columns.Length)
|
// Special empty row case
|
||||||
|
if (row == null)
|
||||||
|
{
|
||||||
|
m_Rows.Add(null);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (row.Length != m_Columns.Length)
|
||||||
throw new ArgumentException("Row must match columns length.");
|
throw new ArgumentException("Row must match columns length.");
|
||||||
|
|
||||||
m_Rows.Add(row);
|
// Split new-lines into multiple rows
|
||||||
|
List<string[]> rows = new List<string[]>();
|
||||||
|
|
||||||
|
for (int column = 0; column < row.Length; column++)
|
||||||
|
{
|
||||||
|
string cell = row[column];
|
||||||
|
string[] lines = Regex.Split(cell, "\r\n|\r|\n");
|
||||||
|
|
||||||
|
for (int line = 0; line < lines.Length; line++)
|
||||||
|
{
|
||||||
|
// Add a new row for this line
|
||||||
|
if (line >= rows.Count)
|
||||||
|
rows.Add(new string[row.Length]);
|
||||||
|
|
||||||
|
// Insert the line into the row
|
||||||
|
rows[line][column] = lines[line];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add all of the rows into the table
|
||||||
|
m_Rows.AddRange(rows);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -108,12 +140,21 @@ namespace ICD.Common.Utils
|
|||||||
return AddRow(new string[m_Columns.Length]);
|
return AddRow(new string[m_Columns.Length]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a horizontal line to the builder.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public TableBuilder AddSeparator()
|
public TableBuilder AddSeparator()
|
||||||
{
|
{
|
||||||
return AddRow(null);
|
return AddRow(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a new header to the builder.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="row"></param>
|
||||||
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public TableBuilder AddHeader(params string[] row)
|
public TableBuilder AddHeader(params string[] row)
|
||||||
{
|
{
|
||||||
@@ -133,7 +174,7 @@ namespace ICD.Common.Utils
|
|||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
{
|
||||||
int[] columnWidths = GetColumnWidths();
|
int[] columnWidths = GetColumnWidths();
|
||||||
|
|
||||||
AppendTopSeparator(sb, columnWidths);
|
AppendTopSeparator(sb, columnWidths);
|
||||||
@@ -150,7 +191,7 @@ namespace ICD.Common.Utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
AppendBottomSeparator(sb, columnWidths);
|
AppendBottomSeparator(sb, columnWidths);
|
||||||
|
}
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user