Skip to content

Commit

Permalink
add support to configure number alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-machado committed Feb 16, 2019
1 parent 8601de2 commit 7cc74a3
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 14 deletions.
54 changes: 54 additions & 0 deletions ConsoleTables.Tests/ConsoleTableTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using Xunit;

namespace ConsoleTables.Tests
{
public class ConsoleTableTest
{
[Fact]
public void ShouldBeToStringFromList()
{
var users = new List<User>
{
new User { Name = "Alexandre" , Age = 36 }
};
var table = ConsoleTable.From(users).ToString();

Assert.Equal($@" -------------------
| Name | Age |
-------------------
| Alexandre | 36 |
-------------------
Count: 1", table);
}

[Fact]
public void NumberShouldBeRightAligned()
{
var users = new List<User>
{
new User { Name = "Alexandre" , Age = 36 }
};
var table = ConsoleTable
.From(users)
.Configure(o => o.NumberRigthAligned = true)
.ToString();

Assert.Equal($@" -------------------
| Name | Age |
-------------------
| Alexandre | 36 |
-------------------
Count: 1", table);
}

class User
{
public string Name { get; set; }
public int Age { get; set; }
}
}
}
25 changes: 25 additions & 0 deletions ConsoleTables.Tests/ConsoleTables.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>

<ApplicationIcon />

<OutputType>Library</OutputType>

<StartupObject />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\ConsoleTables\ConsoleTables.csproj" />
</ItemGroup>

</Project>
18 changes: 16 additions & 2 deletions ConsoleTables.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.25928.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.28531.58
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTables.Sample", "src\ConsoleTables.Sample\ConsoleTables.Sample.csproj", "{D0DA8E1D-EDFD-483E-955D-977245CD6C31}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTables", "src\ConsoleTables\ConsoleTables.csproj", "{0E25B231-A2D3-4159-9B7E-711855E7B605}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTables.Tests", "ConsoleTables.Tests\ConsoleTables.Tests.csproj", "{06804159-B6DC-4EB1-AF69-A846424038D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -41,6 +43,18 @@ Global
{0E25B231-A2D3-4159-9B7E-711855E7B605}.Release|x64.Build.0 = Release|Any CPU
{0E25B231-A2D3-4159-9B7E-711855E7B605}.Release|x86.ActiveCfg = Release|Any CPU
{0E25B231-A2D3-4159-9B7E-711855E7B605}.Release|x86.Build.0 = Release|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Debug|x64.ActiveCfg = Debug|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Debug|x64.Build.0 = Debug|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Debug|x86.ActiveCfg = Debug|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Debug|x86.Build.0 = Debug|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Release|Any CPU.Build.0 = Release|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Release|x64.ActiveCfg = Release|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Release|x64.Build.0 = Release|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Release|x86.ActiveCfg = Release|Any CPU
{06804159-B6DC-4EB1-AF69-A846424038D5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleTables.Sample/ConsoleTables.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 9 additions & 2 deletions src/ConsoleTables.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using ConsoleTables;

namespace ConsoleTables.Sample
{
Expand Down Expand Up @@ -32,13 +31,20 @@ static void Main(String[] args)

var rows = Enumerable.Repeat(new Something(), 10);



ConsoleTable.From<Something>(rows).Write();

rows = Enumerable.Repeat(new Something(), 0);
ConsoleTable.From<Something>(rows).Write();

Console.WriteLine("\nNumberRigthAligned = true\n");
rows = Enumerable.Repeat(new Something(), 2);
ConsoleTable
.From(rows)
.Configure(o => o.NumberRigthAligned = true)
.Write();

var noCount =
new ConsoleTable(new ConsoleTableOptions
{
Expand All @@ -64,5 +70,6 @@ public Something()
public string Id { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public int NumberOfChildren { get; set; }
}
}
53 changes: 44 additions & 9 deletions src/ConsoleTables/ConsoleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ public class ConsoleTable
public IList<object[]> Rows { get; protected set; }

public ConsoleTableOptions Options { get; protected set; }
public Type[] ColumnTypes { get; private set; }

public static HashSet<Type> NumericTypes = new HashSet<Type>
{
typeof(int), typeof(double), typeof(decimal),
typeof(long), typeof(short), typeof(sbyte),
typeof(byte), typeof(ulong), typeof(ushort),
typeof(uint), typeof(float)
};

public ConsoleTable(params string[] columns)
:this(new ConsoleTableOptions { Columns = new List<string>(columns) })
{
: this(new ConsoleTableOptions { Columns = new List<string>(columns) })
{
}

public ConsoleTable(ConsoleTableOptions options)
Expand Down Expand Up @@ -49,16 +58,27 @@ public ConsoleTable AddRow(params object[] values)
return this;
}

public ConsoleTable Configure(Action<ConsoleTableOptions> action)
{
action(Options);
return this;
}

public static ConsoleTable From<T>(IEnumerable<T> values)
{
var table = new ConsoleTable();
var table = new ConsoleTable
{
ColumnTypes = GetColumnsType<T>().ToArray()
};

var columns = GetColumns<T>();

table.AddColumn(columns);

foreach (var propertyValues in values.Select(value => columns.Select(column => GetColumnValue<T>(value, column) )))
table.AddRow(propertyValues.ToArray());
foreach (
var propertyValues
in values.Select(value => columns.Select(column => GetColumnValue<T>(value, column)))
) table.AddRow(propertyValues.ToArray());

return table;
}
Expand All @@ -70,9 +90,14 @@ public override string ToString()
// find the longest column by searching each row
var columnLengths = ColumnLengths();

// set rigth alinment if is a number
var columnAlingment = Enumerable.Range(0, Columns.Count)
.Select(i => Options.NumberRigthAligned && NumericTypes.Contains(ColumnTypes[i]) ? "" : "-")
.ToList();

// create the string format with padding
var format = Enumerable.Range(0, Columns.Count)
.Select(i => " | {" + i + ",-" + columnLengths[i] + "}")
.Select(i => " | {" + i + "," + columnAlingment[i] + columnLengths[i] + "}")
.Aggregate((s, a) => s + a) + " |";

// find the longest formatted line
Expand Down Expand Up @@ -181,7 +206,7 @@ private string Format(List<int> columnLengths, char delimiter = '|')
{
var delimiterStr = delimiter == char.MinValue ? string.Empty : delimiter.ToString();
var format = (Enumerable.Range(0, Columns.Count)
.Select(i => " "+ delimiterStr + " {" + i + ",-" + columnLengths[i] + "}")
.Select(i => " " + delimiterStr + " {" + i + ",-" + columnLengths[i] + "}")
.Aggregate((s, a) => s + a) + " " + delimiterStr).Trim();
return format;
}
Expand Down Expand Up @@ -219,20 +244,30 @@ public void Write(Format format = ConsoleTables.Format.Default)
}

private static IEnumerable<string> GetColumns<T>()
{
{
return typeof(T).GetProperties().Select(x => x.Name).ToArray();
}

private static object GetColumnValue<T>(object target, string column)
{
return typeof(T).GetProperty(column).GetValue(target, null);
}

private static IEnumerable<Type> GetColumnsType<T>()
{
return typeof(T).GetProperties().Select(x => x.PropertyType).ToArray();
}
}

public class ConsoleTableOptions
{
public IEnumerable<string> Columns { get; set; } = new List<string>();
public bool EnableCount { get; set; } = true;

/// <summary>
/// Enable only from a list of objects
/// </summary>
public bool NumberRigthAligned { get; set; }
}

public enum Format
Expand Down

0 comments on commit 7cc74a3

Please sign in to comment.