Skip to content

Commit

Permalink
Merge pull request #118 from willmcgugan/table-justify
Browse files Browse the repository at this point in the history
table fix
  • Loading branch information
willmcgugan authored Jun 23, 2020
2 parents 03a5213 + fb5b4eb commit 2356d7c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 28 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.2.5] - 2020-06-23

### Fixed

- Fixed justify of tables

## [2.2.4] - 2020-06-21

### Added

- Added enable_link_path to RichHandler
- Added legacy_windows switch to Console contstructor
- Added legacy_windows switch to Console constructor

## [2.2.3] - 2020-06-15

Expand Down
2 changes: 1 addition & 1 deletion examples/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
table.add_row("Dec 16, 2016", "Rouge One: A Star Wars Story", "$1,332,439,889")

console = Console()
console.print(table)
console.print(table, justify="center")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rich"
homepage = "https://github.com/willmcgugan/rich"
documentation = "https://rich.readthedocs.io/en/latest/"
version = "2.2.4"
version = "2.2.5"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <[email protected]>"]
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions rich/_log_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Iterable, List, Optional, TYPE_CHECKING, Union


from .text import Text
from .text import Text, TextType

if TYPE_CHECKING:
from .console import Console, ConsoleRenderable, RenderableType
Expand All @@ -29,7 +29,7 @@ def __call__(
renderables: Iterable["ConsoleRenderable"],
log_time: datetime = None,
time_format: str = None,
level: Union[str, Text] = "",
level: TextType = "",
path: str = None,
line_no: int = None,
link_path: str = None,
Expand Down
6 changes: 3 additions & 3 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from . import themes
from .terminal_theme import TerminalTheme, DEFAULT_TERMINAL_THEME
from .segment import Segment
from .text import Text
from .text import Text, TextType
from .theme import Theme


Expand Down Expand Up @@ -950,12 +950,12 @@ def _render_buffer(self) -> str:
return rendered

def input(
self, prompt: Union[str, Text] = "", *, markup: bool = True, emoji: bool = True
self, prompt: TextType = "", *, markup: bool = True, emoji: bool = True
) -> str:
"""Displays a prompt and waits for input from the user. The prompt may contain color / style.
Args:
prompt (Union[Str, Text]): Text to render in the prompt.
prompt (Union[str, Text]): Text to render in the prompt.
markup (bool, optional): Enable console markup (requires a str prompt). Defaults to True.
emoji (bool, optional): Enable emoji (requires a str prompt). Defaults to True.
Expand Down
29 changes: 15 additions & 14 deletions rich/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .protocol import is_renderable
from .segment import Segment
from .style import Style, StyleType
from .text import Text
from .text import Text, TextType

if TYPE_CHECKING:
from .console import (
Expand Down Expand Up @@ -120,8 +120,8 @@ class Table(JupyterMixin):
def __init__(
self,
*headers: Union[Column, str],
title: Union[str, Text] = None,
caption: Union[str, Text] = None,
title: TextType = None,
caption: TextType = None,
width: int = None,
box: Optional[box.Box] = box.HEAVY_HEAD,
padding: PaddingDimensions = (0, 1),
Expand Down Expand Up @@ -346,22 +346,23 @@ def __rich_console__(
widths = self._calculate_column_widths(console, max_width)
table_width = sum(widths) + self._extra_width

def render_annotation(
text: Union[Text, str], style: Union[str, Style]
) -> "Lines":
if isinstance(text, Text):
render_text = text
else:
render_text = console.render_str(text, style=style)
return render_text.wrap(console, table_width, justify="center")
render_options = options.update(width=table_width)

def render_annotation(text: TextType, style: StyleType) -> "RenderResult":
render_text = (
console.render_str(text, style=style) if isinstance(text, str) else text
)
return console.render(
render_text, options=render_options.update(justify="center")
)

if self.title:
yield render_annotation(
yield from render_annotation(
self.title, style=Style.pick_first(self.title_style, "table.title")
)
yield from self._render(console, options, widths)
yield from self._render(console, render_options, widths)
if self.caption:
yield render_annotation(
yield from render_annotation(
self.caption,
style=Style.pick_first(self.caption_style, "table.caption"),
)
Expand Down
2 changes: 2 additions & 0 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

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

TextType = Union[str, "Text"]


class Span(NamedTuple):
"""A marked up region in some text."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def render_log():


def test_log():
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"
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"
assert render_log() == expected


Expand Down
46 changes: 41 additions & 5 deletions tests/test_table.py

Large diffs are not rendered by default.

0 comments on commit 2356d7c

Please sign in to comment.