views:

134

answers:

1

While writing a small C# application for myself I realized that it would be neat if I could easily draw tables in textmode. You know, like this:

+-----------------+-----------------+
|     Header 1    |    Header 2     |
+--------+--------+--------+--------+
| Data 1 | Data 2 | Data 3 | Data 4 |
| Data 1 | Data 2 | Data 3 | Data 4 |
| Data 1 | Data 2 | Data 3 | Data 4 |
+--------+--------+--------+--------+

A quick google search revealed nothing. Is there anything like this ready-made, or should I roll out my own?

Added: The ideal version would support:

  • Row/column spans;
  • Different border widths and styles;
  • Horizontal and vertical text alignment

But I'll settle for less as well. :)

+8  A: 

This is the one you are looking for http://www.phpguru.org/static/ConsoleTable.html or http://www.phpguru.org/downloads/csharp/ConsoleTable/ConsoleTable.cs

ConsoleTable table = new ConsoleTable();

table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});

table.SetHeaders(new string[] {"First Column", "Second Column", "Third Column"});
table.SetFooters(new string[] {"Yabba"});

table.InsertRow(new string[] {"", "ferfr", "frf        r"}, 7);
table.PrependRow(new string[] {});

System.Console.Write(table.ToString());

Produces...

+--------------+---------------+--------------+
| First Column | Second Column | Third Column |
+--------------+---------------+--------------+
|              |               |              |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
|              |               |              |
|              |               |              |
|              |               |              |
|              | ferfr         | frf        r |
+--------------+---------------+--------------+
| Yabba        |               |              |
+--------------+---------------+--------------+
Tzury Bar Yochay
Apparently this is the best there is. Oh well. Guess I'll roll out my own after all. :)
Vilx-