MCPcopy
hub / github.com/Textualize/rich / from_ansi

Method from_ansi

rich/text.py:294–329  ·  view source on GitHub ↗

Create a Text object from a string containing ANSI escape codes. Args: text (str): A string containing escape codes. style (Union[str, Style], optional): Base style for text. Defaults to "". justify (str, optional): Justify method: "left", "center", "full

(
        cls,
        text: str,
        *,
        style: Union[str, Style] = "",
        justify: Optional["JustifyMethod"] = None,
        overflow: Optional["OverflowMethod"] = None,
        no_wrap: Optional[bool] = None,
        end: str = "\n",
        tab_size: Optional[int] = 8,
    )

Source from the content-addressed store, hash-verified

292
293 @classmethod
294 def from_ansi(
295 cls,
296 text: str,
297 *,
298 style: Union[str, Style] = "",
299 justify: Optional["JustifyMethod"] = None,
300 overflow: Optional["OverflowMethod"] = None,
301 no_wrap: Optional[bool] = None,
302 end: str = "\n",
303 tab_size: Optional[int] = 8,
304 ) -> "Text":
305 """Create a Text object from a string containing ANSI escape codes.
306
307 Args:
308 text (str): A string containing escape codes.
309 style (Union[str, Style], optional): Base style for text. Defaults to "".
310 justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
311 overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
312 no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.
313 end (str, optional): Character to end text with. Defaults to "\\\\n".
314 tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None.
315 """
316 from .ansi import AnsiDecoder
317
318 joiner = Text(
319 "\n",
320 justify=justify,
321 overflow=overflow,
322 no_wrap=no_wrap,
323 end=end,
324 tab_size=tab_size,
325 style=style,
326 )
327 decoder = AnsiDecoder()
328 result = joiner.join(line for line in decoder.decode(text))
329 return result
330
331 @classmethod
332 def styled(

Callers 7

test_decode_exampleFunction · 0.45
test_decode_issue_2688Function · 0.45
test_decode_newlinesFunction · 0.45
test_from_ansiFunction · 0.45
test_from_ansiFunction · 0.45
__rich_console__Method · 0.45

Calls 4

joinMethod · 0.95
decodeMethod · 0.95
TextClass · 0.85
AnsiDecoderClass · 0.85

Tested by 6

test_decode_exampleFunction · 0.36
test_decode_issue_2688Function · 0.36
test_decode_newlinesFunction · 0.36
test_from_ansiFunction · 0.36
test_from_ansiFunction · 0.36