Skip to content

2. Styling your table

Tom Wright edited this page Nov 18, 2018 · 2 revisions

The characters that make up ConTabs tables can be controlled, so that you can make your tables look however you'd like.

There are some built-in styles, which are easy to apply, but it is also possible to completely customise them.

Using built-in styles

To use a built in style, assign one of the static members of Style to your table's TableStyle property. For instance, here's how we'd apply the "Heavy" style to our table of the planets:

var table = Table<Planet>.Create(Planets.Take(3));
table.TableStyle = Style.Heavy;
Console.WriteLine(table.ToString());

NB: If you'd like to use one of the Unicode styles in a console application, you may need to set the encoding:

Console.OutputEncoding = Encoding.Unicode;

Built-in style gallery

Default

+---------+-----------------+---------------+
| Name    | DistanceFromSun | OrbitalPeriod |
+---------+-----------------+---------------+
| Mercury | 57909227        | 88            |
| Venus   | 108209475       | 225           |
| Earth   | 149598262       | 365.24        |
+---------+-----------------+---------------+

Heavy

#=========#=================#===============#
# Name    # DistanceFromSun # OrbitalPeriod #
#=========#=================#===============#
# Mercury # 57909227        # 88            #
# Venus   # 108209475       # 225           #
# Earth   # 149598262       # 365.24        #
#=========#=================#===============#

Dots

.............................................
: Name    : DistanceFromSun : OrbitalPeriod :
:.........:.................:...............:
: Mercury : 57909227        : 88            :
: Venus   : 108209475       : 225           :
: Earth   : 149598262       : 365.24        :
:.........:.................:...............:

UnicodeLines

┌─────────┬─────────────────┬───────────────┐
│ Name    │ DistanceFromSun │ OrbitalPeriod │
├─────────┼─────────────────┼───────────────┤
│ Mercury │ 57909227        │ 88            │
│ Venus   │ 108209475       │ 225           │
│ Earth   │ 149598262       │ 365.24        │
└─────────┴─────────────────┴───────────────┘

UnicodePipes

╔═════════╦═════════════════╦═══════════════╗
║ Name    ║ DistanceFromSun ║ OrbitalPeriod ║
╠═════════╬═════════════════╬═══════════════╣
║ Mercury ║ 57909227        ║ 88            ║
║ Venus   ║ 108209475       ║ 225           ║
║ Earth   ║ 149598262       ║ 365.24        ║
╚═════════╩═════════════════╩═══════════════╝

UnicodeArcs

╭─────────┬─────────────────┬───────────────╮
│ Name    │ DistanceFromSun │ OrbitalPeriod │
├─────────┼─────────────────┼───────────────┤
│ Mercury │ 57909227        │ 88            │
│ Venus   │ 108209475       │ 225           │
│ Earth   │ 149598262       │ 365.24        │
╰─────────┴─────────────────┴───────────────╯

Custom styles

If you'd like to create a completely custom style, you can do so in a couple of ways.

The first is a simple approach that specifies 3 characters: wall, floor, corner. This works like this:

table.TableStyle = new Style('|','=','*');

This will render as:

*=========*=================*===============*
| Name    | DistanceFromSun | OrbitalPeriod |
*=========*=================*===============*
| Mercury | 57909227        | 88            |
| Venus   | 108209475       | 225           |
| Earth   | 149598262       | 365.24        |
*=========*=================*===============*

The second approach is more verbose, as it involves specifying each of the 9 possible "corner" pieces individually:

table.TableStyle = new Style(
	'|',
	'-',
	new Corners
	{
		CornerBottomLeft  = '1',
		CornerBottomRight = '2',
		CornerTopLeft     = '3',
		CornerTopRight    = '4',
		Intersection      = '5',
		TeeNoDown         = '6',
		TeeNoLeft         = '7',
		TeeNoRight        = '8',
		TeeNoUp           = '9'
	}
);

This will render as:

3---------9-----------------9---------------4
| Name    | DistanceFromSun | OrbitalPeriod |
7---------5-----------------5---------------8
| Mercury | 57909227        | 88            |
| Venus   | 108209475       | 225           |
| Earth   | 149598262       | 365.24        |
1---------6-----------------6---------------2

Mixing and matching

Finally, it's worth pointing out that styles can be tweaked once set. Whether you start with a built-in style or a custom style, it is possible to change the value of any of the constituent characters. For example:

table.TableStyle = Style.Default;
table.TableStyle.Corners.TeeNoUp    = 'v';
table.TableStyle.Corners.TeeNoDown  = '^';
table.TableStyle.Corners.TeeNoLeft  = '>';
table.TableStyle.Corners.TeeNoRight = '<';

Which will result in:

+---------v-----------------v---------------+
| Name    | DistanceFromSun | OrbitalPeriod |
>---------+-----------------+---------------<
| Mercury | 57909227        | 88            |
| Venus   | 108209475       | 225           |
| Earth   | 149598262       | 365.24        |
+---------^-----------------^---------------+

Happy styling!