Permalink
Browse files

Nested OutputBuilder inside Table

  • Loading branch information...
tdwright committed Dec 18, 2017
1 parent a3ce1e6 commit 79b98469252c7af70e0907cd9b08ec04a0776ce8
Showing with 77 additions and 74 deletions.
  1. +76 −73 ConTabs/OutputBuilder.cs
  2. +1 −1 ConTabs/Table.cs
View
@@ -4,102 +4,105 @@
namespace ConTabs
{
internal class OutputBuilder<T> where T:class
public partial class Table<T>
{
private StringBuilder sb;
private Table<T> table;
private Style style;
public static string BuildOutput(Table<T> t, Style s)
internal sealed class OutputBuilder<T2> where T2 : class
{
var instance = new OutputBuilder<T>(t, s);
return instance.sb.ToString();
}
private readonly StringBuilder sb;
private readonly Table<T2> table;
private readonly Style style;
private OutputBuilder(Table<T> t, Style s)
{
table = t;
style = s;
sb = new StringBuilder();
HLine(TopMidBot.Top); NewLine();
Headers(); NewLine();
HLine(TopMidBot.Mid); NewLine();
if (table.Data == null || table.Data.Count() == 0)
public static string BuildOutput(Table<T2> t, Style s)
{
NoDataLine(); NewLine();
var instance = new OutputBuilder<T2>(t, s);
return instance.sb.ToString();
}
else
private OutputBuilder(Table<T2> t, Style s)
{
for (int i = 0; i < table.Data.Count(); i++)
table = t;
style = s;
sb = new StringBuilder();
HLine(TopMidBot.Top); NewLine();
Headers(); NewLine();
HLine(TopMidBot.Mid); NewLine();
if (table.Data == null || table.Data.Count() == 0)
{
NoDataLine(); NewLine();
}
else
{
DataLine(i); NewLine();
for (int i = 0; i < table.Data.Count(); i++)
{
DataLine(i); NewLine();
}
}
HLine(TopMidBot.Bot);
}
HLine(TopMidBot.Bot);
}
private void NewLine()
{
sb.Append(Environment.NewLine);
}
private void NewLine()
{
sb.Append(Environment.NewLine);
}
private void HLine(TopMidBot v)
{
sb.Append(GetCorner(v, LeftCentreRight.Left));
for (int i = 0; i < table._colsShown.Count; i++)
private void HLine(TopMidBot v)
{
sb.Append(new string(style.Floor, table._colsShown[i].MaxWidth + 2));
if (i < table._colsShown.Count - 1) sb.Append(GetCorner(v, LeftCentreRight.Centre));
sb.Append(GetCorner(v, LeftCentreRight.Left));
for (int i = 0; i < table._colsShown.Count; i++)
{
sb.Append(new string(style.Floor, table._colsShown[i].MaxWidth + 2));
if (i < table._colsShown.Count - 1) sb.Append(GetCorner(v, LeftCentreRight.Centre));
}
sb.Append(GetCorner(v, LeftCentreRight.Right));
}
sb.Append(GetCorner(v, LeftCentreRight.Right));
}
private void NoDataLine()
{
var noDataText = "no data";
int colWidths = table._colsShown.Sum(c => c.MaxWidth);
int innerWidth = colWidths + (3 * table._colsShown.Count) - 1;
int leftPad = (innerWidth - noDataText.Length) / 2;
int rightPad = innerWidth - (leftPad + noDataText.Length);
sb.Append(style.Wall + new String(' ', leftPad) + noDataText + new string(' ', rightPad) + style.Wall);
}
private void NoDataLine()
{
var noDataText = "no data";
int colWidths = table._colsShown.Sum(c => c.MaxWidth);
int innerWidth = colWidths + (3 * table._colsShown.Count) - 1;
int leftPad = (innerWidth - noDataText.Length) / 2;
int rightPad = innerWidth - (leftPad + noDataText.Length);
sb.Append(style.Wall + new String(' ', leftPad) + noDataText + new string(' ', rightPad) + style.Wall);
}
private void Headers()
{
sb.Append(style.Wall);
foreach (var col in table._colsShown)
private void Headers()
{
sb.Append(" " + col.ColumnName + new string(' ', col.MaxWidth - col.ColumnName.Length) + " " + style.Wall);
sb.Append(style.Wall);
foreach (var col in table._colsShown)
{
sb.Append(" " + col.ColumnName + new string(' ', col.MaxWidth - col.ColumnName.Length) + " " + style.Wall);
}
}
}
private void DataLine(int i)
{
sb.Append(style.Wall);
foreach (var col in table._colsShown)
private void DataLine(int i)
{
var value = col.StringValForCol(col.Values[i]);
sb.Append(" " + value + new string(' ', col.MaxWidth - value.Length) + " " + style.Wall);
sb.Append(style.Wall);
foreach (var col in table._colsShown)
{
var value = col.StringValForCol(col.Values[i]);
sb.Append(" " + value + new string(' ', col.MaxWidth - value.Length) + " " + style.Wall);
}
}
}
private enum TopMidBot
{
Top,
Mid,
Bot
}
private enum TopMidBot
{
Top,
Mid,
Bot
}
private enum LeftCentreRight
{
Left,
Centre,
Right
}
private enum LeftCentreRight
{
Left,
Centre,
Right
}
private char GetCorner(TopMidBot v, LeftCentreRight h)
{
return style.Corners[(int)h, (int)v];
private char GetCorner(TopMidBot v, LeftCentreRight h)
{
return style.Corners[(int)h, (int)v];
}
}
}
}
View
@@ -5,7 +5,7 @@
namespace ConTabs
{
public class Table<T> where T:class
public partial class Table<T> where T:class
{
public List<Column> Columns { get; set; }
internal List<Column> _colsShown => Columns.Where(c => !c.Hide).ToList();

0 comments on commit 79b9846

Please sign in to comment.