[BUG] Ellipsize last column in a table #4098
Hello, I'm trying to create a table where the last column ellipsizes. First tryThe last column doesn't ellipsize, instead it wraps. Let's try again with Second tryThe last column does ellipsize, however all the other columns are useless. Is it a bug in the way rich allocates the width? Expected outputWhat I'm trying to achieve is that: Ie. the last column should display as much as it can of the text, and ellipsize the rest. Other columns shouldn't be affected and should have enough width to display their content. Am I doing it wrong, or is there a bug with column width allocation? Platform I'm running Linux Debian unstable. DetailsIf you're using Rich in a terminal: Thanks |
Replies: 1 comment 1 reply
|
If you disable wrapping, it increases the chances it will overflow. But then the table will try to fit the entire line in, and you've created constraints that are impossible to satisfy. If you want this behavior, you could extend the Text class. Something like this. #!/usr/bin/python3
from rich.console import Console
from rich.table import Table
from rich.text import Text
class SingleLineText(Text):
def __rich_measure__(self, console, options):
return super().__rich_measure__(console, options).with_minimum(options.max_width)
console = Console()
table = Table()
table.add_column("Name")
table.add_column("Age")
table.add_column("Country")
table.add_column("Biography",)
table.add_row("Louis", "14", "France", SingleLineText("The quick brown fox jumps over the lazy dog. " * 2, no_wrap=True, overflow="ellipsis"))
console.print(table) |
overflow="ellipsis"defines what will happen when the text overflows (i.e. doesn't fit). If it can wrap onto the following line, it likely won't overflow (unless you have a very large word).If you disable wrapping, it increases the chances it will overflow. But then the table will try to fit the entire line in, and you've created constraints that are impossible to satisfy.
If you want this behavior, you could extend the Text class. Something like this.