Skip to content

Commit 2356d7c

Browse files
authored
Merge pull request #118 from willmcgugan/table-justify
table fix
2 parents 03a5213 + fb5b4eb commit 2356d7c

File tree

9 files changed

+73
-28
lines changed

9 files changed

+73
-28
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.2.5] - 2020-06-23
9+
10+
### Fixed
11+
12+
- Fixed justify of tables
13+
814
## [2.2.4] - 2020-06-21
915

1016
### Added
1117

1218
- Added enable_link_path to RichHandler
13-
- Added legacy_windows switch to Console contstructor
19+
- Added legacy_windows switch to Console constructor
1420

1521
## [2.2.3] - 2020-06-15
1622

examples/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
table.add_row("Dec 16, 2016", "Rouge One: A Star Wars Story", "$1,332,439,889")
1919

2020
console = Console()
21-
console.print(table)
21+
console.print(table, justify="center")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/willmcgugan/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "2.2.4"
5+
version = "2.2.5"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <willmcgugan@gmail.com>"]
88
license = "MIT"

rich/_log_render.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Iterable, List, Optional, TYPE_CHECKING, Union
33

44

5-
from .text import Text
5+
from .text import Text, TextType
66

77
if TYPE_CHECKING:
88
from .console import Console, ConsoleRenderable, RenderableType
@@ -29,7 +29,7 @@ def __call__(
2929
renderables: Iterable["ConsoleRenderable"],
3030
log_time: datetime = None,
3131
time_format: str = None,
32-
level: Union[str, Text] = "",
32+
level: TextType = "",
3333
path: str = None,
3434
line_no: int = None,
3535
link_path: str = None,

rich/console.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from . import themes
4242
from .terminal_theme import TerminalTheme, DEFAULT_TERMINAL_THEME
4343
from .segment import Segment
44-
from .text import Text
44+
from .text import Text, TextType
4545
from .theme import Theme
4646

4747

@@ -950,12 +950,12 @@ def _render_buffer(self) -> str:
950950
return rendered
951951

952952
def input(
953-
self, prompt: Union[str, Text] = "", *, markup: bool = True, emoji: bool = True
953+
self, prompt: TextType = "", *, markup: bool = True, emoji: bool = True
954954
) -> str:
955955
"""Displays a prompt and waits for input from the user. The prompt may contain color / style.
956956
957957
Args:
958-
prompt (Union[Str, Text]): Text to render in the prompt.
958+
prompt (Union[str, Text]): Text to render in the prompt.
959959
markup (bool, optional): Enable console markup (requires a str prompt). Defaults to True.
960960
emoji (bool, optional): Enable emoji (requires a str prompt). Defaults to True.
961961

rich/table.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .protocol import is_renderable
2020
from .segment import Segment
2121
from .style import Style, StyleType
22-
from .text import Text
22+
from .text import Text, TextType
2323

2424
if TYPE_CHECKING:
2525
from .console import (
@@ -120,8 +120,8 @@ class Table(JupyterMixin):
120120
def __init__(
121121
self,
122122
*headers: Union[Column, str],
123-
title: Union[str, Text] = None,
124-
caption: Union[str, Text] = None,
123+
title: TextType = None,
124+
caption: TextType = None,
125125
width: int = None,
126126
box: Optional[box.Box] = box.HEAVY_HEAD,
127127
padding: PaddingDimensions = (0, 1),
@@ -346,22 +346,23 @@ def __rich_console__(
346346
widths = self._calculate_column_widths(console, max_width)
347347
table_width = sum(widths) + self._extra_width
348348

349-
def render_annotation(
350-
text: Union[Text, str], style: Union[str, Style]
351-
) -> "Lines":
352-
if isinstance(text, Text):
353-
render_text = text
354-
else:
355-
render_text = console.render_str(text, style=style)
356-
return render_text.wrap(console, table_width, justify="center")
349+
render_options = options.update(width=table_width)
350+
351+
def render_annotation(text: TextType, style: StyleType) -> "RenderResult":
352+
render_text = (
353+
console.render_str(text, style=style) if isinstance(text, str) else text
354+
)
355+
return console.render(
356+
render_text, options=render_options.update(justify="center")
357+
)
357358

358359
if self.title:
359-
yield render_annotation(
360+
yield from render_annotation(
360361
self.title, style=Style.pick_first(self.title_style, "table.title")
361362
)
362-
yield from self._render(console, options, widths)
363+
yield from self._render(console, render_options, widths)
363364
if self.caption:
364-
yield render_annotation(
365+
yield from render_annotation(
365366
self.caption,
366367
style=Style.pick_first(self.caption_style, "table.caption"),
367368
)

rich/text.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
_re_whitespace = re.compile(r"\s+$")
4040

41+
TextType = Union[str, "Text"]
42+
4143

4244
class Span(NamedTuple):
4345
"""A marked up region in some text."""

tests/test_log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def render_log():
2727

2828

2929
def test_log():
30-
expected = "\n\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0mHello from \x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:24\x1b[0m\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;34m1\x1b[0m, \x1b[1;34m2\x1b[0m, \x1b[1;34m3\x1b[0m\x1b[1m]\x1b[0m \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:25\x1b[0m\n \x1b[3m Locals \x1b[0m \n \x1b[34m╭─────────┬────────────────────────────────────────╮\x1b[0m \n \x1b[34m│\x1b[0m\x1b[32m'console'\x1b[0m\x1b[34m│\x1b[0m\x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m\x1b[34m│\x1b[0m \n \x1b[34m╰─────────┴────────────────────────────────────────╯\x1b[0m \n"
30+
expected = "\n\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0mHello from \x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:24\x1b[0m\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;34m1\x1b[0m, \x1b[1;34m2\x1b[0m, \x1b[1;34m3\x1b[0m\x1b[1m]\x1b[0m \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:25\x1b[0m\n \x1b[3m Locals \x1b[0m \n \x1b[34m╭─────────┬────────────────────────────────────────╮\x1b[0m \n \x1b[34m│\x1b[0m\x1b[32m'console'\x1b[0m\x1b[34m│\x1b[0m\x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m\x1b[34m│\x1b[0m \n \x1b[34m╰─────────┴────────────────────────────────────────╯\x1b[0m \n"
3131
assert render_log() == expected
3232

3333

tests/test_table.py

Lines changed: 41 additions & 5 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)