mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 18:18:22 +00:00
docs and rule
This commit is contained in:
parent
6d7ba589e2
commit
f9ac1c2f54
8 changed files with 21 additions and 13 deletions
|
@ -5,6 +5,12 @@ 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.0.2] - Unreleased
|
||||
|
||||
### Changed
|
||||
|
||||
- Truncated overly long text in Rule with ellipsis overflow
|
||||
|
||||
## [2.0.1] - 2020-06-10
|
||||
|
||||
### Added
|
||||
|
|
|
@ -39,7 +39,7 @@ Setting the highlighter on the Console will apply highlighting to all text you p
|
|||
console.print(highlight_emails("Send funds to money@example.org"))
|
||||
|
||||
|
||||
While :class:`~rich.highlighter.RegexHighlighter` is quite powerful, you can also extend its base class :class:`~rich.highlighter.Highlighter` which you can use to implement any scheme for highlighting. It contains a single method :class:`~rich.highlighter.Highlighter.highlight` which is passed the :class:`~rich.text.Text` to highlight.
|
||||
While :class:`~rich.highlighter.RegexHighlighter` is quite powerful, you can also extend its base class :class:`~rich.highlighter.Highlighter` to implement a custom scheme for highlighting. It contains a single method :class:`~rich.highlighter.Highlighter.highlight` which is passed the :class:`~rich.text.Text` to highlight.
|
||||
|
||||
Here's a silly example that highlights every character with a different color::
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ The ``__rich_console__`` method should accept a :class:`~rich.console.Console` a
|
|||
|
||||
Here's an example of a ``__rich_console__`` method::
|
||||
|
||||
from dataclasses import dataclass
|
||||
from rich.console import Console, ConsoleOptions, RenderResult
|
||||
from rich.table import Table
|
||||
|
||||
|
|
|
@ -81,7 +81,8 @@ Style Class
|
|||
Ultimately the style definition is parsed and an instance of a :class:`~rich.style.Style` class is created. If you prefer, you can use the Style class in place of the style definition. Here's an example::
|
||||
|
||||
from rich.style import Style
|
||||
console.print("Danger, Will Robinson!", style=Style(color="red", blink=True, bold=True)
|
||||
danger_style = Style(color="red", blink=True, bold=True)
|
||||
console.print("Danger, Will Robinson!", style=danger_style)
|
||||
|
||||
It is slightly quicker to construct a Style class like this, since a style definition takes a little time to parse -- but only on the first call, as Rich will cache parsed style definitions.
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from typing import Union
|
||||
|
||||
from .cells import cell_len
|
||||
from .cells import cell_len, set_cell_size
|
||||
from .console import Console, ConsoleOptions, RenderResult
|
||||
from .jupyter import JupyterMixin
|
||||
from .segment import Segment
|
||||
|
@ -47,8 +47,10 @@ class Rule(JupyterMixin):
|
|||
title_text = console.render_str(self.title, style="rule.text")
|
||||
|
||||
if cell_len(title_text.plain) > width - 4:
|
||||
title_text.set_length(width - 4)
|
||||
title_text.truncate(width - 4, overflow="ellipsis")
|
||||
|
||||
title_text.plain = title_text.plain.replace("\n", " ")
|
||||
title_text = title_text.tabs_to_spaces()
|
||||
rule_text = Text()
|
||||
center = (width - cell_len(title_text.plain)) // 2
|
||||
rule_text.append(character * (center - 1) + " ", self.style)
|
||||
|
|
|
@ -15,6 +15,8 @@ StyleType = Union[str, "Style"]
|
|||
class _Bit:
|
||||
"""A descriptor to get/set a style attribute bit."""
|
||||
|
||||
__slots__ = ["bit"]
|
||||
|
||||
def __init__(self, bit_no: int) -> None:
|
||||
self.bit = 1 << bit_no
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ class Theme:
|
|||
|
||||
Args:
|
||||
styles (Dict[str, Style], optional): A mapping of style names on to styles. Defaults to None for empty styles.
|
||||
inherit (bool, optional): Switch to inherit default styles. Defaults to True.
|
||||
inherit (bool, optional): Inherit default styles. Defaults to True.
|
||||
"""
|
||||
|
||||
def __init__(self, styles: Mapping[str, StyleType] = None, inherit: bool = True):
|
||||
|
@ -40,7 +40,7 @@ class Theme:
|
|||
Args:
|
||||
config_file (IO[str]): An open conf file.
|
||||
source (str, optional): The filename of the open file. Defaults to None.
|
||||
inherit (bool, optional): Switch to inherit default styles. Defaults to True.
|
||||
inherit (bool, optional): Inherit default styles. Defaults to True.
|
||||
|
||||
Returns:
|
||||
Theme: A New theme instance.
|
||||
|
@ -57,7 +57,7 @@ class Theme:
|
|||
|
||||
Args:
|
||||
path (str): Path to a config file readable by Python configparser module.
|
||||
inherit (bool, optional): Switch to inherit default styles. Defaults to True.
|
||||
inherit (bool, optional): Inherit default styles. Defaults to True.
|
||||
|
||||
Returns:
|
||||
Theme: A new theme instance.
|
||||
|
@ -67,9 +67,5 @@ class Theme:
|
|||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
from .console import Console
|
||||
from .markup import escape
|
||||
|
||||
console = Console()
|
||||
theme = Theme()
|
||||
console.print(escape(theme.config))
|
||||
print(theme.config)
|
||||
|
|
|
@ -13,7 +13,7 @@ def test_rule():
|
|||
console.rule("foo")
|
||||
console.rule(Text("foo", style="bold"))
|
||||
console.rule("foobarbazeggfoobarbazegg")
|
||||
expected = "\x1b[38;5;10m────────────────\x1b[0m\n\x1b[38;5;10m───── \x1b[0mfoo\x1b[38;5;10m ──────\x1b[0m\n\x1b[38;5;10m───── \x1b[0m\x1b[1mfoo\x1b[0m\x1b[38;5;10m ──────\x1b[0m\n\x1b[38;5;10m─ \x1b[0mfoobarbazegg\x1b[38;5;10m ─\x1b[0m\n"
|
||||
expected = "\x1b[38;5;10m────────────────\x1b[0m\n\x1b[38;5;10m───── \x1b[0mfoo\x1b[38;5;10m ──────\x1b[0m\n\x1b[38;5;10m───── \x1b[0m\x1b[1mfoo\x1b[0m\x1b[38;5;10m ──────\x1b[0m\n\x1b[38;5;10m─ \x1b[0mfoobarbazeg…\x1b[38;5;10m ─\x1b[0m\n"
|
||||
assert console.file.getvalue() == expected
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue