mirror of
https://github.com/Textualize/rich.git
synced 2025-08-31 15:37:24 +00:00
overflow ignore
This commit is contained in:
parent
79dba900da
commit
ce82bb67bb
6 changed files with 46 additions and 46 deletions
|
@ -5,6 +5,11 @@ 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).
|
||||
|
||||
## [5.2.0] - Unreleased
|
||||
|
||||
- Added crop argument to Console.print
|
||||
- Added "ignore" overflow method
|
||||
|
||||
## [5.1.2] - 2020-08-10
|
||||
|
||||
### Fixed
|
||||
|
|
|
@ -112,7 +112,7 @@ Note that you generally don't need to think about cropping, as Rich will wrap te
|
|||
Justify / Alignment
|
||||
-------------------
|
||||
|
||||
Both print and log support a ``justify`` argument which if set must be one of "default", "left", "right", "center", "full", or "ignore". If "left", any text printed (or logged) will be left aligned, if "right" text will be aligned to the right of the terminal, if "center" the text will be centered, and if "full" the text will be lined up with both the left and right edges of the terminal (like printed text in a book). Finally, if justify is set to "ignore" then text will not be justified and allowed to extend to the next line(s).
|
||||
Both print and log support a ``justify`` argument which if set must be one of "default", "left", "right", "center", "full", or "ignore". If "left", any text printed (or logged) will be left aligned, if "right" text will be aligned to the right of the terminal, if "center" the text will be centered, and if "full" the text will be lined up with both the left and right edges of the terminal (like printed text in a book).
|
||||
|
||||
The default for ``justify`` is ``"default"`` which will generally look the same as ``"left"`` but with a subtle difference. Left justify will pad the right of the text with spaces, while a default justify will not. You will only notice the difference if you set a background color with the ``style`` argument. The following example demonstrates the difference::
|
||||
|
||||
|
@ -142,13 +142,13 @@ Overflow
|
|||
|
||||
Overflow is what happens when text you print is larger than the available space. Overflow may occur if you print long 'words' such as URLs for instance, or if you have text inside a panel or table cell with restricted space.
|
||||
|
||||
You can specify how Rich should handle overflow with the ``overflow`` argument to :meth:`~rich.console.Console.print` which should be one of the following strings: "fold", "crop", or "ellipsis". The default is "fold" which will put any excess characters on the following line, creating as many new lines as required to fit the text.
|
||||
You can specify how Rich should handle overflow with the ``overflow`` argument to :meth:`~rich.console.Console.print` which should be one of the following strings: "fold", "crop", "ellipsis", or "ignore". The default is "fold" which will put any excess characters on the following line, creating as many new lines as required to fit the text.
|
||||
|
||||
The "crop" method truncates the text at the end of the line, discarding any characters that would overflow.
|
||||
|
||||
The "ellipsis" method is similar to "crop", but will insert an ellipsis character ("…") at the end of any text that has been truncated.
|
||||
|
||||
The following code demonstrates Rich's overflow methods::
|
||||
The following code demonstrates the basic overflow methods::
|
||||
|
||||
from typing import List
|
||||
from rich.console import Console, OverflowMethod
|
||||
|
@ -179,6 +179,7 @@ This produces the following output:
|
|||
</span>
|
||||
</pre>
|
||||
|
||||
You can also set overflow to "ignore" which allows text to run on to the next line. In practice this will look the same as "crop" unless you also set ``crop=False` in :meth:`~rich.console.Console.print`.
|
||||
|
||||
Input
|
||||
-----
|
||||
|
|
|
@ -50,8 +50,8 @@ if TYPE_CHECKING:
|
|||
WINDOWS = platform.system() == "Windows"
|
||||
|
||||
HighlighterType = Callable[[Union[str, "Text"]], "Text"]
|
||||
JustifyMethod = Literal["default", "left", "center", "right", "full", "ignore"]
|
||||
OverflowMethod = Literal["fold", "crop", "ellipsis"]
|
||||
JustifyMethod = Literal["default", "left", "center", "right", "full"]
|
||||
OverflowMethod = Literal["fold", "crop", "ellipsis", "ignore"]
|
||||
|
||||
|
||||
CONSOLE_HTML_FORMAT = """\
|
||||
|
@ -933,6 +933,7 @@ class Console:
|
|||
sep (str, optional): String to write between print data. Defaults to " ".
|
||||
end (str, optional): String to write at end of print data. Defaults to "\\n".
|
||||
justify (str, optional): One of "left", "right", "center", or "full". Defaults to ``None``.
|
||||
overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None.
|
||||
emoji (Optional[bool], optional): Enable emoji code, or ``None`` to use console default. Defaults to None.
|
||||
markup (Optional[bool], optional): Enable markup, or ``None`` to use console default. Defaults to None.
|
||||
highlight (Optional[bool], optional): Enable automatic highlighting, or ``None`` to use console default. Defaults to None.
|
||||
|
|
|
@ -49,11 +49,7 @@ def install(
|
|||
value
|
||||
if hasattr(value, "__rich_console__") or hasattr(value, "__rich__")
|
||||
else pretty_repr(
|
||||
value,
|
||||
max_width=console.width,
|
||||
no_wrap=no_wrap,
|
||||
justify="default" if crop else "ignore",
|
||||
overflow=overflow,
|
||||
value, max_width=console.width, no_wrap=no_wrap, overflow=overflow
|
||||
),
|
||||
crop=crop,
|
||||
)
|
||||
|
|
62
rich/text.py
62
rich/text.py
|
@ -639,16 +639,17 @@ class Text(JupyterMixin):
|
|||
overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow.
|
||||
pad (bool, optional): Pad with spaces if the length is less than max_width. Defaults to False.
|
||||
"""
|
||||
length = cell_len(self.plain)
|
||||
_overflow = overflow or self.overflow or DEFAULT_OVERFLOW
|
||||
if length > max_width:
|
||||
if _overflow == "ellipsis":
|
||||
self.plain = set_cell_size(self.plain, max_width - 1).rstrip() + "…"
|
||||
else:
|
||||
self.plain = set_cell_size(self.plain, max_width)
|
||||
if pad and length < max_width:
|
||||
spaces = max_width - length
|
||||
self.plain = f"{self.plain}{' ' * spaces}"
|
||||
if _overflow != "ignore":
|
||||
length = cell_len(self.plain)
|
||||
if length > max_width:
|
||||
if _overflow == "ellipsis":
|
||||
self.plain = set_cell_size(self.plain, max_width - 1).rstrip() + "…"
|
||||
else:
|
||||
self.plain = set_cell_size(self.plain, max_width)
|
||||
if pad and length < max_width:
|
||||
spaces = max_width - length
|
||||
self.plain = f"{self.plain}{' ' * spaces}"
|
||||
|
||||
def _trim_spans(self) -> None:
|
||||
"""Remove or modify any spans that are over the end of the text."""
|
||||
|
@ -939,31 +940,26 @@ class Text(JupyterMixin):
|
|||
wrap_overflow = cast(
|
||||
"OverflowMethod", overflow or self.overflow or DEFAULT_OVERFLOW
|
||||
)
|
||||
no_wrap = pick_bool(no_wrap, self.no_wrap, False)
|
||||
no_wrap = pick_bool(no_wrap, self.no_wrap, False) or overflow == "ignore"
|
||||
|
||||
if wrap_justify == "ignore":
|
||||
lines = Lines(self.split(allow_blank=True))
|
||||
else:
|
||||
lines = Lines()
|
||||
for line in self.split(allow_blank=True):
|
||||
if "\t" in line:
|
||||
line = line.tabs_to_spaces(tab_size)
|
||||
if no_wrap:
|
||||
new_lines = Lines([line])
|
||||
else:
|
||||
offsets = divide_line(
|
||||
str(line), width, fold=wrap_overflow == "fold"
|
||||
)
|
||||
new_lines = line.divide(offsets)
|
||||
for line in new_lines:
|
||||
line.rstrip_end(width)
|
||||
if wrap_justify:
|
||||
new_lines.justify(
|
||||
console, width, justify=wrap_justify, overflow=wrap_overflow
|
||||
)
|
||||
for line in new_lines:
|
||||
line.truncate(width, overflow=wrap_overflow)
|
||||
lines.extend(new_lines)
|
||||
lines = Lines()
|
||||
for line in self.split(allow_blank=True):
|
||||
if "\t" in line:
|
||||
line = line.tabs_to_spaces(tab_size)
|
||||
if no_wrap:
|
||||
new_lines = Lines([line])
|
||||
else:
|
||||
offsets = divide_line(str(line), width, fold=wrap_overflow == "fold")
|
||||
new_lines = line.divide(offsets)
|
||||
for line in new_lines:
|
||||
line.rstrip_end(width)
|
||||
if wrap_justify:
|
||||
new_lines.justify(
|
||||
console, width, justify=wrap_justify, overflow=wrap_overflow
|
||||
)
|
||||
for line in new_lines:
|
||||
line.truncate(width, overflow=wrap_overflow)
|
||||
lines.extend(new_lines)
|
||||
return lines
|
||||
|
||||
def fit(self, width: int) -> Lines:
|
||||
|
|
|
@ -411,13 +411,14 @@ def test_wrap_long_words():
|
|||
assert lines[2] == Text("789 ")
|
||||
|
||||
|
||||
def test_justify_ignore():
|
||||
def test_no_wrap_no_crop():
|
||||
test = Text("Hello World!" * 3)
|
||||
|
||||
console = Console(width=20, file=StringIO())
|
||||
console.print(test, justify="ignore")
|
||||
console.print(test, justify="ignore", crop=False)
|
||||
console.print(test, no_wrap=True)
|
||||
console.print(test, no_wrap=True, crop=False, overflow="ignore")
|
||||
|
||||
print(repr(console.file.getvalue()))
|
||||
assert (
|
||||
console.file.getvalue()
|
||||
== "Hello World!Hello Wo\nHello World!Hello World!Hello World!\n"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue