Skip to content

Commit

Permalink
Fixed: Write method which gave issues while using Alternative format
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmander authored Sep 29, 2023
1 parent 8762e7b commit abe793c
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/ConsoleTables/ConsoleTable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -72,7 +72,7 @@ public static ConsoleTable FromDictionary(Dictionary<string, Dictionary<string,
var table = new ConsoleTable();

var columNames = values.SelectMany(x => x.Value.Keys).Distinct().ToList();
columNames.Insert(0,"");
columNames.Insert(0, "");
table.AddColumn(columNames);
foreach (var row in values)
{
Expand Down Expand Up @@ -225,31 +225,31 @@ public string ToMinimalString()
}

public string ToStringAlternative()
{
var builder = new StringBuilder();
{
var builder = new StringBuilder();

// find the longest formatted line
var columnHeaders = string.Format(Formats[0].TrimStart(), Columns.ToArray());
// find the longest formatted line
var columnHeaders = string.Format(Formats[0].TrimStart(), Columns.ToArray());

// add each row
var results = Rows.Select((row, i) => string.Format(Formats[i + 1].TrimStart(), row)).ToList();
// add each row
var results = Rows.Select((row, i) => string.Format(Formats[i + 1].TrimStart(), row)).ToList();

// create the divider
var divider = Regex.Replace(columnHeaders, "[^| ]", "-");
var dividerPlus = divider.Replace("|", "+");
// create the divider
var divider = Regex.Replace(columnHeaders, "[^| ]", "-");
var dividerPlus = divider.Replace("|", "+");

builder.AppendLine(dividerPlus);
builder.AppendLine(columnHeaders);
builder.AppendLine(dividerPlus);
builder.AppendLine(columnHeaders);

foreach (var row in results)
{
builder.AppendLine(dividerPlus);
builder.AppendLine(row);
}
builder.AppendLine(dividerPlus);
foreach (var row in results)
{
builder.AppendLine(dividerPlus);
builder.AppendLine(row);
}
builder.AppendLine(dividerPlus);

return builder.ToString();
}
return builder.ToString();
}


private string Format(List<int> columnLengths, char delimiter = '|')
Expand Down Expand Up @@ -290,6 +290,8 @@ private List<int> ColumnLengths()

public void Write(Format format = ConsoleTables.Format.Default)
{
SetFormats(ColumnLengths(), Enumerable.Range(0, Columns.Count).Select(GetNumberAlignment).ToList());

switch (format)
{
case ConsoleTables.Format.Default:
Expand Down

0 comments on commit abe793c

Please sign in to comment.