Skip to content

Commit 2691548

Browse files
authored
Merge pull request #20 from BrunoVT1992/develop
ShowBorders property (default true) + some small code cleanups
2 parents d839a47 + 87f4ed9 commit 2691548

8 files changed

Lines changed: 254 additions & 26 deletions

File tree

ChangeLogs/2.1.0-ChangeLog.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# V2.1.0
2+
3+
## New Properties
4+
5+
| Property | Type | Default | Description |
6+
|----------|------|---------|-------------|
7+
| `ShowBorders` | `bool` | `true` | When `false`, the table is drawn without borders for a more minimalist style |
8+
9+
10+
```csharp
11+
using ConsoleTable.Text;
12+
13+
// Setup the table
14+
var table = new Table
15+
{
16+
ShowBorders = false
17+
};
18+
19+
// Set headers
20+
table.SetHeaders("Name", "Age", "City");
21+
22+
// Add rows
23+
table.AddRow("Alice Cooper", "30", "New York");
24+
table.AddRows(new string[][]
25+
{
26+
new string[] { "Bob", "25", "Los Angeles" },
27+
new string[] { "Charlie Brown", "47", "Chicago" }
28+
});
29+
30+
// Set footers
31+
table.SetFooters("Total: 3", "Total Age: 102");
32+
33+
// Display the table
34+
Console.WriteLine(table.ToTable());
35+
```
36+
37+
Output:
38+
```
39+
Name Age City
40+
Alice Cooper 30 New York
41+
Bob 25 Los Angeles
42+
Charlie Brown 47 Chicago
43+
Total: 3 Total Age: 102
44+
```

ConsoleTable.Text.Examples/Program.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ class Program
44
{
55
static void Main(string[] args)
66
{
7+
Console.ForegroundColor = ConsoleColor.Black;
8+
Console.BackgroundColor = ConsoleColor.White;
9+
Console.Clear();
10+
Console.WriteLine();
11+
Console.WriteLine();
12+
713
WriteDefaultTable();
814

915
WriteDefaultTableWithProperties();
@@ -32,8 +38,12 @@ static void Main(string[] args)
3238

3339
WriteTableFluent();
3440

35-
WriteBigTable();
41+
WriteTableWithoutBorders();
3642

43+
//WriteBigTable();
44+
45+
Console.WriteLine();
46+
Console.WriteLine();
3747
Console.Read();
3848
}
3949

@@ -78,7 +88,8 @@ private static void WriteDefaultTableWithProperties()
7888
new string[] { "Alice Cooper", "30", "New York" },
7989
new string[] { "Bob", "25", "Los Angeles" },
8090
new string[] { "Charlie Brown", "47", "Chicago" }
81-
}
91+
},
92+
Footers = new string[] { "Total: 3", "Total Age: 102" }
8293
};
8394

8495
Console.WriteLine(table.ToTable());
@@ -245,6 +256,36 @@ private static void WriteTableFluent()
245256
Console.WriteLine();
246257
}
247258

259+
private static void WriteTableWithoutBorders()
260+
{
261+
Console.WriteLine();
262+
Console.WriteLine("Table without borders:");
263+
264+
// Setup the table
265+
var table = new Table
266+
{
267+
ShowBorders = false
268+
};
269+
270+
// Set headers
271+
table.SetHeaders("Name", "Age", "City");
272+
273+
// Add rows
274+
table.AddRow("Alice Cooper", "30", "New York");
275+
table.AddRows(new string[][]
276+
{
277+
new string[] { "Bob", "25", "Los Angeles" },
278+
new string[] { "Charlie Brown", "47", "Chicago" }
279+
});
280+
281+
// Set footers
282+
table.SetFooters("Total: 3", "Total Age: 102");
283+
284+
// Display the table
285+
Console.WriteLine(table.ToTable());
286+
Console.WriteLine();
287+
}
288+
248289
private static void WriteBigTable()
249290
{
250291
Console.WriteLine();

ConsoleTable.Text/ConsoleTable.Text.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<!-- NuGet Package Metadata -->
77
<PackageId>ConsoleTable.Text</PackageId>
8-
<Version>2.0.0</Version>
8+
<Version>2.1.0</Version>
99
<Authors>Bruno Van Thournout</Authors>
1010
<Description>A library for creating a formatted string table with customizable headers, footers, rows and easy to use styling options.</Description>
1111
<PackageProjectUrl>https://github.com/BrunoVT1992/ConsoleTable</PackageProjectUrl>

ConsoleTable.Text/Table.cs

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ public bool HeaderTextAlignmentRight
100100
}
101101
}
102102

