mirror of https://github.com/Textualize/rich.git
typing fixes
This commit is contained in:
parent
bee681d359
commit
356bdf8d2a
|
@ -2,6 +2,7 @@
|
||||||
.vscode
|
.vscode
|
||||||
mypy_report
|
mypy_report
|
||||||
docs/build
|
docs/build
|
||||||
|
docs/source/_build
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
|
@ -56,50 +56,3 @@ class LogRender:
|
||||||
|
|
||||||
output.add_row(*row)
|
output.add_row(*row)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__": # pragma: no cover
|
|
||||||
from .console import Console
|
|
||||||
|
|
||||||
console = Console()
|
|
||||||
print(console)
|
|
||||||
logger = Logger(console)
|
|
||||||
|
|
||||||
from .markdown import Markdown
|
|
||||||
from .syntax import Syntax
|
|
||||||
|
|
||||||
s = Syntax(
|
|
||||||
'''\
|
|
||||||
@classmethod
|
|
||||||
def get(cls, renderable: RenderableType, max_width: int) -> RenderWidth:
|
|
||||||
"""Get desired width for a renderable."""
|
|
||||||
if hasattr(renderable, "__console__"):
|
|
||||||
get_console_width = getattr(renderable, "__console_width__", None)
|
|
||||||
if get_console_width is not None:
|
|
||||||
render_width = get_console_width(max_width).with_maximum(max_width)
|
|
||||||
return render_width.normalize()
|
|
||||||
else:
|
|
||||||
return RenderWidth(1, max_width)
|
|
||||||
elif isinstance(renderable, Segment):
|
|
||||||
text, _style = renderable
|
|
||||||
width = min(max_width, len(text))
|
|
||||||
return RenderWidth(width, width)
|
|
||||||
elif isinstance(renderable, str):
|
|
||||||
text = renderable.rstrip()
|
|
||||||
return RenderWidth(len(text), len(text))
|
|
||||||
else:
|
|
||||||
raise errors.NotRenderableError(
|
|
||||||
f"Unable to get render width for {renderable!r}; "
|
|
||||||
"a str, Segment, or object with __console__ method is required"
|
|
||||||
)
|
|
||||||
''',
|
|
||||||
"python",
|
|
||||||
theme="monokai",
|
|
||||||
)
|
|
||||||
|
|
||||||
logger(
|
|
||||||
"Hello", path="foo.py", line_no=20,
|
|
||||||
)
|
|
||||||
logger(
|
|
||||||
"World!", path="foo.py", line_no=20,
|
|
||||||
)
|
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple, TYPE_CHECKING
|
||||||
|
|
||||||
|
from . import errors
|
||||||
from .segment import Segment
|
from .segment import Segment
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .console import RenderableType
|
||||||
|
|
||||||
|
|
||||||
class RenderWidth(NamedTuple):
|
class RenderWidth(NamedTuple):
|
||||||
"""Range of widths for a renderable object."""
|
"""Range of widths for a renderable object."""
|
||||||
|
|
|
@ -48,7 +48,7 @@ from .segment import Segment
|
||||||
if TYPE_CHECKING: # pragma: no cover
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
from .text import Text
|
from .text import Text
|
||||||
|
|
||||||
HighlighterType = Callable[[Union[str, Text]], Text]
|
HighlighterType = Callable[[Union[str, "Text"]], "Text"]
|
||||||
|
|
||||||
JustifyValues = Optional[Literal["left", "center", "right", "full"]]
|
JustifyValues = Optional[Literal["left", "center", "right", "full"]]
|
||||||
|
|
||||||
|
@ -570,14 +570,14 @@ class Console:
|
||||||
append_text(renderable)
|
append_text(renderable)
|
||||||
elif isinstance(renderable, (int, float, bool, bytes, type(None))):
|
elif isinstance(renderable, (int, float, bool, bytes, type(None))):
|
||||||
append_text(
|
append_text(
|
||||||
highlight(repr(renderable)) if highlight else repr(renderable)
|
highlight(repr(renderable)) if highlight else Text(repr(renderable))
|
||||||
)
|
)
|
||||||
elif isinstance(renderable, (Mapping, Sequence)):
|
elif isinstance(renderable, (Mapping, Sequence)):
|
||||||
check_text()
|
check_text()
|
||||||
append(Pretty(renderable))
|
append(Pretty(renderable))
|
||||||
else:
|
else:
|
||||||
append_text(
|
append_text(
|
||||||
highlight(repr(renderable)) if highlight else repr(renderable)
|
highlight(repr(renderable)) if highlight else Text(repr(renderable))
|
||||||
)
|
)
|
||||||
|
|
||||||
check_text()
|
check_text()
|
||||||
|
|
|
@ -8,6 +8,7 @@ from .console import (
|
||||||
Console,
|
Console,
|
||||||
ConsoleOptions,
|
ConsoleOptions,
|
||||||
ConsoleRenderable,
|
ConsoleRenderable,
|
||||||
|
JustifyValues,
|
||||||
RenderResult,
|
RenderResult,
|
||||||
Segment,
|
Segment,
|
||||||
)
|
)
|
||||||
|
@ -77,8 +78,7 @@ class MarkdownElement:
|
||||||
def __console__(
|
def __console__(
|
||||||
self, console: "Console", options: "ConsoleOptions"
|
self, console: "Console", options: "ConsoleOptions"
|
||||||
) -> "RenderResult":
|
) -> "RenderResult":
|
||||||
return
|
return ()
|
||||||
yield
|
|
||||||
|
|
||||||
|
|
||||||
class UnknownElement(MarkdownElement):
|
class UnknownElement(MarkdownElement):
|
||||||
|
@ -113,12 +113,13 @@ class Paragraph(TextElement):
|
||||||
"""A Paragraph."""
|
"""A Paragraph."""
|
||||||
|
|
||||||
style_name = "markdown.paragraph"
|
style_name = "markdown.paragraph"
|
||||||
|
justify: JustifyValues
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, markdown: "Markdown", node) -> "Paragraph":
|
def create(cls, markdown: "Markdown", node) -> "Paragraph":
|
||||||
return cls(justify=markdown.justify)
|
return cls(justify=markdown.justify or "left")
|
||||||
|
|
||||||
def __init__(self, justify: str) -> None:
|
def __init__(self, justify: JustifyValues) -> None:
|
||||||
self.justify = justify
|
self.justify = justify
|
||||||
|
|
||||||
def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
|
def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
|
||||||
|
@ -348,7 +349,7 @@ class Markdown:
|
||||||
self,
|
self,
|
||||||
markup: str,
|
markup: str,
|
||||||
code_theme: str = "monokai",
|
code_theme: str = "monokai",
|
||||||
justify: str = None,
|
justify: JustifyValues = None,
|
||||||
style: Union[str, Style] = "none",
|
style: Union[str, Style] = "none",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Parses the markup."""
|
"""Parses the markup."""
|
||||||
|
|
|
@ -71,7 +71,7 @@ class Panel:
|
||||||
if self.expand:
|
if self.expand:
|
||||||
return RenderWidth(max_width, max_width)
|
return RenderWidth(max_width, max_width)
|
||||||
width = RenderWidth.get(self.renderable, max_width - 2).maximum + 2
|
width = RenderWidth.get(self.renderable, max_width - 2).maximum + 2
|
||||||
return RenderWidth.get(width, width)
|
return RenderWidth(width, width)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -23,6 +23,11 @@ class _Bit:
|
||||||
class Style:
|
class Style:
|
||||||
"""A terminal style."""
|
"""A terminal style."""
|
||||||
|
|
||||||
|
_color: Optional[Color]
|
||||||
|
_bgcolor: Optional[Color]
|
||||||
|
_attributes: int
|
||||||
|
_set_attributes: int
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
|
|
|
@ -195,7 +195,7 @@ CODE = r'''
|
||||||
yield new_line
|
yield new_line
|
||||||
'''
|
'''
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__": # pragma: no cover
|
||||||
|
|
||||||
syntax = Syntax(CODE, "python", dedent=True, line_numbers=True, start_line=990)
|
syntax = Syntax(CODE, "python", dedent=True, line_numbers=True, start_line=990)
|
||||||
|
|
||||||
|
@ -208,9 +208,9 @@ if __name__ == "__main__":
|
||||||
elapsed = int((time() - start) * 1000)
|
elapsed = int((time() - start) * 1000)
|
||||||
print(f"{elapsed}ms")
|
print(f"{elapsed}ms")
|
||||||
|
|
||||||
print(Color.downgrade.cache_info())
|
# print(Color.downgrade.cache_info())
|
||||||
print(Color.parse.cache_info())
|
# print(Color.parse.cache_info())
|
||||||
print(Color.get_ansi_codes.cache_info())
|
# print(Color.get_ansi_codes.cache_info())
|
||||||
|
|
||||||
print(Style.parse.cache_info())
|
# print(Style.parse.cache_info())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue