Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Mar 2, 2020
1 parent 9a98dec commit 06279f7
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/source/console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/source/markup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]``.

Expand Down
6 changes: 6 additions & 0 deletions docs/source/reference/theme.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rich.theme
==========

.. automodule:: rich.theme
:members: Theme

8 changes: 4 additions & 4 deletions docs/source/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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.
2 changes: 1 addition & 1 deletion docs/source/tables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
4 changes: 2 additions & 2 deletions docs/source/text.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,6 @@ def _collect_renderables(
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
Expand Down Expand Up @@ -753,7 +751,7 @@ def check_text() -> None:
check_text()
append(Pretty(renderable, highlighter=_highlighter))
else:
append_text(_highlighter(repr(renderable)))
append_text(_highlighter(str(renderable)))

check_text()
return renderables
Expand Down Expand Up @@ -788,7 +786,6 @@ def print(
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".
Expand Down
12 changes: 5 additions & 7 deletions rich/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from .console import (
Console,
ConsoleOptions,
ConsoleRenderable,
JustifyValues,
RenderableType,
RenderResult,
Expand Down Expand Up @@ -147,8 +146,8 @@ def padding(self, padding: PaddingDimensions) -> "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,
Expand All @@ -160,9 +159,9 @@ def add_column(
"""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".
Expand Down Expand Up @@ -196,7 +195,6 @@ def add_row(self, *renderables: Optional["RenderableType"]) -> None:
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)
Expand Down Expand Up @@ -456,7 +454,7 @@ def _render(
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
Expand Down
13 changes: 4 additions & 9 deletions rich/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 06279f7

Please sign in to comment.