This commit is contained in:
Will McGugan 2020-03-02 17:29:32 +00:00
parent 9a98dec22b
commit 06279f7605
9 changed files with 26 additions and 30 deletions

View File

@ -44,7 +44,7 @@ The :meth:`~rich.console.Console.log` methods offers the same capabilities as pr
<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #7fbfbf">[16:32:08] </span>Hello, World! <span style="color: #7f7f7f">&lt;stdin&gt;:1</span>
</pre>
To help with debugging, the log() method has a ``log_locals`` parameter. If you set this to ``True`` Rich will display a table of local variables where the method was called.
To help with debugging, the log() method has a ``log_locals`` parameter. If you set this to ``True``, Rich will display a table of local variables where the method was called.
Exporting

View File

@ -20,8 +20,8 @@ For strikethrough, wrap the text in tildes. e.g. ``~this has a line through it~`
For code, wrap the text in backticks. e.g. ```import this```
Styles
------
Styling
-------
For other styles and color, you can use a syntax similar to bbcode. If you write the style (see :ref:`styles`) in square brackets, i.e. ``[bold red]``, that style will apply until it is *closed* with a corresponding ``[/bold red]``.

View File

@ -0,0 +1,6 @@
rich.theme
==========
.. automodule:: rich.theme
:members: Theme

View File

@ -66,9 +66,9 @@ You can parse a style definition explicitly with the :meth:`~rich.style.Style.pa
Style Themes
------------
If you re-use styles it can be a maintenance headache if you ever want to modify an attribute or color -- you would have to change every line where the style is used. Rich provides a :class:`rich.theme.Theme` class which you can use to pre-define styles, so if you ever need to modify a style you only need change one file.
If you re-use styles it can be a maintenance headache if you ever want to modify an attribute or color -- you would have to change every line where the style is used. Rich provides a :class:`rich.theme.Theme` class which you can use to define custom styles that you can refer to by name. That way you only need update your styles in one place.
Style themes can also make your code more semantic, for instance a style called ``"warning"`` better expresses intent that ``"italic magenta underline"``.
Style themes can make your code more semantic, for instance a style called ``"warning"`` better expresses intent that ``"italic magenta underline"``.
To use a style theme, construct a :class:`rich.theme.Theme` instance and pass it to the :class:`~rich.console.Console` constructor. Here's an example::
@ -92,6 +92,6 @@ If you prefer you can write your styles in an external config file rather than i
[styles]
info = dim cyan
warning = magenta
danger bold red
danger = bold red
You can read these files with the :method:`~rich.theme.Theme.read` method.
You can read these files with the :meth:`~rich.theme.Theme.read` method.

View File

@ -3,7 +3,7 @@ Tables
Rich's :class:`~rich.table.Table` class offers a variety ways of rendering tabular data to the terminal.
To render a table, construct a :class:`~rich.table.Table` object, add data, then call :meth:`~rich.console.Console.print` or :meth:`~rich.console.Console.log` to write it to the console.
To render a table, construct a :class:`~rich.table.Table` object, add columns with :meth:`~rich.table.Table.add_column`, and rows with :meth:`~rich.table.Table.add_row` -- then print it to the console.
Here's an example::

View File

@ -3,11 +3,11 @@ Rich Text
Rich has a :class:`~rich.text.Text` class for text that may be marked up with color and style attributes. You can consider this class to be like a mutable string with style information. The methods on the Text() instance are similar to a Python ``str`` but are designed to preserve any style information.
One way to add a style to Text is the :meth:`~tich.text.Text.styleize` method which applies a style to a start and end offset. Here is an example::
One way to add a style to Text is the :meth:`~tich.text.Text.stylize` method which applies a style to a start and end offset. Here is an example::
from rich.text import Text
text = Text("Hello, World!")
text.styleize(0, 6, "bold magenta")
text.stylize(0, 6, "bold magenta")
console.print(text)
This will print "Hello, World!" to the terminal, with the first word in bold magenta.

View File

@ -719,8 +719,6 @@ class Console:
Returns:
List[ConsoleRenderable]: A list of things to render.
"""
from .text import Text
sep_text = Text(sep, end=end)
renderables: List[ConsoleRenderable] = []
append = renderables.append
@ -753,7 +751,7 @@ class Console:
check_text()
append(Pretty(renderable, highlighter=_highlighter))
else:
append_text(_highlighter(repr(renderable)))
append_text(_highlighter(str(renderable)))
check_text()
return renderables
@ -788,7 +786,6 @@ class Console:
r"""Print to the console.
Args:
objects (positional args): Objects to log to the terminal.
sep (str, optional): String to write between print data. Defaults to " ".
end (str, optional): String to write at end of print data. Defaults to "\n".

View File

@ -19,7 +19,6 @@ if TYPE_CHECKING:
from .console import (
Console,
ConsoleOptions,
ConsoleRenderable,
JustifyValues,
RenderableType,
RenderResult,
@ -147,8 +146,8 @@ class Table:
def add_column(
self,
header: Union[str, "ConsoleRenderable"] = "",
footer: Union[str, "ConsoleRenderable"] = "",
header: "RenderableType" = "",
footer: "RenderableType" = "",
header_style: Union[str, Style] = None,
footer_style: Union[str, Style] = None,
style: Union[str, Style] = None,
@ -160,9 +159,9 @@ class Table:
"""Add a column to the table.
Args:
header (Union[str, ConsoleRenderable], optional): Text or renderable for the header.
header (RenderableType, optional): Text or renderable for the header.
Defaults to "".
footer (Union[str, ConsoleRenderable], optional): Text or renderable for the footer.
footer (RenderableType, optional): Text or renderable for the footer.
Defaults to "".
header_style (Union[str, Style], optional): Style for the header. Defaults to "none".
footer_style (Union[str, Style], optional): Style for the header. Defaults to "none".
@ -196,7 +195,6 @@ class Table:
Raises:
errors.NotRenderableError: If you add something that can't be rendered.
"""
from .console import ConsoleRenderable
def add_cell(column: Column, renderable: "RenderableType") -> None:
column._cells.append(renderable)
@ -456,7 +454,7 @@ if __name__ == "__main__": # pragma: no cover
from .console import Console
from . import box
c = Console(markup=False)
c = Console(markup=True)
table = Table(
Column(
"Foo", footer=Text("Total", justify="right"), footer_style="bold", ratio=1

View File

@ -14,21 +14,16 @@ class Theme:
"""
def __init__(self, styles: Dict[str, Style] = None, inherit: bool = True):
if inherit:
self.styles = DEFAULT_STYLES.copy()
else:
self.styles = {}
self.styles = DEFAULT_STYLES.copy() if inherit else {}
if styles is not None:
self.styles.update(styles)
@property
def config(self) -> str:
"""Get contents of a config file for this theme."""
config_lines = ["[styles]"]
append = config_lines.append
for name, style in sorted(self.styles.items()):
append(f"{name} = {style}")
config = "\n".join(config_lines)
config = "[styles]\n" + "\n".join(
f"{name} = {style}" for name, style in sorted(self.styles.items())
)
return config
@classmethod