Skip to content

Commit 252ad08

Browse files
committed
rich_to_pt_style now also supports blink, reverse, and conceal/hidden styles
rich_to_pt_style should now support all styles in common between rich and prompt-toolkit
1 parent ae78cb4 commit 252ad08

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

cmd2/pt_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ def rich_to_pt_style(rich_style: StyleType) -> str:
112112
parts.append("italic" if rich_style.italic else "noitalic")
113113
if rich_style.underline is not None:
114114
parts.append("underline" if rich_style.underline else "nounderline")
115+
if rich_style.blink is not None:
116+
parts.append("blink" if rich_style.blink else "noblink")
117+
if rich_style.reverse is not None:
118+
# prompt-toolkit uses 'reverse'
119+
parts.append("reverse" if rich_style.reverse else "noreverse")
120+
if rich_style.conceal is not None:
121+
# prompt-toolkit uses 'hidden' for Rich's 'conceal'
122+
parts.append("hidden" if rich_style.conceal else "nohidden")
115123
return " ".join(parts)
116124

117125

tests/test_pt_utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,3 +758,49 @@ def test_rich_to_pt_style_nounderline(self):
758758
style = Style(underline=False)
759759
pt_style = pt_utils.rich_to_pt_style(style)
760760
assert "nounderline" in pt_style
761+
762+
def test_rich_to_pt_style_blink(self):
763+
from rich.style import Style
764+
765+
style = Style(blink=True)
766+
pt_style = pt_utils.rich_to_pt_style(style)
767+
assert "blink" in pt_style
768+
769+
def test_rich_to_pt_style_noblink(self):
770+
from rich.style import Style
771+
772+
style = Style(blink=False)
773+
pt_style = pt_utils.rich_to_pt_style(style)
774+
assert "noblink" in pt_style
775+
776+
def test_rich_to_pt_style_reverse(self):
777+
from rich.style import Style
778+
779+
style = Style(reverse=True)
780+
pt_style = pt_utils.rich_to_pt_style(style)
781+
# Note: reverse replaces the default 'noreverse' that is added at the start of parts
782+
# wait, we'll check how it works exactly. It will append "reverse". So we just assert "reverse" in pt_style
783+
# actually, if reverse=True, "reverse" will be appended to the list, while "noreverse" is also at index 0.
784+
# Let's just check the last appended one.
785+
assert "reverse" in pt_style.split()
786+
787+
def test_rich_to_pt_style_noreverse(self):
788+
from rich.style import Style
789+
790+
style = Style(reverse=False)
791+
pt_style = pt_utils.rich_to_pt_style(style)
792+
assert "noreverse" in pt_style
793+
794+
def test_rich_to_pt_style_hidden_conceal(self):
795+
from rich.style import Style
796+
797+
style = Style(conceal=True)
798+
pt_style = pt_utils.rich_to_pt_style(style)
799+
assert "hidden" in pt_style
800+
801+
def test_rich_to_pt_style_nohidden_conceal(self):
802+
from rich.style import Style
803+
804+
style = Style(conceal=False)
805+
pt_style = pt_utils.rich_to_pt_style(style)
806+
assert "nohidden" in pt_style

0 commit comments

Comments
 (0)