2020-06-06 14:34:59 +00:00
|
|
|
# encoding=utf-8
|
|
|
|
|
|
|
|
import io
|
|
|
|
|
|
|
|
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.table import Table
|
2020-06-23 10:40:31 +00:00
|
|
|
from rich.text import Text
|
2020-06-06 14:34:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render_tables():
|
2020-06-23 10:40:31 +00:00
|
|
|
console = Console(
|
|
|
|
width=60,
|
|
|
|
force_terminal=True,
|
|
|
|
file=io.StringIO(),
|
|
|
|
legacy_windows=False,
|
|
|
|
color_system="truecolor",
|
|
|
|
)
|
2020-06-06 14:34:59 +00:00
|
|
|
|
2020-06-23 10:40:31 +00:00
|
|
|
table = Table(title="test table", caption="table caption", expand=True)
|
|
|
|
table.add_column("foo", footer=Text("total"), no_wrap=True, overflow="ellipsis")
|
2020-06-06 14:34:59 +00:00
|
|
|
table.add_column("bar", justify="center")
|
|
|
|
table.add_column("baz", justify="right")
|
|
|
|
|
|
|
|
table.add_row("Averlongwordgoeshere", "banana pancakes", None)
|
|
|
|
for width in range(10, 60, 5):
|
|
|
|
console.print(table, width=width)
|
|
|
|
|
2020-06-23 10:40:31 +00:00
|
|
|
table.expand = False
|
|
|
|
console.print(table, justify="left")
|
|
|
|
console.print(table, justify="center")
|
|
|
|
console.print(table, justify="right")
|
|
|
|
|
|
|
|
assert table.row_count == 1
|
|
|
|
|
|
|
|
table.row_styles = ["red", "yellow"]
|
|
|
|
table.add_row("Coffee", "Chocolate")
|
|
|
|
|
|
|
|
assert table.row_count == 2
|
|
|
|
|
|
|
|
console.print(table)
|
|
|
|
|
|
|
|
table.show_lines = True
|
|
|
|
console.print(table)
|
|
|
|
|
|
|
|
table.show_footer = True
|
|
|
|
console.print(table)
|
|
|
|
|
|
|
|
table.show_edge = False
|
|
|
|
|
|
|
|
console.print(table)
|
|
|
|
|
|
|
|
table.padding = 1
|
|
|
|
console.print(table)
|
|
|
|
|
|
|
|
table.width = 20
|
|
|
|
console.print(table)
|
|
|
|
|
2020-06-06 14:34:59 +00:00
|
|
|
return console.file.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
def test_render_table():
|
2020-06-23 10:40:31 +00:00
|
|
|
expected = "\x1b[3mtest table\x1b[0m\n┏━━┳━━┳━━┓\n┃\x1b[1m \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m \x1b[0m┃\n┡━━╇━━╇━━┩\n│ │ │ │\n└──┴──┴──┘\n\x1b[2;3m table \x1b[0m\n\x1b[2;3m caption \x1b[0m\n\x1b[3m test table \x1b[0m\n┏━━━━━┳━━━━┳━━┓\n┃\x1b[1m \x1b[0m\x1b[1mfoo\x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mb…\x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m \x1b[0m┃\n┡━━━━━╇━━━━╇━━┩\n│ Av… │ b… │ │\n└─────┴────┴──┘\n\x1b[2;3m table caption \x1b[0m\n\x1b[3m test table \x1b[0m\n┏━━━━━━━━┳━━━━━┳━━━┓\n┃\x1b[1m \x1b[0m\x1b[1mfoo \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mbar\x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m…\x1b[0m\x1b[1m \x1b[0m┃\n┡━━━━━━━━╇━━━━━╇━━━┩\n│ Averl… │ ba… │ │\n└────────┴─────┴───┘\n\x1b[2;3m table caption \x1b[0m\n\x1b[3m test table \x1b[0m\n┏━━━━━━━━━━━━┳━━━━━━┳━━━┓\n┃\x1b[1m \x1b[0m\x1b[1mfoo \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mbar \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m…\x1b[0m\x1b[1m \x1b[0m┃\n┡━━━━━━━━━━━━╇━━━━━━╇━━━┩\n│ Averlongw… │ ban… │ │\n└────────────┴──────┴───┘\n\x1b[2;3m table caption \x1b[0m\n\x1b[3m test table \x1b[0m\n┏━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━┓\n┃\x1b[1m \x1b[0m\x1b[1mfoo \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m bar \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mb…\x1b[0m\x1b[1m \x1b[0m┃\n┡━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━┩\n│ Averlongword… │ bana… │ │\n└───────────────┴───────┴────┘\n\x1b[2;3m table caption \x1b[0m\n\x1b[3m test table \x1b[0m\n┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━┓\n┃\x1b[1m \x1b[0m\x1b[1mfoo \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m bar \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mb…\x1b[0m\x1b[1m \x1b[0m┃\n┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━┩\n│ Averlongwordgoe… │ banana… │ │\n└──────────────────┴─────────┴────┘\n\x1b[2;3m table caption \x1b[0m\n\x1b[3m test table \x1b[0m\n┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃\x1b[1m \x1b[0m\x1b[1mfoo \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m bar \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mbaz\x1b[0m\x1b[1m \x1b[0m┃\n┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshe… │ banana │ │\n│ │ pancakes │ │\n└─────────────────────┴──────────┴─────┘\n\x1b[2;3m table caption \x1b[0m\n\x1b[3m test table \x1b[0m\n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━┓\n┃\x1b[1m \x1b[0m\x1b[1mfoo \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m bar \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mbaz\x1b[0m\x1b[1m \x1b[0m┃\n┡━━━━━━━━━━━━━━━━━━━━━━
|
2020-06-06 14:34:59 +00:00
|
|
|
assert render_tables() == expected
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
render = render_tables()
|
|
|
|
print(render)
|
|
|
|
print(repr(render))
|