Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multiple characters for rule.py #207

Merged
merged 21 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,18 +795,18 @@ def rule(
self,
title: str = "",
*,
character: str = "─",
characters: str = "─",
style: Union[str, Style] = "rule.line",
) -> None:
"""Draw a line with optional centered title.

Args:
title (str, optional): Text to render over the rule. Defaults to "".
character (str, optional): Character to form the line. Defaults to "─".
characters (str, optional): Character(s) to form the line. Defaults to "─".
"""
from .rule import Rule

rule = Rule(title=title, character=character, style=style)
rule = Rule(title=title, characters=characters, style=style)
self.print(rule)

def control(self, control_codes: Union["Control", str]) -> None:
Expand Down
44 changes: 29 additions & 15 deletions rich/rule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Union

from .cells import cell_len
from .cells import cell_len, get_character_cell_size as get_char_size, set_cell_size
hedyhli marked this conversation as resolved.
Show resolved Hide resolved
from .console import Console, ConsoleOptions, RenderResult
from .jupyter import JupyterMixin
from .style import Style
Expand All @@ -12,7 +12,7 @@ class Rule(JupyterMixin):

Args:
title (Union[str, Text], optional): Text to render in the rule. Defaults to "".
character (str, optional): Character used to draw the line. Defaults to "─".
characters (str, optional): Character(s) used to draw the line. Defaults to "─".
hedyhli marked this conversation as resolved.
Show resolved Hide resolved
style (StyleType, optional): Style of Rule. Defaults to "rule.line".
end (str, optional): Character at end of Rule. defaults to "\\n"
"""
Expand All @@ -21,29 +21,37 @@ def __init__(
self,
title: Union[str, Text] = "",
*,
character: str = None,
characters: str = "─",
style: Union[str, Style] = "rule.line",
end: str = "\n",
) -> None:
if character and cell_len(character) != 1:
raise ValueError("'character' argument must have a cell width of 1")
if cell_len(characters) < 1:
raise ValueError(
"'characters' argument must have at least a cell width of 1"
)
self.title = title
self.character = character
self.characters = characters
self.style = style
self.end = end

def __repr__(self) -> str:
return f"Rule({self.title!r}, {self.character!r})"
return f"Rule({self.title!r}, {self.characters!r})"

def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
width = options.max_width

character = self.character or "─"
characters = self.characters or "─"

if cell_len(characters) == 1:
chars_len = get_char_size(characters)
else:
chars_len = 0
for i in list(characters):
hedyhli marked this conversation as resolved.
Show resolved Hide resolved
chars_len += get_char_size(i)
if not self.title:
yield Text(character * width, self.style)
yield Text(characters * (width // chars_len), self.style)
hedyhli marked this conversation as resolved.
Show resolved Hide resolved
else:
if isinstance(self.title, Text):
title_text = self.title
Expand All @@ -56,12 +64,18 @@ def __rich_console__(
title_text.plain = title_text.plain.replace("\n", " ")
title_text = title_text.tabs_to_spaces()
rule_text = Text(end=self.end)
center = (width - cell_len(title_text.plain)) // 2
rule_text.append(character * (center - 1) + " ", self.style)
side_width = (width - cell_len(title_text.plain)) // 2
if chars_len == 1:
side = Text(characters * side_width)
else:
side = Text(characters * (side_width // (chars_len - 1)))
side.truncate(side_width - 1)
rule_text.append(str(side) + " ", self.style)
rule_text.append(title_text)
rule_text.append(
" " + character * (width - cell_len(rule_text.plain) - 1), self.style
)
rule_text.append(" " + str(side), self.style)
if len(rule_text) < width:
rule_text.append(characters[0], self.style)
hedyhli marked this conversation as resolved.
Show resolved Hide resolved
rule_text.plain = set_cell_size(rule_text.plain, width)
yield rule_text


Expand All @@ -74,4 +88,4 @@ def __rich_console__(
except IndexError:
text = "Hello, World"
console = Console()
console.print(Rule(text))
console.print(Rule(title=text))
Loading