Skip to content

Commit

Permalink
rule
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Feb 21, 2020
1 parent 33361e1 commit 2dd136b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added Traceback rendering and handler
- Added rich.constrain
- Added rich.rule

### Fixed

Expand Down
30 changes: 9 additions & 21 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,34 +691,22 @@ def check_text() -> None:
check_text()
return renderables

def rule(self, title: str = "", character: str = "─") -> None:
def rule(
self,
title: str = "",
character: 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 "─".
"""
from .rule import Rule

from .text import Text

width = self.width

if not title:
self.print(Text(character * width, "rule.line"))
else:
title_text = Text.from_markup(title, "rule.text")
if len(title_text) > width - 4:
title_text.set_length(width - 4)

rule_text = Text()

center = (width - len(title_text)) // 2
rule_text.append(character * (center - 1) + " ", "rule.line")
rule_text.append(title_text)
rule_text.append(
" " + character * (width - len(rule_text) - 1), "rule.line"
)
self.print(rule_text)
rule = Rule(title=title, character=character, style=style)
self.print(rule)

def print(
self,
Expand Down
2 changes: 1 addition & 1 deletion rich/default_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"repr.bool_false": Style(color="bright_red", italic=True),
"repr.none": Style(color="magenta", italic=True),
"repr.url": Style(underline=True, color="bright_blue", bold=False),
"rule.line": Style(dim=True),
"rule.line": Style(dim=True, color="green"),
"rule.text": Style(),
"repr.path": Style(color="magenta"),
"repr.filename": Style(color="bright_magenta", bold=True),
Expand Down
3 changes: 2 additions & 1 deletion rich/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from .containers import Renderables
from .panel import Panel
from .rule import Rule
from .style import Style, StyleStack
from .syntax import Syntax
from .text import Lines, Text
Expand Down Expand Up @@ -218,7 +219,7 @@ class HorizontalRule(MarkdownElement):

def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
style = console.get_style("markdown.hr")
yield Segment(f'{"─" * options.max_width}\n', style)
yield Rule(style=style)


class ListElement(MarkdownElement):
Expand Down
39 changes: 34 additions & 5 deletions rich/rule.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from typing import Union

from .console import Console, ConsoleOptions, RenderResult
from .style import Style
from .text import Text


class Rule:
def __init__(self, title: str = "", character: str = "─") -> None:
def __init__(
self,
title: Union[str, Text] = "",
character: str = "─",
style: Union[str, Style] = "rule.line",
) -> None:
"""A console renderable to draw a horizontal rule (line).
Args:
title (str, optional): Text to render in the rule. Defaults to "".
character (str, optional): Character used to draw the line. Defaults to "─".
"""
self.title = title
self.character = character
self.style = style

def __repr__(self) -> str:
return f"Rule({self.title!r}, {self.character!r})"
Expand All @@ -14,17 +29,31 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
width = options.max_width

if not self.title:
yield Text(self.character * width, "rule.line")
yield Text(self.character * width, self.style)
else:
title_text = Text.from_markup(self.title, "rule.text")
if isinstance(self.title, Text):
title_text = self.title
else:
title_text = Text.from_markup(self.title, "rule.text")
if len(title_text) > width - 4:
title_text.set_length(width - 4)

rule_text = Text()
center = (width - len(title_text)) // 2
rule_text.append(self.character * (center - 1) + " ", "rule.line")
rule_text.append(self.character * (center - 1) + " ", self.rule_style)
rule_text.append(title_text)
rule_text.append(
" " + self.character * (width - len(rule_text) - 1), "rule.line"
" " + self.character * (width - len(rule_text) - 1), self.rule_style
)
yield rule_text


if __name__ == "__main__":
from .console import Console

c = Console()

c.print(Rule())
c.print(Rule("*Hello, World*"))
c.print(Rule(Text("Hello, World", "bold red reverse")))
c.print(Rule("*Hello, World*", character="."))

0 comments on commit 2dd136b

Please sign in to comment.