Skip to content

Commit

Permalink
Merge pull request #10 from tdwright/develop
Browse files Browse the repository at this point in the history
Added support for styling
  • Loading branch information
tdwright authored Nov 15, 2017
2 parents 85276d1 + bf57a46 commit 42b3ffc
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 26 deletions.
75 changes: 69 additions & 6 deletions ConTabs.Tests/ConformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public void BasicTableWithNoDataShouldLookLikeThis()

// Assert
string expected = "";
expected += "+------------------------------------------------------------+" + Environment.NewLine;
expected += "+--------------+-----------+----------------+----------------+" + Environment.NewLine;
expected += "| StringColumn | IntColumn | CurrencyColumn | DateTimeColumn |" + Environment.NewLine;
expected += "+------------------------------------------------------------+" + Environment.NewLine;
expected += "+--------------+-----------+----------------+----------------+" + Environment.NewLine;
expected += "| no data |" + Environment.NewLine;
expected += "+------------------------------------------------------------+";
expected += "+--------------+-----------+----------------+----------------+";
tableString.ShouldBe(expected);
}

Expand All @@ -39,11 +39,74 @@ public void BasicTableWithOneLineOfDataShouldLookLikeThis()

// Assert
string expected = "";
expected += "+-------------------------------------------+" + Environment.NewLine;
expected += "+--------------+-----------+----------------+" + Environment.NewLine;
expected += "| StringColumn | IntColumn | CurrencyColumn |" + Environment.NewLine;
expected += "+-------------------------------------------+" + Environment.NewLine;
expected += "+--------------+-----------+----------------+" + Environment.NewLine;
expected += "| AAAA | 999 | 19.95 |" + Environment.NewLine;
expected += "+-------------------------------------------+";
expected += "+--------------+-----------+----------------+";
tableString.ShouldBe(expected);
}

[Test]
public void TableStyledAsUnicodePipesShouldLookLikeThis()
{
// Arrange
var listOfTestClasses = TestData.ListOfMinimalData(1);
var tableObj = Table<MinimalDataType>.Create(listOfTestClasses);
tableObj.TableStyle = Style.UnicodePipes;

// Act
var tableString = tableObj.ToString();

// Assert
string expected = "";
expected += "╔══════╦══════╗" + Environment.NewLine;
expected += "║ IntA ║ IntB ║" + Environment.NewLine;
expected += "╠══════╬══════╣" + Environment.NewLine;
expected += "║ 1 ║ 3 ║" + Environment.NewLine;
expected += "╚══════╩══════╝";
tableString.ShouldBe(expected);
}

[Test]
public void TableStyledAsHeavyShouldLookLikeThis()
{
// Arrange
var listOfTestClasses = TestData.ListOfMinimalData(1);
var tableObj = Table<MinimalDataType>.Create(listOfTestClasses);
tableObj.TableStyle = Style.Heavy;

// Act
var tableString = tableObj.ToString();

// Assert
string expected = "";
expected += "#======#======#" + Environment.NewLine;
expected += "# IntA # IntB #" + Environment.NewLine;
expected += "#======#======#" + Environment.NewLine;
expected += "# 1 # 3 #" + Environment.NewLine;
expected += "#======#======#";
tableString.ShouldBe(expected);
}

[Test]
public void TableWithCustomStyleShouldLookLikeThis()
{
// Arrange
var listOfTestClasses = TestData.ListOfMinimalData(1);
var tableObj = Table<MinimalDataType>.Create(listOfTestClasses);
tableObj.TableStyle = new Style('W', 'F', 'C');

// Act
var tableString = tableObj.ToString();

// Assert
string expected = "";
expected += "CFFFFFFCFFFFFFC" + Environment.NewLine;
expected += "W IntA W IntB W" + Environment.NewLine;
expected += "CFFFFFFCFFFFFFC" + Environment.NewLine;
expected += "W 1 W 3 W" + Environment.NewLine;
expected += "CFFFFFFCFFFFFFC";
tableString.ShouldBe(expected);
}
}
Expand Down
20 changes: 20 additions & 0 deletions ConTabs.Tests/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ public static List<TestDataType> ListOfTestData(int? limit = null)
if (!limit.HasValue || limit < 0) limit = list.Count;
return list.Take(limit.Value).ToList();
}

public static List<MinimalDataType> ListOfMinimalData(int? limit = null)
{
var list = new List<MinimalDataType>
{
new MinimalDataType{IntA = 1, IntB = 3},
new MinimalDataType{IntA = 2, IntB = 9},
new MinimalDataType{IntA = 3, IntB = 27},
new MinimalDataType{IntA = 4, IntB = 81},
new MinimalDataType{IntA = 4, IntB = 243}
};
if (!limit.HasValue || limit < 0) limit = list.Count;
return list.Take(limit.Value).ToList();
}
}

public class TestDataType
Expand All @@ -27,4 +41,10 @@ public class TestDataType
public DateTime DateTimeColumn { get; set; }
private string HiddenProp { get; set; }
}

