Skip to content

Commit f0d0da8

Browse files
committed
Merge branch 'master' into v10.15.0
2 parents 86032f6 + a6d1d78 commit f0d0da8

File tree

9 files changed

+102
-4
lines changed

9 files changed

+102
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4141
- Allowed `__rich__` to work recursively
4242
- Allowed Text classes to work with sep in print https://github.com/willmcgugan/rich/issues/1689
4343

44+
### Added
45+
46+
- Added a `rich.text.Text.from_ansi` helper method for handling pre-formatted input strings https://github.com/willmcgugan/rich/issues/1670
47+
4448
## [10.13.0] - 2021-11-07
4549

4650
### Added

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ The following people have contributed to the development of Rich:
2323
- [Clément Robert](https://github.com/neutrinoceros)
2424
- [Tushar Sadhwani](https://github.com/tusharsadhwani)
2525
- [Tim Savage](https://github.com/timsavage)
26+
- [Nicolas Simonds](https://github.com/0xDEC0DE)
2627
- [Gabriele N. Tornetta](https://github.com/p403n1x87)

README.pt-br.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Confira a [documentação do inspect](https://rich.readthedocs.io/en/latest/refe
125125

126126
# A biblioteca Rich
127127

128-
O Rich possui vários _renderizaveis_ nativos que podem ser usados para criar outputs elegantes no seu CLI e ajudar a debugar o código.
128+
O Rich possui vários _renderizáveis_ nativos que podem ser usados para criar outputs elegantes no seu CLI e ajudar a debugar o código.
129129

130130
Clique nos itens a seguir para expandir os detalhes:
131131

docs/source/live.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Live Display
55

66
Progress bars and status indicators use a *live* display to animate parts of the terminal. You can build custom live displays with the :class:`~rich.live.Live` class.
77

8-
For a demonstration of a live display, run the following command:
8+
For a demonstration of a live display, run the following command::
99

1010
python -m rich.live
1111

docs/source/text.rst

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Alternatively, you can construct styled text by calling :meth:`~rich.text.Text.a
2626
text.append(" World!")
2727
console.print(text)
2828

29+
If you would like to use text that is already formatted with ANSI codes, call :meth:`~rich.text.Text.from_ansi` to convert it to a ``Text`` object:
30+
31+
text = Text.from_ansi("\033[1mHello, World!\033[0m")
32+
console.print(text.spans)
33+
2934
Since building Text instances from parts is a common requirement, Rich offers :meth:`~rich.text.Text.assemble` which will combine strings or pairs of string and Style, and return a Text instance. The follow example is equivalent to the code above::
3035

3136
text = Text.assemble(("Hello", "bold magenta"), " World!")

poetry.lock

+46-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jupyter = ["ipywidgets"]
4040

4141
[tool.poetry.dev-dependencies]
4242
pytest = "^6.2.5"
43-
black = "^21.10b0"
43+
black = "^21.11b1"
4444
mypy = "^0.910"
4545
pytest-cov = "^3.0.0"
4646
attrs = "^21.2.0"

rich/text.py

+34
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,40 @@ def from_markup(
242242
rendered_text.overflow = overflow
243243
return rendered_text
244244

245+
@classmethod
246+
def from_ansi(
247+
cls,
248+
text: str,
249+
*,
250+
style: Union[str, Style] = "",
251+
justify: Optional["JustifyMethod"] = None,
252+
overflow: Optional["OverflowMethod"] = None,
253+
no_wrap: Optional[bool] = None,
254+
end: str = "\n",
255+
tab_size: Optional[int] = 8,
256+
) -> "Text":
257+
"""Create a Text object from pre-formatted ANSI.
258+
259+
Args:
260+
text (str): A string containing ANSI color codes.
261+
style (Union[str, Style], optional): Base style for text. Defaults to "".
262+
justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
263+
overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
264+
no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.
265+
end (str, optional): Character to end text with. Defaults to "\\\\n".
266+
tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to 8.
267+
"""
268+
from .ansi import AnsiDecoder
269+
270+
decoded_text = AnsiDecoder().decode_line(text)
271+
decoded_text.justify = justify
272+
decoded_text.overflow = overflow
273+
decoded_text.no_wrap = no_wrap
274+
decoded_text.end = end
275+
decoded_text.tab_size = tab_size
276+
decoded_text.stylize(style)
277+
return decoded_text
278+
245279
@classmethod
246280
def styled(
247281
cls,

tests/test_text.py

+9
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ def test_from_markup():
9595
assert text._spans == [Span(7, 13, "bold")]
9696

9797

98+
def test_from_ansi():
99+
text = Text.from_ansi("Hello, \033[1mWorld!\033[0m")
100+
text2 = Text.from_ansi("Hello, \033[1mWorld!\033[0m", style="red")
101+
assert str(text) == "Hello, World!"
102+
assert text._spans == [Span(7, 13, Style(bold=True))]
103+
assert str(text2) == "Hello, World!"
104+
assert text2._spans == [Span(7, 13, Style(bold=True)), Span(0, 13, "red")]
105+
106+
98107
def test_copy():
99108
test = Text()
100109
test.append("Hello", "bold")

0 commit comments

Comments
 (0)