Skip to content

Commit

Permalink
made tababular
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed Feb 20, 2016
0 parents commit 9d22939
Show file tree
Hide file tree
Showing 30 changed files with 12,070 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
obj
bin
deploy
deploy/*
_ReSharper.*
*.csproj.user
*.resharper.user
*.ReSharper.user
*.teamcity.user
*.TeamCity.user
*.resharper
*.DotSettings.user
*.dotsettings.user
*.ncrunchproject
*.ncrunchsolution
*.suo
*.cache
~$*
.vs
.vs/*
_NCrunch_*
*.user
*.backup
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## 1.0.1

* Made Tababular

23 changes: 23 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Tababular is licensed under [The MIT License (MIT)](http://opensource.org/licenses/MIT)

# The MIT License (MIT)

Copyright (c) 2012-2016 Mogens Heller Grabe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Tababular

A simple .NET monospace text table formatting library.

You can use it if you are standing with a bunch of objects or dictionaries in your hand, and you
wish for them to become as nice as this:

===============================================
| FirstColumn | SecondColumn | ThirdColumn |
===============================================
| r1 | hej | hej igen |
===============================================
| r2 | hej | hej igen |
===============================================

This can easily be achieved by newing up the `TableFormatter` like this:

var formatter = new TableFormatter();

and then you call an appropriate `Format***` method on it, e.g. `FormatObjects`:

var objects = new[]
{
new {FirstColumn = "r1", SecondColumn = "hej", ThirdColumn = "hej igen"},
new {FirstColumn = "r2", SecondColumn = "hej", ThirdColumn = "hej igen"},
};

var text = tableFormatter.FormatObjects(objects);

Console.WriteLine(text);

which will result in the table shown above.

For now, Tababular does not support that much stuff - but one nice thing about it is that
it will properly format lines in cells, so that e.g.

var objects = new[]
{
new { MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}},
new { MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {80, 5432}}
};

var text = new TableFormatter().FormatObjects(objects);

Console.WriteLine(text);

becomes nice like this:

======================================
| MachineName | Ip | Ports |
======================================
| ctxtest01 | 10.0.0.10 | 80 |
| | | 8080 |
| | | 9090 |
======================================
| ctxtest02 | 10.0.0.11 | 80 |
| | | 5432 |
======================================

which looks pretty neat if you ask me.

# License

[The MIT License (MIT)](http://opensource.org/licenses/MIT)
19 changes: 19 additions & 0 deletions Tababular.Tests/Ex/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Linq;

namespace Tababular.Tests.Ex
{
public static class StringExtensions
{
public static string Normalized(this string str)
{
var lines = str.Split(new[] {Environment.NewLine, "\r", "\n"}, StringSplitOptions.None)
.SkipWhile(string.IsNullOrWhiteSpace)
.Reverse()
.SkipWhile(string.IsNullOrWhiteSpace)
.Reverse();

return string.Join(Environment.NewLine, lines.Select(l => l.TrimEnd()));
}
}
}
36 changes: 36 additions & 0 deletions Tababular.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Tababular.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Tababular.Tests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("512230fe-18e5-4c59-9ae8-6432b0a6143a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
172 changes: 172 additions & 0 deletions Tababular.Tests/SimpleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using NUnit.Framework;
using Tababular.Tests.Ex;

namespace Tababular.Tests
{
[TestFixture]
public class SimpleTest
{
[Test]
public void CanFormatAsTableWithTwoColumns()
{
var tableFormatter = new TableFormatter();

var text = tableFormatter.FormatObjects(new[]
{
new {FirstColumn = "r1", SecondColumn = "hej"},
new {FirstColumn = "r2", SecondColumn = "hej"},
new {FirstColumn = "r3", SecondColumn = "hej"},
new {FirstColumn = "r4", SecondColumn = "hej"}
});

Console.WriteLine(text);

const string expected = @"
================================
| FirstColumn | SecondColumn |
================================
| r1 | hej |
================================
| r2 | hej |
================================
| r3 | hej |
================================
| r4 | hej |
================================
";
Assert.That(text.Normalized(), Is.EqualTo(expected.Normalized()));
}

[Test]
public void CanFormatAsTableWithThreeColumns()
{
var tableFormatter = new TableFormatter();

var objects = new[]
{
new {FirstColumn = "r1", SecondColumn = "hej", ThirdColumn = "hej igen"},
new {FirstColumn = "r2", SecondColumn = "hej", ThirdColumn = "hej igen"},
};

var text = tableFormatter.FormatObjects(objects);

Console.WriteLine(text);

const string expected = @"
===============================================
| FirstColumn | SecondColumn | ThirdColumn |
===============================================
| r1 | hej | hej igen |
===============================================
| r2 | hej | hej igen |
===============================================
";

Assert.That(text.Normalized(), Is.EqualTo(expected.Normalized()));
}

[Test]
public void CanFormatAsTableWithCellWiderThanColumnLabel()
{
var tableFormatter = new TableFormatter();

var text = tableFormatter.FormatObjects(new[]
{
new {FirstColumn = "This is a fairly long text"}
});

Console.WriteLine(text);

const string expected = @"
===============================
| FirstColumn |
===============================
| This is a fairly long text |
===============================
";
Assert.That(text.Normalized(), Is.EqualTo(expected.Normalized()));
}

[Test]
public void CanUseDictionaryAsInput()
{
var tableFormatter = new TableFormatter();

var text = tableFormatter.FormatDictionaries(new[]
{
new Dictionary<string, string> { {"Headline with space", "Some value"} },
new Dictionary<string, string> { {"Headline with space", "Another value"} },
new Dictionary<string, string> { {"Another headline with space", "Third value"} },
new Dictionary<string, string> { {"Yet another headline with space", "Fourth value"} },
});

Console.WriteLine(text);

const string expected = @"
==========================================================================================
| Headline with space | Another headline with space | Yet another headline with space |
==========================================================================================
| Some value | | |
==========================================================================================
| Another value | | |
==========================================================================================
| | Third value | |
==========================================================================================
| | | Fourth value |
==========================================================================================
";

Assert.That(text.Normalized(), Is.EqualTo(expected.Normalized()));
}

[Test]
public void CanFormatAsTableWithCellWithMultipleLines()
{
var tableFormatter = new TableFormatter();

var text = tableFormatter.FormatObjects(new[]
{
new {FirstColumn = @"This is the first line
This is the second line
And this is the third and last line"}
});

Console.WriteLine(text);

const string expected = @"
========================================
| FirstColumn |
========================================
| This is the first line |
| This is the second line |
| And this is the third and last line |
========================================
";

Assert.That(text.Normalized(), Is.EqualTo(expected.Normalized()));
}

[Test]
public void MultipleCellsWithMultipleLines()
{
var objects = new[]
{
new { MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}},
new { MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {80, 5432}}
};

var text = new TableFormatter().FormatObjects(objects);

Console.WriteLine(text);
}
}
}
Loading

0 comments on commit 9d22939

Please sign in to comment.