103+
private bool _rowTextAlignmentRight;
103104
/// <summary>
104105
/// Gets or sets a value indicating whether the row text is aligned to the right or left
105106
/// </summary>
106-
private bool _rowTextAlignmentRight;
107107
public bool RowTextAlignmentRight
108108
{
109109
get => _rowTextAlignmentRight;
@@ -114,10 +114,10 @@ public bool RowTextAlignmentRight
114114
}
115115
}
116116

117+
private bool _footerTextAlignmentRight;
117118
/// <summary>
118119
/// Gets or sets a value indicating whether the footer text is aligned to the right or left
119120
/// </summary>
120-
private bool _footerTextAlignmentRight;
121121
public bool FooterTextAlignmentRight
122122
{
123123
get => _footerTextAlignmentRight;
@@ -128,6 +128,20 @@ public bool FooterTextAlignmentRight
128128
}
129129
}
130130

131+
private bool _showBorders = true;
132+
/// <summary>
133+
/// Gets or sets a value indicating whether the table borders are visible. Default is true.
134+
/// </summary>
135+
public bool ShowBorders
136+
{
137+
get => _showBorders;
138+
set
139+
{
140+
_showBorders = value;
141+
ClearCache();
142+
}
143+
}
144+
131145
/// <summary>
132146
/// Sets the headers of the table. Overwrites them each time.
133147
/// </summary>
@@ -194,8 +208,6 @@ public Table ClearRows()
194208
return this;
195209
}
196210

197-
198-
199211
/// <summary>
200212
/// Clears the cached generated table string
201213
/// </summary>
@@ -238,28 +250,37 @@ public string ToTable()
238250

239251
if (Headers?.Any() == true)
240252
{
241-
formattedTable = CreateTopLine(maximumCellWidths, Headers.Count(), formattedTable);
242-
topLineCreated = true;
253+
if (ShowBorders)
254+
{
255+
formattedTable = CreateTopLine(maximumCellWidths, Headers.Count(), formattedTable);
256+
topLineCreated = true;
257+
}
243258

244-
formattedTable = CreateValueLine(maximumCellWidths, Headers, HeaderTextAlignmentRight, TableDrawing.VerticalLine, formattedTable);
259+
formattedTable = CreateValueLine(maximumCellWidths, Headers, HeaderTextAlignmentRight, ShowBorders ? TableDrawing.VerticalLine : TableDrawing.EmptySpace, formattedTable);
245260

246261
previousRow = Headers;
247262

248263
//When there are no rows immediatly draw the bottom line after the header
249264
if (Rows?.Any() == true)
250265
{
251266
nextRow = Rows.First();
252-
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
267+
if (ShowBorders)
268+
{
269+
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
270+
}
253271
}
254272
else
255273
{
256-
formattedTable = CreateBottomLine(maximumCellWidths, Headers.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
274+
if (ShowBorders)
275+
{
276+
formattedTable = CreateBottomLine(maximumCellWidths, Headers.Count(), TableDrawing.HorizontalHeaderLine, formattedTable);
277+
}
257278
}
258279
}
259280

260281
if (Rows?.Any() == true)
261282
{
262-
if (!topLineCreated)
283+
if (!topLineCreated && ShowBorders)
263284
{
264285
formattedTable = CreateTopLine(maximumCellWidths, Rows.First().Count(), formattedTable);
265286
topLineCreated = true;
@@ -272,21 +293,27 @@ public string ToTable()
272293
{
273294
var row = CleanupRow(Rows[i]);
274295

275-
formattedTable = CreateValueLine(maximumCellWidths, row, RowTextAlignmentRight, TableDrawing.VerticalLine, formattedTable);
296+
formattedTable = CreateValueLine(maximumCellWidths, row, RowTextAlignmentRight, ShowBorders ? TableDrawing.VerticalLine : TableDrawing.EmptySpace, formattedTable);
276297

277298
previousRow = row;
278299

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

283-
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalLine, formattedTable);
304+
if (ShowBorders)
305+
{
306+
formattedTable = CreateSeperatorLine(maximumCellWidths, previousRow.Count(), nextRow.Count(), TableDrawing.HorizontalLine, formattedTable);
307+
}
284308
}
285309

286310
rowIndex++;
287311
}
288312

289-
formattedTable = CreateBottomLine(maximumCellWidths, previousRow.Count(), TableDrawing.HorizontalLine, formattedTable);
313+
if (ShowBorders)
314+
{
315+
formattedTable = CreateBottomLine(maximumCellWidths, previousRow.Count(), TableDrawing.HorizontalLine, formattedTable);
316+
}
290317
}
291318

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

