Skip to content

Commit

Permalink
Merge pull request #17 from tdwright/develop
Browse files Browse the repository at this point in the history
Version 0.2.0. Refactoring with some breaking changes to public API. Notably, OutputBuilder is now nested and private and Corners have been split out into a separate class.
  • Loading branch information
tdwright authored Dec 18, 2017
2 parents b8b9c87 + 36af7c3 commit 273ed3e
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 124 deletions.
1 change: 1 addition & 0 deletions ConTabs.Tests/ConTabs.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="FormatTests.cs" />
<Compile Include="Table-Basic.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UsageTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
42 changes: 42 additions & 0 deletions ConTabs.Tests/UsageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using ConTabs.TestData;
using NUnit.Framework;
using Shouldly;
using System;

namespace ConTabs.Tests
{
[TestFixture]
class UsageTests
{
[Test]
public void OnceFormatSet_FormatCanBeUpdated()
{
// Arrange
var tableObj = Table<TestDataType>.Create();
tableObj.TableStyle = Style.Default;

// Act
tableObj.TableStyle = Style.Heavy;
var result = tableObj.ToString();

// Assert
result[0].ShouldBe('#');
}

[Test]
public void OnceFormatSet_CornerCanBeUpdated()
{
// Arrange
var tableObj = Table<TestDataType>.Create();
tableObj.TableStyle = Style.Default;

// Act
tableObj.TableStyle.Corners.CornerTopLeft = '$';
var result = tableObj.ToString();

// Assert
result[0].ShouldBe('$');
}

}
}
50 changes: 50 additions & 0 deletions ConTabs/Corners.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace ConTabs
{
public class Corners
{
private readonly char[,] cornerChars = new char[3, 3];

/*
* ╔═══╤═════╤═════╤═════╗
* ║ │ 0 │ 1 │ 2 ║
* ╠═══╪═════╪═════╪═════╣
* ║ 0 │ TL │ TNU │ TR ║
* ╟───┼─────┼─────┼─────╢
* ║ 1 │ TNL │ I │ TNR ║
* ╟───┼─────┼─────┼─────╢
* ║ 2 │ BL │ TND │ BR ║
* ╚═══╧═════╧═════╧═════╝
*
*/

public char CornerTopLeft { get { return cornerChars[0, 0]; } set { cornerChars[0, 0] = value; } }
public char CornerTopRight { get { return cornerChars[2, 0]; } set { cornerChars[2, 0] = value; } }
public char CornerBottomLeft { get { return cornerChars[0, 2]; } set { cornerChars[0, 2] = value; } }
public char CornerBottomRight { get { return cornerChars[2, 2]; } set { cornerChars[2, 2] = value; } }

public char Intersection { get { return cornerChars[1, 1]; } set { cornerChars[1, 1] = value; } }
public char TeeNoUp { get { return cornerChars[1, 0]; } set { cornerChars[1, 0] = value; } }
public char TeeNoRight { get { return cornerChars[2, 1]; } set { cornerChars[2, 1] = value; } }
public char TeeNoDown { get { return cornerChars[1, 2]; } set { cornerChars[1, 2] = value; } }
public char TeeNoLeft { get { return cornerChars[0, 1]; } set { cornerChars[0, 1] = value; } }

public char this[int i, int j]
{
get
{
return cornerChars[i, j];
}
}

public void SetAllCorners(char corner)
{
for(int i=0;i<cornerChars.GetLength(0);i++)
{
for(int j =0;j<cornerChars.GetLength(1);j++)
{
cornerChars[i, j] = corner;
}
}
}
}
}
149 changes: 76 additions & 73 deletions ConTabs/OutputBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
private 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)
internal 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];
}
}
}
}
Loading

0 comments on commit 273ed3e

Please sign in to comment.