Skip to content
Open
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
8 changes: 6 additions & 2 deletions src/ConsoleTables/ConsoleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace ConsoleTables
{
public class ConsoleTable
{
private static readonly Regex AnsiRegex = new Regex(@"\e\[[0-9;]*m", RegexOptions.Compiled);

public IList<object> Columns { get; }
public IList<object[]> Rows { get; }

Expand Down Expand Up @@ -264,7 +266,7 @@ public static int GetTextWidth(string value)
if (value == null)
return 0;

var length = value.ToCharArray().Sum(c => UnicodeCalculator.GetWidth(c));
var length = AnsiRegex.Replace(value, "").ToCharArray().Sum(GetCharWidth);
return length;
}

Expand Down Expand Up @@ -357,13 +359,15 @@ private string GetNumberAlignment(int i)
: "-";
}

private static int GetCharWidth(char c) => UnicodeCalculator.GetWidth(c);

private List<int> ColumnLengths()
{
var columnLengths = Columns
.Select((t, i) => Rows.Select(x => x[i])
.Union(new[] { Columns[i] })
.Where(x => x != null)
.Select(x => x.ToString().ToCharArray().Sum(c => UnicodeCalculator.GetWidth(c))).Max())
.Max(x => AnsiRegex.Replace(x.ToString(), "").ToCharArray().Sum(GetCharWidth)))
.ToList();
return columnLengths;
}
Expand Down