Skip to content
Open
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
38 changes: 38 additions & 0 deletions src/ConsoleTables.Tests/ConsoleTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Xunit;

namespace ConsoleTables.Tests
Expand Down Expand Up @@ -280,6 +281,43 @@ this line should be longer 哈哈哈哈 yes it is oh
""", table);
}

[Fact]
public void MaxWidthZeroShouldDisableTruncation()
{
var table = new ConsoleTable("Column1", "Column2");
table.MaxWidth = 0; // Should disable truncation completely
table.AddRow("This is a very long text that should not be truncated when MaxWidth is 0", "Short");

var result = table.ToString();

// Should contain the full text without truncation
Assert.Contains("This is a very long text that should not be truncated when MaxWidth is 0", result);
Assert.Contains("Short", result);

// Should only have one data row (no extra rows from word wrapping)
var lines = result.Split('\n');
var dataLines = lines.Where(line => line.Contains("This is a very long") || line.Contains("Short")).ToArray();
Assert.Single(dataLines);
}

[Fact]
public void MaxWidthPositiveValueShouldTruncate()
{
var table = new ConsoleTable("Column1", "Column2");
table.MaxWidth = 10; // Should truncate at 10 characters
table.AddRow("This is a very long text that should be truncated", "Short");

var result = table.ToString();

// Should not contain the full original text in a single line
Assert.DoesNotContain("This is a very long text that should be truncated", result);

// Should have multiple rows due to word wrapping
var lines = result.Split('\n');
var dataLines = lines.Where(line => line.Contains("|") && !line.All(c => c == ' ' || c == '-' || c == '|')).ToArray();
Assert.True(dataLines.Length > 2); // Header + at least 2 data rows due to wrapping
}

class User
{
public string Name { get; set; }
Expand Down
39 changes: 21 additions & 18 deletions src/ConsoleTables/ConsoleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,31 +217,34 @@ private void SetFormats(List<int> columnLengths, List<string> columnAlignment, s
{
var row = Rows[i];

if (row.Any(o => o != null && o.ToString().Length > MaxWidth)) //checks if any column exceeds the MaxWidth
// Skip truncation entirely if MaxWidth is 0 or negative
if (MaxWidth <= 0 || !row.Any(o => o != null && o.ToString().Length > MaxWidth)) //checks if any column exceeds the MaxWidth
{
var newRow = new object[row.Length];
for (var j = 0; j < row.Length; j++) //loop through cells
continue;
}

var newRow = new object[row.Length];
for (var j = 0; j < row.Length; j++) //loop through cells
{
var cellStr = row[j]?.ToString() ?? "";
if (cellStr.Length > MaxWidth) //if cell exceeds MaxWidth
{
var cellStr = row[j]?.ToString() ?? "";
if (cellStr.Length > MaxWidth) //if cell exceeds MaxWidth
var cutCell = cellStr[..MaxWidth]; //cut the cell
var leftOver = cellStr[MaxWidth..]; //into two parts
if (cutCell.Length > 0 && cutCell[^1] != ' ' && cutCell[0] != ' ') //if the cut is in the middle of a word
{
var cutCell = cellStr[..MaxWidth]; //cut the cell
var leftOver = cellStr[MaxWidth..]; //into two parts
if (cutCell[^1] != ' ' && cutCell[0] != ' ') //if the cut is in the middle of a word
var lastSpace = cutCell.LastIndexOf(WordBreakDelimiter); //find the last WordBreak Delimiter of the first cell
if (lastSpace > 0) //if there is a space
{
var lastSpace = cutCell.LastIndexOf(WordBreakDelimiter); //find the last WordBreak Delimiter of the first cell
if (lastSpace > 0) //if there is a space
{
cutCell = cellStr[..lastSpace]; //cut the cell at the last space
leftOver = cellStr[lastSpace..]; //the leftover is the rest of the cell
}//if there is no space, the cell will be cut at MaxWidth
}
newRow[j] = leftOver.Trim();
Rows[i][j] = cutCell.Trim();
cutCell = cellStr[..lastSpace]; //cut the cell at the last space
leftOver = cellStr[lastSpace..]; //the leftover is the rest of the cell
}//if there is no space, the cell will be cut at MaxWidth
}
newRow[j] = leftOver.Trim();
Rows[i][j] = cutCell.Trim();
}
Rows.Insert(i + 1, newRow);
}
Rows.Insert(i + 1, newRow);
}

allLines.AddRange(Rows);
Expand Down