Skip to content

Commit

Permalink
table styles
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Oct 18, 2020
1 parent 2cfb923 commit 2a897aa
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions rich/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,12 @@ def __rich_measure__(self, console: "Console", max_width: int) -> Measurement:

extra_width = self._extra_width

max_width = sum(self._calculate_column_widths(console, max_width))
max_width = sum(self._calculate_column_widths(console, max_width - extra_width))

_measure_column = self._measure_column

measurements = [
_measure_column(console, column, max_width - extra_width)
for column in self.columns
_measure_column(console, column, max_width) for column in self.columns
]
minimum_width = (
sum(measurement.minimum for measurement in measurements) + extra_width
Expand Down Expand Up @@ -769,11 +768,16 @@ def _render(

if __name__ == "__main__": # pragma: no cover
from rich.console import Console
from rich.highlighter import ReprHighlighter
from rich.table import Table

table = Table(title="Star Wars Movies")
table = Table(
title="Star Wars Movies",
caption="Rich example table",
caption_justify="right",
)

table.add_column("Released", style="cyan", no_wrap=True)
table.add_column("Released", header_style="bright_cyan", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")
table.add_column("Box Office", justify="right", style="green")

Expand All @@ -782,5 +786,43 @@ def _render(
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")

def header(text) -> None:
console.print()
console.rule(highlight(text))
console.print()

console = Console()
highlight = ReprHighlighter()
header("Example Table")
console.print(table, justify="center")

table.expand = True
header("expand=True")
console.print(table, justify="center")

table.width = 50
header("width=50")

console.print(table, justify="center")

table.width = None
table.expand = False
table.row_styles = ["dim", "none"]
header("row_styles=['dim', 'none']")

console.print(table, justify="center")

table.width = None
table.expand = False
table.row_styles = ["dim", "none"]
table.leading = 1
header("leading=1, row_styles=['dim', 'none']")
console.print(table, justify="center")

table.width = None
table.expand = False
table.row_styles = ["dim", "none"]
table.show_lines = True
table.leading = 0
header("show_lines=True, row_styles=['dim', 'none']")
console.print(table, justify="center")

0 comments on commit 2a897aa

Please sign in to comment.