Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions ChangeLogs/2.1.0-ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# V2.1.0

## New Properties

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `ShowBorders` | `bool` | `true` | When `false`, the table is drawn without borders for a more minimalist style |


```csharp
using ConsoleTable.Text;

// Setup the table
var table = new Table
{
ShowBorders = false
};

// Set headers
table.SetHeaders("Name", "Age", "City");

// Add rows
table.AddRow("Alice Cooper", "30", "New York");
table.AddRows(new string[][]
{
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
});

// Set footers
table.SetFooters("Total: 3", "Total Age: 102");

// Display the table
Console.WriteLine(table.ToTable());
```

Output:
```
Name Age City
Alice Cooper 30 New York
Bob 25 Los Angeles
Charlie Brown 47 Chicago
Total: 3 Total Age: 102
```
45 changes: 43 additions & 2 deletions ConsoleTable.Text.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine();
Console.WriteLine();

WriteDefaultTable();

WriteDefaultTableWithProperties();
Expand Down Expand Up @@ -32,8 +38,12 @@ static void Main(string[] args)

WriteTableFluent();

WriteBigTable();
WriteTableWithoutBorders();

//WriteBigTable();

Console.WriteLine();
Console.WriteLine();
Console.Read();
}

Expand Down Expand Up @@ -78,7 +88,8 @@ private static void WriteDefaultTableWithProperties()
new string[] { "Alice Cooper", "30", "New York" },
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
}
},
Footers = new string[] { "Total: 3", "Total Age: 102" }
};

Console.WriteLine(table.ToTable());
Expand Down Expand Up @@ -245,6 +256,36 @@ private static void WriteTableFluent()
Console.WriteLine();
}

private static void WriteTableWithoutBorders()
{
Console.WriteLine();
Console.WriteLine("Table without borders:");

// Setup the table
var table = new Table
{
ShowBorders = false
};

// Set headers
table.SetHeaders("Name", "Age", "City");

// Add rows
table.AddRow("Alice Cooper", "30", "New York");
table.AddRows(new string[][]
{
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
});

// Set footers
table.SetFooters("Total: 3", "Total Age: 102");

// Display the table
Console.WriteLine(table.ToTable());
Console.WriteLine();
}

private static void WriteBigTable()
{
Console.WriteLine();
Expand Down
2 changes: 1 addition & 1 deletion ConsoleTable.Text/ConsoleTable.Text.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<!-- NuGet Package Metadata -->
<PackageId>ConsoleTable.Text</PackageId>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Bruno Van Thournout</Authors>
<Description>A library for creating a formatted string table with customizable headers, footers, rows and easy to use styling options.</Description>
<PackageProjectUrl>https://github.com/BrunoVT1992/ConsoleTable</PackageProjectUrl>
Expand Down
71 changes: 53 additions & 18 deletions ConsoleTable.Text/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public bool HeaderTextAlignmentRight
}
}

private bool _rowTextAlignmentRight;
/// <summary>
/// Gets or sets a value indicating whether the row text is aligned to the right or left
/// </summary>
private bool _rowTextAlignmentRight;
public bool RowTextAlignmentRight
{
get => _rowTextAlignmentRight;
Expand All @@ -114,10 +114,10 @@ public bool RowTextAlignmentRight
}
}

private bool _footerTextAlignmentRight;
/// <summary>
/// Gets or sets a value indicating whether the footer text is aligned to the right or left
/// </summary>
private bool _footerTextAlignmentRight;
public bool FooterTextAlignmentRight
{
get => _footerTextAlignmentRight;
Expand All @@ -128,6 +128,20 @@ public bool FooterTextAlignmentRight
}
}

private bool _showBorders = true;
/// <summary>
/// Gets or sets a value indicating whether the table borders are visible. Default is true.
/// </summary>
public bool ShowBorders
{
get => _showBorders;
set
{
_showBorders = value;
ClearCache();
}
}

/// <summary>
/// Sets the headers of the table. Overwrites them each time.
/// </summary>
Expand Down Expand Up @@ -194,8 +208,6 @@ public Table ClearRows()
return this;
}



/// <summary>
/// Clears the cached generated table string
/// </summary>
Expand Down Expand Up @@ -238,28 +250,37 @@ public string ToTable()