414-
foreach (var column in row)
441+
for (int i = 0; i < row.Length; i++)
415442
{
443+
var column = row[i];
444+
445+
var leftVerticalLine = verticalLine;
446+
if (i == 0 && !ShowBorders)
447+
{
448+
leftVerticalLine = TableDrawing.Empty;
449+
}
450+
416451
var restWidth = maximumCellWidths[cellIndex];
417452
if (Padding > 0)
418453
restWidth -= Padding * 2;
419454

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

422457
if (cellIndex == 0 && cellIndex == lastCellIndex)
423-
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", verticalLine, paddingString, cellValue, paddingString, verticalLine));
458+
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", leftVerticalLine, paddingString, cellValue, paddingString, verticalLine));
424459
else if (cellIndex == 0)
425-
formattedTable.Append(string.Format("{0}{1}{2}{3}", verticalLine, paddingString, cellValue, paddingString));
460+
formattedTable.Append(string.Format("{0}{1}{2}{3}", leftVerticalLine, paddingString, cellValue, paddingString));
426461
else if (cellIndex == lastCellIndex)
427-
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", verticalLine, paddingString, cellValue, paddingString, verticalLine));
462+
formattedTable.AppendLine(string.Format("{0}{1}{2}{3}{4}", leftVerticalLine, paddingString, cellValue, paddingString, verticalLine));
428463
else
429-
formattedTable.Append(string.Format("{0}{1}{2}{3}", verticalLine, paddingString, cellValue, paddingString));
464+
formattedTable.Append(string.Format("{0}{1}{2}{3}", leftVerticalLine, paddingString, cellValue, paddingString));
430465

431466
cellIndex++;
432467
}

ConsoleTable.Text/TableDrawing.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ internal static class TableDrawing
1515
public const char HorizontalHeaderLine = '═';
1616
public const string VerticalLine = "│";
1717
public const string EmptySpace = " ";
18+
public const string Empty = "";
1819
}
1920
}

ConsoleTable.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<File Path="ChangeLogs/1.0.2-ChangeLog.md" />
1010
<File Path="ChangeLogs/1.0.3-ChangeLog.md" />
1111
<File Path="ChangeLogs/2.0.0-ChangeLog.md" />
12+
<File Path="ChangeLogs/2.1.0-ChangeLog.md" />
1213
</Folder>
1314
<Folder Name="/Tests/">
1415
<Project Path="Tests/ConsoleTable.Text.Tests/ConsoleTable.Text.Tests.csproj" />

0 commit comments

Comments
 (0)