feat: Console uses unicode for table drawing on Net Standard

This commit is contained in:
Chris Cameron
2020-02-24 17:57:43 -05:00
parent 985578985a
commit a82645e902
2 changed files with 98 additions and 6 deletions

View File

@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed exception trying to get DHCP status of network interfaces on Linux
- Fixed a bug where color formatted console output on Net Standard was not raising the OnConsolePrint event
- Simplifying ANSI color methods, better cross-platform color support
- Console uses unicode for table drawing on Net Standard
## [10.3.0] - 2020-01-20
### Changed

View File

@@ -12,9 +12,28 @@ namespace ICD.Common.Utils
/// </summary>
public sealed class TableBuilder
{
#if SIMPLSHARP
private const char HORIZONTAL = '-';
private const char VERTICAL = '|';
private const char INTERSECT = '+';
#else
private const char INTERSECT = '\u253C';
private const char HORIZONTAL = '\u2500';
private const char VERTICAL = '\u2502';
private const char HORIZONTAL_DOWN = '\u252C';
private const char HORIZONTAL_UP = '\u2534';
private const char VERTICAL_RIGHT = '\u251C';
private const char VERTICAL_LEFT = '\u2524';
private const char DOWN_RIGHT = '\u250C';
private const char DOWN_LEFT = '\u2510';
private const char UP_RIGHT = '\u2514';
private const char UP_LEFT = '\u2518';
#endif
private readonly List<string[]> m_Rows;
private readonly string[] m_Columns;
@@ -117,6 +136,8 @@ namespace ICD.Common.Utils
int[] columnWidths = GetColumnWidths();
AppendTopSeparator(sb, columnWidths);
AppendRow(sb, m_Columns, columnWidths);
AppendSeparator(sb, columnWidths);
@@ -128,11 +149,65 @@ namespace ICD.Common.Utils
AppendRow(sb, row, columnWidths);
}
AppendSeparator(sb, columnWidths);
AppendBottomSeparator(sb, columnWidths);
return sb.ToString();
}
private void AppendTopSeparator(StringBuilder builder, IList<int> columnWidths)
{
#if SIMPLSHARP
// Can't do fancy tables so don't bother drawing the top row
return;
#else
builder.Append(DOWN_RIGHT);
for (int index = 0; index < columnWidths.Count; index++)
{
int length = columnWidths[index];
// Subsequent columns have padding
if (index > 0)
length++;
builder.Append(new string(HORIZONTAL, length));
if (index < columnWidths.Count - 1)
builder.Append(HORIZONTAL_DOWN);
}
builder.Append(DOWN_LEFT);
builder.AppendLine();
#endif
}
private void AppendBottomSeparator(StringBuilder builder, IList<int> columnWidths)
{
#if SIMPLSHARP
AppendSeparator(builder, columnWidths);
return;
#else
builder.Append(UP_RIGHT);
for (int index = 0; index < columnWidths.Count; index++)
{
int length = columnWidths[index];
// Subsequent columns have padding
if (index > 0)
length++;
builder.Append(new string(HORIZONTAL, length));
if (index < columnWidths.Count - 1)
builder.Append(HORIZONTAL_UP);
}
builder.Append(UP_LEFT);
builder.AppendLine();
#endif
}
#endregion
#region Private Methods
@@ -160,6 +235,10 @@ namespace ICD.Common.Utils
private static void AppendRow(StringBuilder builder, IList<string> row, IList<int> columnWidths)
{
#if !SIMPLSHARP
builder.Append(VERTICAL);
#endif
for (int index = 0; index < row.Count; index++)
{
if (index > 0)
@@ -172,11 +251,19 @@ namespace ICD.Common.Utils
builder.Append(VERTICAL);
}
#if !SIMPLSHARP
builder.Append(VERTICAL);
#endif
builder.AppendLine();
}
private static void AppendSeparator(StringBuilder sb, IList<int> columnWidths)
private static void AppendSeparator(StringBuilder builder, IList<int> columnWidths)
{
#if !SIMPLSHARP
builder.Append(VERTICAL_RIGHT);
#endif
for (int index = 0; index < columnWidths.Count; index++)
{
int length = columnWidths[index];
@@ -185,14 +272,18 @@ namespace ICD.Common.Utils
if (index > 0)
length++;
sb.Append(new string(HORIZONTAL, length));
builder.Append(new string(HORIZONTAL, length));
if (index < columnWidths.Count - 1)
sb.Append(INTERSECT);
builder.Append(INTERSECT);
}
sb.AppendLine();
#if !SIMPLSHARP
builder.Append(VERTICAL_LEFT);
#endif
builder.AppendLine();
}
#endregion
#endregion
}
}