Skip to content

Commit

Permalink
fix for newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Mar 1, 2020
1 parent 608ff22 commit c73d2b6
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 41 deletions.
6 changes: 2 additions & 4 deletions rich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ def print(

_console = Console(log_time=False)

write_console = _console if file is None else Console(log_time=False, file=file)
return write_console.log(
*objects, sep=sep, end=end, log_locals=log_locals, _stack_offset=2
)
write_console = _console if file is None else Console(file=file)
return write_console.print(*objects, sep=sep, end=end)


init()
Expand Down
15 changes: 7 additions & 8 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,10 @@ def render_lines(
_rendered = self.render(renderable, render_options)
lines = list(
Segment.split_and_crop_lines(
_rendered, render_options.max_width, style=style
_rendered,
render_options.max_width,
style=style,
include_new_lines=False,
)
)
return lines
Expand Down Expand Up @@ -668,8 +671,7 @@ def _collect_renderables(
"""
from .text import Text

sep_text = Text(sep)
end_text = Text(end)
sep_text = Text(sep, end=end)
renderables: List[ConsoleRenderable] = []
append = renderables.append
text: List[Text] = []
Expand All @@ -683,9 +685,7 @@ def _collect_renderables(

def check_text() -> None:
if text:
all_text = sep_text.join(text)
all_text.end = end
append(all_text)
append(sep_text.join(text))
del text[:]

for renderable in objects:
Expand Down Expand Up @@ -756,7 +756,6 @@ def print(
renderables = self._collect_renderables(
objects, sep=sep, end=end, emoji=emoji, highlight=highlight,
)
print(repr(renderables))

render_options = self.options
extend = self.buffer.extend
Expand Down Expand Up @@ -850,7 +849,6 @@ def _render_buffer(self) -> str:
append(style.render(text, color_system=color_system, reset=True))
else:
append(text)
append("\n")
rendered = "".join(output)
return rendered

Expand All @@ -869,6 +867,7 @@ def export_text(self, clear: bool = True, styles: bool = False) -> str:
assert (
self.record
), "To export console contents set record=True in the constructor or instance"

if styles:
text = "".join(
(style.render(text, reset=True) if style else text)
Expand Down
8 changes: 3 additions & 5 deletions rich/constrain.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from typing import Optional

from .console import Console, ConsoleOptions, ConsoleRenderable, RenderResult
from .console import Console, ConsoleOptions, RenderableType, RenderResult


class Constrain:
"""Constrain the width of a renderable to a given number of characters.
Args:
renderable (ConsoleRenderable): A renderable object.
renderable (RenderableType): A renderable object.
width (int, optional): The maximum width (in characters) to render. Defaults to 80.
"""

def __init__(
self, renderable: ConsoleRenderable, width: Optional[int] = 80
) -> None:
def __init__(self, renderable: RenderableType, width: Optional[int] = 80) -> None:
self.renderable = renderable
self.width = width

Expand Down
2 changes: 1 addition & 1 deletion rich/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def adjust_line_length(

import commonmark

print(commonmark.commonmark(markup))
# print(commonmark.commonmark(markup))

from .console import Console

Expand Down
7 changes: 2 additions & 5 deletions rich/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ class Panel:

def __init__(
self,
renderable: Union[str, ConsoleRenderable],
renderable: RenderableType,
box=box.ROUNDED,
expand: bool = True,
style: Union[str, Style] = "none",
) -> None:

self.renderable = (
Text.from_markup(renderable) if isinstance(renderable, str) else renderable
)
self.renderable = renderable
self.box = box
self.expand = expand
self.style = style
Expand Down
23 changes: 15 additions & 8 deletions rich/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def split_and_crop_lines(
length: int,
style: Style = None,
pad: bool = True,
include_new_lines: bool = True,
) -> Iterable[List["Segment"]]:
"""Split segments in to lines, and crop lines greater than a given length.
Expand All @@ -75,9 +76,12 @@ def split_and_crop_lines(
if _text:
append(cls(_text, style))
if new_line:
yield cls.adjust_line_length(
lines[-1], length, style=style, pad=pad
line = cls.adjust_line_length(
lines[-1], length, style=style, pad=pad,
)
if include_new_lines:
line.append(cls("\n"))
yield line
lines.append([])
append = lines[-1].append
else:
Expand All @@ -101,15 +105,17 @@ def adjust_line_length(
List[Segment]: A line of segments with the desired length.
"""
line_length = sum(len(text) for text, _style in line)
new_line: List[Segment]

if line_length < length:
if pad:
return line + [cls(" " * (length - line_length), style)]
new_line = line + [cls(" " * (length - line_length), style)]
else:
return line[:]
new_line = line[:]
elif line_length > length:
line_length = 0
new_line: List[Segment] = []
new_line = []
append = new_line.append
line_length = 0
for segment in line:
segment_length = len(segment.text)
if line_length + segment_length < length:
Expand All @@ -119,8 +125,9 @@ def adjust_line_length(
text, style = segment
append(cls(text[: length - line_length], style))
break
return new_line
return line[:]
else:
new_line = line[:]
return new_line

@classmethod
def get_line_length(cls, line: List["Segment"]) -> int:
Expand Down
4 changes: 1 addition & 3 deletions rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ def __console_width__(self, max_width: int) -> "RenderWidth":
return RenderWidth(max_width, max_width)

def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult:

code_width = options.max_width if self.code_width is None else self.code_width

code = self.code
if self.dedent:
code = textwrap.dedent(code)
Expand Down Expand Up @@ -272,5 +270,5 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult

console = Console()

syntax = Syntax.from_path(sys.argv[1])
syntax = Syntax.from_path(sys.argv[1], line_numbers=False)
console.print(syntax)
2 changes: 0 additions & 2 deletions rich/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,9 @@ def _render(
"Foo", footer=Text("Total", justify="right"), footer_style="bold", ratio=1
),
Column("Bar", style="red", footer="123", ratio=1),
box=box.SIMPLE,
expand=True,
show_footer=True,
show_edge=True,
width=100,
style="on blue",
)
# table.columns[0].width = 50
Expand Down
17 changes: 13 additions & 4 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Span(NamedTuple):
style: Union[str, Style]

def __repr__(self) -> str:
return f'<span {self.start}:{self.end} "{self.style}">'
return f"Span({self.start}, {self.end}, {self.style!r})"

def __bool__(self) -> bool:
return self.end > self.start
Expand Down Expand Up @@ -163,7 +163,7 @@ def assemble(
style: Union[str, Style] = "",
justify: "JustifyValues" = None,
end: str = "\n",
tab_size: Optional[int] = None,
tab_size: int = 8,
) -> "Text":
text = cls(style=style, justify=justify, end=end, tab_size=tab_size)
append = text.append
Expand All @@ -189,9 +189,18 @@ def text(self, new_text: str) -> "Text":
self._trim_spans()
return self

def blank_copy(self) -> "Text":
"""Return a new Text instance with copied meta data (but not the string or spans)."""
copy_self = Text(
style=self.style,
justify=self.justify,
end=self.end,
tab_size=self.tab_size,
)
return copy_self

def copy(self) -> "Text":
"""Return a copy of this instance."""
self.text
copy_self = Text(
self.text,
style=self.style,
Expand Down Expand Up @@ -389,7 +398,7 @@ def join(self, lines: Iterable["Text"]) -> "Text":
Text: A new text instance containing join text.
"""

new_text = Text()
new_text = self.blank_copy()
append = new_text.append
for last, line in iter_last(lines):
append(line)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ def test_apply_style():
def test_split_and_crop_lines():
assert list(
Segment.split_and_crop_lines([Segment("Hello\nWorld!\n"), Segment("foo")], 4)
) == [[Segment("Hell")], [Segment("Worl")], [Segment("foo"), Segment(" ")]]
) == [
[Segment("Hell"), Segment("\n", None)],
[Segment("Worl"), Segment("\n", None)],
[Segment("foo"), Segment(" ")],
]


def test_adjust_line_length():
Expand Down

0 comments on commit c73d2b6

Please sign in to comment.