console out

This commit is contained in:
Will McGugan 2020-10-11 17:33:16 +01:00
parent a83ee864e6
commit 6d56cfad90
5 changed files with 53 additions and 1 deletions

View file

@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Addded box.SQUARE_DOUBLE_HEAD
- Added highlighting of EUI-48 and EUI-64 (MAC addresses)
- Added Console.pager
- Added Console.out
### Changed

View file

@ -69,6 +69,15 @@ The :meth:`~rich.console.Console.log` methods offers the same capabilities as pr
To help with debugging, the log() method has a ``log_locals`` parameter. If you set this to ``True``, Rich will display a table of local variables where the method was called.
Low level output
----------------
In additional to :meth:`~rich.console.Console.print` and :meth:`~rich.console.Console.log`, Rich has a :meth:`~rich.console.Console.out` method which provides a lower-level way of writing to the terminal. The out() method converts all the positional arguments to strings and won't pretty print, word wrap, or apply markup to the output, but can apply a basic style and will optionally do highlighting.
Here's an example::
>>> console.out("Locals", locals())
Justify / Alignment
-------------------

View file

@ -1012,6 +1012,37 @@ class Console:
self._buffer.append(Segment.control(str(control_codes)))
self._check_buffer()
def out(
self,
*objects: Any,
sep=" ",
end="\n",
style: Union[str, Style] = None,
highlight: bool = True,
) -> None:
"""Output to the terminal. This is a low-level way of writing to the terminal which unlike
:meth:`~rich.console.Console.print` doesn't pretty print, wrap text, nor markup, but will highlighting
and apply basic style.
Args:
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".
style (Union[str, Style], optional): A style to apply to output. Defaults to None.
highlight (Optional[bool], optional): Enable automatic highlighting, or ``None`` to use console default. Defaults to ``None``.
"""
raw_output: str = sep.join(str(_object) for _object in objects)
self.print(
raw_output,
style=style,
highlight=highlight,
emoji=False,
markup=False,
no_wrap=True,
overflow="ignore",
crop=False,
end=end,
)
def print(
self,
*objects: Any,

View file

@ -370,3 +370,10 @@ def test_pager() -> None:
console.print("[bold link https:/example.org]Hello World")
assert pager_content == "Hello World\n"
def test_out() -> None:
console = Console(width=10)
console.begin_capture()
console.out(*(["foo bar"] * 5), sep=".", end="X")
assert console.end_capture() == "foo bar.foo bar.foo bar.foo bar.foo barX"

View file

@ -32,7 +32,11 @@ def test_rich_console():
renderable = "test renderable"
style = Style(color="red")
options = ConsoleOptions(
min_width=10, max_width=20, is_terminal=False, encoding="utf-8"
legacy_windows=False,
min_width=10,
max_width=20,
is_terminal=False,
encoding="utf-8",
)
expected_outputs = [