if (Headers?.Any() == true)
{
formattedTable = CreateTopLine(maximumCellWidths, Headers.Count(), formattedTable);
topLineCreated = true;
if (ShowBorders)
{
formattedTable = CreateTopLine(maximumCellWidths, Headers.Count(), formattedTable);
topLineCreated = true;
}

formattedTable = CreateValueLine(maximumCellWidths, Headers, HeaderTextAlignmentRight, TableDrawing.VerticalLine, formattedTable);
formattedTable = CreateValueLine(maximumCellWidths, Headers, HeaderTextAlignmentRight, ShowBorders ? TableDrawing.VerticalLine : TableDrawing.EmptySpace, formattedTable);

previousRow = Headers;

//When there are no rows immediatly draw the bottom line after the header
if (Rows?.Any() == true)
{
nextRow = Rows.First();
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
if (ShowBorders)
{
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
}
}
else
{
formattedTable = CreateBottomLine(maximumCellWidths, Headers.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
if (ShowBorders)
{
formattedTable = CreateBottomLine(maximumCellWidths, Headers.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
}
}
}

if (Rows?.Any() == true)
{
if (!topLineCreated)
if (!topLineCreated && ShowBorders)
{
formattedTable = CreateTopLine(maximumCellWidths, Rows.First().Count(), formattedTable);
topLineCreated = true;
Expand All @@ -272,21 +293,27 @@ public string ToTable()
{
var row = CleanupRow(Rows[i]);

formattedTable = CreateValueLine(maximumCellWidths, row, RowTextAlignmentRight, TableDrawing.VerticalLine, formattedTable);
formattedTable = CreateValueLine(maximumCellWidths, row, RowTextAlignmentRight, ShowBorders ? TableDrawing.VerticalLine : TableDrawing.EmptySpace, formattedTable);

previousRow = row;

if (rowIndex != lastRowIndex)
{
nextRow = CleanupRow(Rows[rowIndex + 1]);

formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalLine, formattedTable);
if (ShowBorders)
{
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalLine, formattedTable);
}
}

rowIndex++;
}

formattedTable = CreateBottomLine(maximumCellWidths, previousRow.Count(), TableDrawing.HorizontalLine, formattedTable);
if (ShowBorders)
{
formattedTable = CreateBottomLine(maximumCellWidths, previousRow.Count(), TableDrawing.HorizontalLine, formattedTable);
}
}

if (Footers?.Any() == true)
Expand Down Expand Up @@ -411,22 +438,30 @@ private StringBuilder CreateValueLine(int[] maximumCellWidths, string[] row, boo
if (Padding > 0)
paddingString = string.Concat(Enumerable.Repeat(' ', Padding));

foreach (var column in row)
for (int i = 0; i < row.Length; i++)
{
var column = row[i];

var leftVerticalLine = verticalLine;
if (i == 0 && !ShowBorders)
{
leftVerticalLine = TableDrawing.Empty;
}

var restWidth = maximumCellWidths[cellIndex];
if (Padding > 0)
restWidth -= Padding * 2;

var cellValue = alignRight ? column.PadLeft(restWidth, ' ') : column.PadRight(restWidth, ' ');

if (cellIndex == 0 && cellIndex == lastCellIndex)
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", verticalLine, paddingString, cellValue, paddingString, verticalLine));
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", leftVerticalLine, paddingString, cellValue, paddingString, verticalLine));
else if (cellIndex == 0)
formattedTable.Append(string.Format("{0}{1}{2}{3}", verticalLine, paddingString, cellValue, paddingString));
formattedTable.Append(string.Format("{0}{1}{2}{3}", leftVerticalLine, paddingString, cellValue, paddingString));
else if (cellIndex == lastCellIndex)
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", verticalLine, paddingString, cellValue, paddingString, verticalLine));
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", leftVerticalLine, paddingString, cellValue, paddingString, verticalLine));
else
formattedTable.Append(string.Format("{0}{1}{2}{3}", verticalLine, paddingString, cellValue, paddingString));
formattedTable.Append(string.Format("{0}{1}{2}{3}", leftVerticalLine, paddingString, cellValue, paddingString));

cellIndex++;
}
Expand Down
1 change: 1 addition & 0 deletions ConsoleTable.Text/TableDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ internal static class TableDrawing
public const char HorizontalHeaderLine = '═';
public const string VerticalLine = "│";
public const string EmptySpace = " ";
public const string Empty = "";
}
}
1 change: 1 addition & 0 deletions ConsoleTable.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<File Path="ChangeLogs/1.0.2-ChangeLog.md" />
<File Path="ChangeLogs/1.0.3-ChangeLog.md" />
<File Path="ChangeLogs/2.0.0-ChangeLog.md" />
<File Path="ChangeLogs/2.1.0-ChangeLog.md" />
</Folder>
<Folder Name="/Tests/">
<Project Path="Tests/ConsoleTable.Text.Tests/ConsoleTable.Text.Tests.csproj" />
Expand Down
Loading