-
Notifications
You must be signed in to change notification settings - Fork 19
5. Handling long strings
Left to their own devices, long strings can break tables by making them wider than their container. This can cause wrapping or clipping depending on where the output goes. Neither is ideal, but wrapping is particularly bad.
Here's what we're trying to avoid:
╔═════════╦═════════════════╦══════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════╗
║ Name ║ DistanceFromSun ║ Fact
║
╠═════════╬═════════════════╬══════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════╣
║ Mars ║ 227943824 ║ Mars is also often described as the “Red Planet”
due to its reddish appearance. ║
║ Jupiter ║ 778340821 ║ Jupiter is the largest planet.
║
║ Saturn ║ 1426666422 ║ Saturn is best known for its fabulous ring system
that was first observed in 1610 by the astronomer Galileo Galilei. ║
╚═════════╩═════════════════╩══════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════╝
Basically garbage. No one wants that. 😢
Luckily, ConTabs has a couple of ways to mitigate this: in-cell wrapping and truncation. These can be activated per-column by setting the LongStringBehaviour
property.
If I want long strings to wrap onto a new line (within their cell) I can do this:
table.Columns["Fact"].LongStringBehaviour = LongStringBehaviour.Wrap;
Which results in:
╔═════════╦═════════════════╦═══════════════════════════╗
║ Name ║ DistanceFromSun ║ Fact ║
╠═════════╬═════════════════╬═══════════════════════════╣
║ Mars ║ 227943824 ║ Mars is also often ║
║ ║ ║ described as the “Red ║
║ ║ ║ Planet” due to its ║
║ ║ ║ reddish appearance. ║
║ Jupiter ║ 778340821 ║ Jupiter is the largest ║
║ ║ ║ planet. ║
║ Saturn ║ 1426666422 ║ Saturn is best known for ║
║ ║ ║ its fabulous ring system ║
║ ║ ║ that was first observed ║
║ ║ ║ in 1610 by the astronomer ║
║ ║ ║ Galileo Galilei. ║
╚═════════╩═════════════════╩═══════════════════════════╝
We can also set the width to something more reasonable:
table.Columns["Fact"].LongStringBehaviour.Width = 45;
Which results in:
╔═════════╦═════════════════╦═══════════════════════════════════════════════╗
║ Name ║ DistanceFromSun ║ Fact ║
╠═════════╬═════════════════╬═══════════════════════════════════════════════╣
║ Mars ║ 227943824 ║ Mars is also often described as the “Red ║
║ ║ ║ Planet” due to its reddish appearance. ║
║ Jupiter ║ 778340821 ║ Jupiter is the largest planet. ║
║ Saturn ║ 1426666422 ║ Saturn is best known for its fabulous ring ║
║ ║ ║ system that was first observed in 1610 by the ║
║ ║ ║ astronomer Galileo Galilei. ║
╚═════════╩═════════════════╩═══════════════════════════════════════════════╝
Alternatively, I can simply cut off the text after a certain point:
table.Columns["Fact"].LongStringBehaviour = LongStringBehaviour.Truncate;
Which looks like this:
╔═════════╦═════════════════╦═════════════════╗
║ Name ║ DistanceFromSun ║ Fact ║
╠═════════╬═════════════════╬═════════════════╣
║ Mars ║ 227943824 ║ Mars is also of ║
║ Jupiter ║ 778340821 ║ Jupiter is the ║
║ Saturn ║ 1426666422 ║ Saturn is best ║
╚═════════╩═════════════════╩═════════════════╝
Or, to provide a visual clue that truncation has taken place, we can add ellipsis. (Let's also change the width.)
table.Columns["Fact"].LongStringBehaviour = LongStringBehaviour.TruncateWithEllipsis;
table.Columns["Fact"].LongStringBehaviour.Width = 45;
Which looks like this:
╔═════════╦═════════════════╦═══════════════════════════════════════════════╗
║ Name ║ DistanceFromSun ║ Fact ║
╠═════════╬═════════════════╬═══════════════════════════════════════════════╣
║ Mars ║ 227943824 ║ Mars is also often described as the “Red P... ║
║ Jupiter ║ 778340821 ║ Jupiter is the largest planet. ║
║ Saturn ║ 1426666422 ║ Saturn is best known for its fabulous ring... ║
╚═════════╩═════════════════╩═══════════════════════════════════════════════╝