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
13 changes: 13 additions & 0 deletions ChangeLogs/1.0.3-ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# V1.0.3

## New Property

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `CachingEnabled` | `bool` | `true` |When `true`, the generated table string is cached when the ToTable method is called. Cache will be cleared on any property change or method call. |

## New Method

| Method | Description |
|--------|-------------|
| `ClearCache()` | Clears the cached generated table string. |
43 changes: 43 additions & 0 deletions ConsoleTable.Text.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ static void Main(string[] args)

WriteTableFluent();

WriteBigTable();

Console.Read();
}

Expand Down Expand Up @@ -54,6 +56,7 @@ private static void WriteDefaultTableWithProperties()

var table = new Table
{
CachingEnabled = true,
Headers = new string[] { "Name", "Age", "City" },
Rows = new List<string[]>
{
Expand Down Expand Up @@ -156,6 +159,7 @@ private static void WriteTableWithStyling(bool headerTextAlignRight, bool rowTex

var table = new Table
{
CachingEnabled = true,
Padding = padding,
HeaderTextAlignmentRight = headerTextAlignRight,
RowTextAlignmentRight = rowTextAlignRight
Expand Down Expand Up @@ -192,4 +196,43 @@ private static void WriteTableFluent()
Console.WriteLine(tableString);
Console.WriteLine();
}

private static void WriteBigTable()
{
Console.WriteLine();
Console.WriteLine("Big table (may take some seconds to generate):");

var table = new Table
{
CachingEnabled = true,
HeaderTextAlignmentRight = false,
RowTextAlignmentRight = false,
Padding = 2
};

var columnCount = 5;
var headers = new List<string>();
for (var columnPos = 1; columnPos <= columnCount; columnPos++)
{
headers.Add($"Header {columnPos}");
}
table.Headers = headers.ToArray();

var rows = new List<string[]>();
for (var rowPos = 1; rowPos <= 100000; rowPos++)
{
var row = new string[columnCount];
for (var columnPos = 1; columnPos <= columnCount; columnPos++)
{
row[columnPos - 1] = $"Row {rowPos} -> Column {columnPos}";
}
rows.Add(row);
}
table.Rows = rows;

var tableString = table.ToTable();

Console.WriteLine(tableString);
Console.WriteLine();
}
}
32 changes: 28 additions & 4 deletions ConsoleTable.Text/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ public class Table
{
private string _tableCache = null;

private bool _cachingEnabled = true;
/// <summary>
/// Enables the caching of the generated table string when the ToTable method is called. Default is true.
/// Cache will be cleared on any property change or method call.
/// </summary>
public bool CachingEnabled
{
get => _cachingEnabled;
set
{
_cachingEnabled = value;

if (!_cachingEnabled)
ClearCache();
}
}

private string[] _headers = Array.Empty<string>();
/// <summary>
/// Gets or sets the headers of the table. This is a single optional top row.
Expand Down Expand Up @@ -282,9 +299,13 @@ private StringBuilder CreateSeperatorLine(int[] maximumCellWidths, int previousR
return formattedTable;
}

private void ClearCache()
/// <summary>
/// Clears the cached generated table string
/// </summary>
public Table ClearCache()
{
_tableCache = null;
return this;
}

/// <summary>
Expand All @@ -303,7 +324,7 @@ public Table Clear()
/// </summary>
public string ToTable()
{
if (!string.IsNullOrEmpty(_tableCache))
if (CachingEnabled && !string.IsNullOrEmpty(_tableCache))
return _tableCache;

var table = new List<string[]>();
Expand Down Expand Up @@ -365,9 +386,12 @@ public string ToTable()

formattedTable = CreateBottomLine(maximumCellWidths, previousRow.Count(), formattedTable);

_tableCache = formattedTable.ToString();
var generatedTable = formattedTable.ToString();

if (CachingEnabled)
_tableCache = generatedTable;

return _tableCache;
return generatedTable;
}

public override string ToString()
Expand Down
1 change: 1 addition & 0 deletions ConsoleTable.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<File Path="ChangeLogs/1.0.0-ChangeLog.md" />
<File Path="ChangeLogs/1.0.1-ChangeLog.md" />
<File Path="ChangeLogs/1.0.2-ChangeLog.md" />
<File Path="ChangeLogs/1.0.3-ChangeLog.md" />
</Folder>
<Folder Name="/Tests/">
<Project Path="Tests/ConsoleTable.Text.Tests/ConsoleTable.Text.Tests.csproj" />
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Output:
| `Padding` | `int` | `1` | The number of spaces on each side of cell content |
| `HeaderTextAlignmentRight` | `bool` | `false` | When `true`, header text is right-aligned otherwise left aligned |
| `RowTextAlignmentRight` | `bool` | `false` | When `true`, row text is right-aligned otherwise left aligned |
| `CachingEnabled` | `bool` | `true` | When `true`, the generated table string is cached when the ToTable method is called. Cache will be cleared on any property change or method call. |

### Methods

Expand All @@ -94,6 +95,7 @@ Output:
| `AddRows(params string[][] rows)` | Adds multiple data rows to the table. Rows are not required. |
| `ClearRows()` | Removes all data rows from the table (headers are preserved). |
| `Clear()` | Clear all the headers and rows from the table. |
| `ClearCache()` | Clears the generated table string cache. This can be done to save memory. |
| `ToTable() / ToString()` | Returns the formatted table as a string. |

## Examples
Expand Down
77 changes: 77 additions & 0 deletions Tests/ConsoleTable.Text.Tests/TableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,46 @@ namespace ConsoleTable.Text.Tests;

public class TableTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void PerformanceCheck(bool cacheEnabled)
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The test method name 'PerformanceCheck' is unclear about what it's verifying. Consider renaming to something more specific like 'ToTable_WithDifferentCachingSettings_GeneratesTableSuccessfully' to clarify that this is a functional test rather than a performance benchmark.

Suggested change
public void PerformanceCheck(bool cacheEnabled)
public void ToTable_WithDifferentCachingSettings_GeneratesTableSuccessfully(bool cacheEnabled)

Copilot uses AI. Check for mistakes.
{
var table = new Table
{
CachingEnabled = cacheEnabled,
HeaderTextAlignmentRight = true,
RowTextAlignmentRight = false,
Padding = 5
};

var columnCount = 100;
var headers = new List<string>();
for (var columnPos = 1; columnPos <= columnCount; columnPos++)
{
headers.Add($"Header {columnPos}");
}
table.Headers = headers.ToArray();

var rows = new List<string[]>();
for (var rowPos = 1; rowPos <= 100000; rowPos++)
{
var row = new string[columnCount];
for (var columnPos = 1; columnPos <= columnCount; columnPos++)
{
row[columnPos - 1] = $"Row {rowPos} -> Column {columnPos}";
}
rows.Add(row);
}
table.Rows = rows;

var tableResult1 = table.ToTable();
Assert.NotEmpty(tableResult1);

var tableResult2 = table.ToTable();
Assert.NotEmpty(tableResult2);
}

[Fact]
public void ToTable_EmptyTable_ReturnsEmptyString()
{
Expand Down Expand Up @@ -555,6 +595,43 @@ public void AddRows_ClearsCache()
Assert.Contains("NewRow", secondResult);
}

[Fact]
public void ClearCache()
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method name 'ClearCache' conflicts with the method being tested. Rename to follow the test naming pattern used elsewhere in the file, such as 'ClearCache_AfterCaching_AllowsTableRegenerationWithSameContent'.

Suggested change
public void ClearCache()
public void ClearCache_AfterCaching_AllowsTableRegenerationWithSameContent()

Copilot uses AI. Check for mistakes.
{
var table = new Table
{
CachingEnabled = true
};

table.AddRow("1");
var firstResult = table.ToTable();

table.ClearCache();
var secondResult = table.ToTable();

Assert.Contains("1", firstResult);
Assert.Contains("1", secondResult);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void CachingEnabled(bool cachingEnabled)
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method name 'CachingEnabled' conflicts with the property being tested. Rename to follow the test naming pattern used elsewhere in the file, such as 'CachingEnabled_WithTrueOrFalse_GeneratesTableSuccessfully'.

Suggested change
public void CachingEnabled(bool cachingEnabled)
public void CachingEnabled_WithTrueOrFalse_ReturnsTableContainingRow(bool cachingEnabled)

Copilot uses AI. Check for mistakes.
{
var table = new Table
{
CachingEnabled = cachingEnabled
};

table.AddRow("1");

var firstResult = table.ToTable();
var secondResult = table.ToTable();

Assert.Contains("1", firstResult);
Assert.Contains("1", secondResult);
}

[Fact]
public void ToString_ReturnsFormattedTable()
{
Expand Down