Giter Club home page Giter Club logo

tababular's Introduction

Tababular

A simple .NET monospace text table formatting library for .NET 4.5 and .NET Standard 1.6.

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:

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

which looks pretty neat if you ask me.

Formatting different things

Tababular can format different things, which at the moment includes:

  • Objects: formatter.FormatObjects(objects)
  • Dictionaries: formatter.FormatDictionaries(dictionaries)
  • JSON: formatter.FormatJson(jsonText)

More niceness

What about longs texts? Consider this example where the "Comments" column can be used to supply arbitrarily long texts:

var objects = new[]
{
	new {MachineName = "ctxtest01", Ip = "10.0.0.10", Ports = new[] {80, 8080, 9090}, Comments = ""},
	new {MachineName = "ctxtest02", Ip = "10.0.0.11", Ports = new[] {5432},
		Comments = @"This bad boy hosts our database and a couple of internal jobs."}
};

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

In this case, the resulting table would look like this:

+----------------------------------------------------------------+-----------+-------------+-------+
| Comments                                                       | Ip        | MachineName | Ports |
+----------------------------------------------------------------+-----------+-------------+-------+
|                                                                | 10.0.0.10 | ctxtest01   | 80    |
|                                                                |           |             | 8080  |
|                                                                |           |             | 9090  |
+----------------------------------------------------------------+-----------+-------------+-------+
| This bad boy hosts our database and a couple of internal jobs. | 10.0.0.11 | ctxtest02   | 5432  |
+----------------------------------------------------------------+-----------+-------------+-------+

which might be fine, but since the texts can be even longer than this, it might end up being a problem.

Fear not! We can supply a small hint to the table formatter like this:

var hints = new Hints { MaxTableWidth = 80 };
var formatter = new TableFormatter(hints);

and then when we

var text = formatter.FormatObjects(objects);

it will look like this:

+------------------------------------------------+-----------+-------------+-------+
| Comments                                       | Ip        | MachineName | Ports |
+------------------------------------------------+-----------+-------------+-------+
|                                                | 10.0.0.10 | ctxtest01   | 80    |
|                                                |           |             | 8080  |
|                                                |           |             | 9090  |
+------------------------------------------------+-----------+-------------+-------+
| This bad boy hosts our database and a couple   | 10.0.0.11 | ctxtest02   | 5432  |
| of internal jobs.                              |           |             |       |
+------------------------------------------------+-----------+-------------+-------+

and the table will never become wider than at most 80 characters. Objectively speaking, this is actually freaking awesome.

License

The MIT License (MIT)

tababular's People

Contributors

gary-palmer avatar mookid8000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

tababular's Issues

dotNetCore

Hello does this library support .netcore?

Ability to specify culture

When formatting stuff to text, the thread's current culture is used e.g. to format decimals, which in Denmark could look like this:

0,18343

In English-speaking countries, it would look like this:

0.18343

and there will be differences in how dates, and maybe other stuff is formatted too.

It would be neat if all Format*** methods would accept a CultureInfo object as an optional argument, which would then be used in all value-to-text formatting methods.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.