Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rebus-org/Tababular
:page_with_curl: Simple monospace text table formatting
https://github.com/rebus-org/Tababular
cli console monospace
Last synced: 14 days ago
JSON representation
:page_with_curl: Simple monospace text table formatting
- Host: GitHub
- URL: https://github.com/rebus-org/Tababular
- Owner: rebus-org
- License: other
- Created: 2016-02-20T14:45:08.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-08-21T08:58:09.000Z (3 months ago)
- Last Synced: 2024-09-21T13:58:11.134Z (about 2 months ago)
- Topics: cli, console, monospace
- Language: C#
- Homepage:
- Size: 22.3 MB
- Stars: 88
- Watchers: 6
- Forks: 16
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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)](http://opensource.org/licenses/MIT)