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

## New Properties

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `Headers` | `string[]` | `Array.Empty<string>()` | The table headers. Headers are not required. |
| `Rows` | `List<string[]>` | `new List<string[]>()` | All the data rows for the table. Rows are not required. |

## New Method

| Method | Description |
|--------|-------------|
| `AddRows(params string[][] rows)` | Adds multiple data rows to the table. Rows are not required. |
51 changes: 41 additions & 10 deletions ConsoleTable.Text.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ static void Main(string[] args)
{
WriteDefaultTable();

WriteDefaultTableWithProperties();

WriteTableWithStyling(true, true, 10);

WriteTableWithStyling(false, true, 10);
Expand Down Expand Up @@ -35,8 +37,31 @@ private static void WriteDefaultTable()
var table = new Table();
table.SetHeaders("Name", "Age", "City");
table.AddRow("Alice Cooper", "30", "New York");
table.AddRow("Bob", "25", "Los Angeles");
table.AddRow("Charlie Brown", "47", "Chicago");
table.AddRows(new string[][]
{
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
});

Console.WriteLine(table.ToTable());
Console.WriteLine();
}

private static void WriteDefaultTableWithProperties()
{
Console.WriteLine();
Console.WriteLine("Default table with properties instead of methods:");

var table = new Table
{
Headers = new string[] { "Name", "Age", "City" },
Rows = new List<string[]>
{
new string[] { "Alice Cooper", "30", "New York" },
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
}
};

Console.WriteLine(table.ToTable());
Console.WriteLine();
Expand Down Expand Up @@ -65,9 +90,11 @@ private static void WriteTableMoreHeaders()

table.SetHeaders("Name", "Age", "City", "Country");

table.AddRow("Alice Cooper", "30");
table.AddRow("Bob", "25");
table.AddRow("Charlie Brown", "47");
table.AddRows(
new string[] { "Alice Cooper", "30" },
new string[] { "Bob", "25" },
new string[] { "Charlie Brown", "47" }
);

Console.WriteLine(table.ToString());
Console.WriteLine();
Expand All @@ -82,9 +109,11 @@ private static void WriteTableLessHeaders()

table.SetHeaders("Name");

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

Console.WriteLine(table.ToString());
Console.WriteLine();
Expand Down Expand Up @@ -154,8 +183,10 @@ private static void WriteTableFluent()
var tableString = new Table()
.SetHeaders("Name", "Age", "City")
.AddRow("Alice Cooper", "30", "New York")
.AddRow("Bob", "25", "Los Angeles")
.AddRow("Charlie Brown", "47", "Chicago")
.AddRows(
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
)
.ToTable();

Console.WriteLine(tableString);
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>1.0.1</Version>
<Version>1.0.2</Version>
<Authors>Bruno Van Thournout</Authors>
<Description>A library for creating a formatted string tables with customizable headers, rows, and alignment options.</Description>
<PackageProjectUrl>https://github.com/BrunoVT1992/ConsoleTable</PackageProjectUrl>
Expand Down
77 changes: 65 additions & 12 deletions ConsoleTable.Text/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,36 @@ namespace ConsoleTable.Text
{
public class Table
{
private string[] _headers;
private List<string[]> _rows = new List<string[]>();
private string _tableCache = null;

private string[] _headers = Array.Empty<string>();
/// <summary>
/// Gets or sets the headers of the table. This is a single optional top row.
/// </summary>
public string[] Headers
{
get => _headers;
set
{
_headers = value ?? Array.Empty<string>();
ClearCache();
}
}

private List<string[]> _rows = new List<string[]>();
/// <summary>
/// Gets or sets the rows of the table
/// </summary>
public List<string[]> Rows
{
get => _rows;
set
{
_rows = value ?? new List<string[]>();
ClearCache();
}
}

private int _padding = 1;
/// <summary>
/// Gets or sets the amount of padding in spaces left and right of the rows cell content. Default is 1
Expand Down Expand Up @@ -63,8 +89,7 @@ public bool RowTextAlignmentRight
/// </summary>
public Table SetHeaders(params string[] headers)
{
_headers = headers;
ClearCache();
Headers = headers;
return this;
}

Expand All @@ -73,18 +98,46 @@ public Table SetHeaders(params string[] headers)
/// </summary>
public Table AddRow(params string[] row)
{
_rows.Add(row);
if (Rows == null)
Rows = new List<string[]>();

Rows.Add(row);
ClearCache();
return this;
}

/// <summary>
/// Adds multiple rows to the table
/// </summary>
public Table AddRows(params string[][] rows)
{
if (rows != null)
{
if (Rows == null)
Rows = new List<string[]>();

Rows.AddRange(rows);
ClearCache();
}

return this;
}

/// <summary>
/// Clears all the rows from the table
/// </summary>
public Table ClearRows()
{
_rows.Clear();
ClearCache();
if (Rows == null)
{
Rows = new List<string[]>();
}
else
{
Rows.Clear();
ClearCache();
}

return this;
}

Expand Down Expand Up @@ -256,18 +309,18 @@ public string ToTable()
var table = new List<string[]>();

var firstRowIsHeader = false;
if (_headers?.Any() == true)
if (Headers?.Any() == true)
{
table.Add(_headers);
table.Add(Headers);
firstRowIsHeader = true;
}

if (_rows?.Any() == true)
if (Rows?.Any() == true)
{
foreach (var row in _rows)
foreach (var row in Rows)
{
//Weird behaviour with empty rows
if ((row == null || row.Length <= 0) && _headers?.Length > 0)
if ((row == null || row.Length <= 0) && Headers?.Length > 0)
table.AddRange(new string[][] { new string[] { " " } });
else
table.Add(row);
Expand Down
1 change: 1 addition & 0 deletions ConsoleTable.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Folder Name="/ChangeLogs/">
<File Path="ChangeLogs/1.0.0-ChangeLog.md" />
<File Path="ChangeLogs/1.0.1-ChangeLog.md" />
<File Path="ChangeLogs/1.0.2-ChangeLog.md" />
</Folder>
<Folder Name="/Tests/">
<Project Path="Tests/ConsoleTable.Text.Tests/ConsoleTable.Text.Tests.csproj" />
Expand Down
47 changes: 33 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ConsoleTable
# ConsoleTable.Text

A lightweight .NET library for creating beautifully formatted console tables with customizable headers, rows, padding, and text alignment.

Expand All @@ -14,6 +14,9 @@ A lightweight .NET library for creating beautifully formatted console tables wit
- Optimized for performance
- Support for varying column counts across rows (each row can have its own number of cells).

## Releases
Check releases for the changelog here [https://github.com/BrunoVT1992/ConsoleTable/releases/](https://github.com/BrunoVT1992/ConsoleTable/releases/)

## Installation

### Package Manager
Expand Down Expand Up @@ -45,12 +48,16 @@ var table = new Table();
table.SetHeaders("Name", "Age", "City");

// Add rows
table.AddRow("Alice", "30", "New York");
table.AddRow("Bob", "25", "Los Angeles");
table.AddRow("Charlie", "35", "Chicago");
table.AddRow("Alice Cooper", "30", "New York");

table.AddRows(new string[][]
{
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
});

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

Output:
Expand All @@ -72,6 +79,8 @@ Output:

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `Headers` | `string[]` | `Array.Empty<string>()` | The table headers. Headers are not required. |
| `Rows` | `List<string[]>` | `new List<string[]>()` | All the data rows for the table. Rows are not required. |
| `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 |
Expand All @@ -82,6 +91,7 @@ Output:
|--------|-------------|
| `SetHeaders(params string[] headers)` | Sets the table headers. Calling this again will overwrite previous headers. Headers are not required. |
| `AddRow(params string[] row)` | Adds a data row to the table. Rows are not required. |
| `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. |
| `ToTable() / ToString()` | Returns the formatted table as a string. |
Expand All @@ -97,9 +107,11 @@ var table = new Table { Padding = 10 };

table.SetHeaders("Name", "Age", "City");

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

Console.WriteLine(table.ToTable());
```
Expand All @@ -126,9 +138,11 @@ var table = new Table { HeaderTextAlignmentRight = true, RowTextAlignmentRight =

table.SetHeaders("Name", "Age", "City");

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

Console.WriteLine(table.ToTable());
```
Expand All @@ -146,6 +160,7 @@ Output:
└───────────────┴─────┴─────────────┘
```


### Table with inconsistent columns across rows

```csharp
Expand All @@ -169,7 +184,7 @@ table.AddRow("Nathalie", "29", "Paris", "France", "Europe", "Earth", "Solar Syst
table.AddRow("Mathias", "37", "Oslo", "Norway", "Europe", "Earth", "Solar System");
table.AddRow("Kenny", "55", "Tokyo");

Console.WriteLine(table.ToString());
Console.WriteLine(table.ToTable());
```

Output:
Expand Down Expand Up @@ -205,6 +220,7 @@ Output:
└──────────┴─────┴──────────┘
```


### Write a Table Fluent

```csharp
Expand All @@ -213,8 +229,10 @@ using ConsoleTable.Text;
var tableString = new Table()
.SetHeaders("Name", "Age", "City")
.AddRow("Alice Cooper", "30", "New York")
.AddRow("Bob", "25", "Los Angeles")
.AddRow("Charlie Brown", "47", "Chicago")
.AddRows(
new string[] { "Bob", "25", "Los Angeles" },
new string[] { "Charlie Brown", "47", "Chicago" }
)
.ToTable();

Console.WriteLine(tableString);
Expand All @@ -233,6 +251,7 @@ Output:
└───────────────┴─────┴─────────────┘
```


## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Loading