Skip to content

Commit 4a66820

Browse files
Added to_plain_text utility. A function to turn formatted text into a string.
1 parent 7dd8435 commit 4a66820

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

prompt_toolkit/formatted_text/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
fragment_list_to_text,
2828
fragment_list_width,
2929
split_lines,
30+
to_plain_text,
3031
)
3132

3233
__all__ = [
@@ -49,4 +50,5 @@
4950
"fragment_list_width",
5051
"fragment_list_to_text",
5152
"split_lines",
53+
"to_plain_text",
5254
]

prompt_toolkit/formatted_text/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@
88

99
from prompt_toolkit.utils import get_cwidth
1010

11-
from .base import OneStyleAndTextTuple, StyleAndTextTuples
11+
from .base import (
12+
AnyFormattedText,
13+
OneStyleAndTextTuple,
14+
StyleAndTextTuples,
15+
to_formatted_text,
16+
)
1217

1318
__all__ = [
19+
"to_plain_text",
1420
"fragment_list_len",
1521
"fragment_list_width",
1622
"fragment_list_to_text",
1723
"split_lines",
1824
]
1925

2026

27+
def to_plain_text(value: AnyFormattedText) -> str:
28+
"""
29+
Turn any kind of formatted text back into plain text.
30+
"""
31+
return fragment_list_to_text(to_formatted_text(value))
32+
33+
2134
def fragment_list_len(fragments: StyleAndTextTuples) -> int:
2235
"""
2336
Return the amount of characters in this text fragment list.

0 commit comments

Comments
 (0)