public class MinimalDataType
{
public int IntA { get; set; }
public int IntB { get; set; }
}
}
59 changes: 40 additions & 19 deletions ConTabs/OutputBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@ namespace ConTabs
{
internal class OutputBuilder<T> where T:class
{
private const char Vertex = '+';
private const char HLineChar = '-';
private const char VLineChar = '|';

private StringBuilder sb;
private Table<T> table;
private Style style;

public static string BuildOutput(Table<T> t)
public static string BuildOutput(Table<T> t, Style s)
{
var instance = new OutputBuilder<T>(t);
var instance = new OutputBuilder<T>(t, s);
return instance.sb.ToString();
}

private OutputBuilder(Table<T> t)
private OutputBuilder(Table<T> t, Style s)
{
table = t;
style = s;
sb = new StringBuilder();
HLine(); NewLine();
HLine(TopMidBot.Top); NewLine();
Headers(); NewLine();
HLine(); NewLine();
HLine(TopMidBot.Mid); NewLine();
if (table.Data == null || table.Data.Count() == 0)
{
NoDataLine(); NewLine();
Expand All @@ -37,19 +35,23 @@ private OutputBuilder(Table<T> t)
DataLine(i); NewLine();
}
}
HLine();
HLine(TopMidBot.Bot);
}

private void NewLine()
{
sb.Append(Environment.NewLine);
}

private void HLine()
private void HLine(TopMidBot v)
{
int colWidths = table._colsShown.Sum(c => c.MaxWidth);
int innerWidth = colWidths + (3 * table._colsShown.Count) - 1;
sb.Append(Vertex + new String(HLineChar, innerWidth) + Vertex);
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));
}

private void NoDataLine()
Expand All @@ -59,26 +61,45 @@ private void NoDataLine()
int innerWidth = colWidths + (3 * table._colsShown.Count) - 1;
int leftPad = (innerWidth - noDataText.Length) / 2;
int rightPad = innerWidth - (leftPad + noDataText.Length);
sb.Append(VLineChar + new String(' ', leftPad) + noDataText + new string(' ', rightPad) + VLineChar);
sb.Append(style.Wall + new String(' ', leftPad) + noDataText + new string(' ', rightPad) + style.Wall);
}

private void Headers()
{
sb.Append(VLineChar);
sb.Append(style.Wall);
foreach (var col in table._colsShown)
{
sb.Append(" " + col.ColumnName + new string(' ', col.MaxWidth - col.ColumnName.Length) + " " + VLineChar);
sb.Append(" " + col.ColumnName + new string(' ', col.MaxWidth - col.ColumnName.Length) + " " + style.Wall);
}
}

private void DataLine(int i)
{
sb.Append(VLineChar);
sb.Append(style.Wall);
foreach (var col in table._colsShown)
{
var value = col.Values[i].ToString();
sb.Append(" " + value + new string(' ', col.MaxWidth - value.Length) + " " + VLineChar);
sb.Append(" " + value + new string(' ', col.MaxWidth - value.Length) + " " + style.Wall);
}
}

private enum TopMidBot
{
Top,
Mid,
Bot
}

private enum LeftCentreRight
{
Left,
Centre,
Right
}

private char GetCorner(TopMidBot v, LeftCentreRight h)
{
return style.Corners[(int)h, (int)v];
}
}
}
77 changes: 77 additions & 0 deletions ConTabs/Styles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace ConTabs
{
public class Style
{

public char Wall { get; set; }
public char Floor { get; set; }

public char[,] Corners = new char[3, 3];

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

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

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

public Style(char wall, char floor, char corners)
{
Wall = wall;
Floor = floor;

CornerTopLeft = corners;
CornerTopRight = corners;
CornerBottomLeft = corners;
CornerBottomRight = corners;

Intersection = corners;
TeeNoUp = corners;
TeeNoRight = corners;
TeeNoDown = corners;
TeeNoLeft = corners;
}

public Style(char wall, char floor, char tl, char tr, char bl, char br, char i, char tnu, char tnr, char tnd, char tnl)
{
Wall = wall;
Floor = floor;

CornerTopLeft = tl;
CornerTopRight = tr;
CornerBottomLeft = bl;
CornerBottomRight = br;

Intersection = i;
TeeNoUp = tnu;
TeeNoRight = tnr;
TeeNoDown = tnd;
TeeNoLeft = tnl;
}

public static Style Default => new Style('|', '-', '+');

public static Style Heavy => new Style('#', '=', '#');

public static Style UnicodePipes => new Style('║', '═', '╔', '╗', '╚', '╝', '╬', '╦', '╠', '╩', '╣');


}
}
4 changes: 3 additions & 1 deletion ConTabs/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Table<T> where T:class
{
public List<Column> Columns { get; set; }
internal List<Column> _colsShown => Columns.Where(c => !c.Hide).ToList();
public Style TableStyle { get; set; }

private IEnumerable<T> _data;
public IEnumerable<T> Data
Expand Down Expand Up @@ -47,6 +48,7 @@ public static Table<T> Create(IEnumerable<T> Source)

private Table()
{
TableStyle = Style.Default;
var props = typeof(T).GetTypeInfo().DeclaredProperties;
Columns = props
.Where(p => p.GetMethod.IsPublic)
Expand All @@ -56,7 +58,7 @@ private Table()

public override string ToString()
{
return OutputBuilder<T>.BuildOutput(this);
return OutputBuilder<T>.BuildOutput(this, TableStyle);
}
}
}

0 comments on commit 42b3ffc

Please sign in to comment.