Skip to content

Commit 9032b00

Browse files
ahnuclkiancross
andauthored
Merge styles with Choice custom styles (#362)
--------- Co-authored-by: Kian Cross <kian@kiancross.co.uk>
1 parent ea93151 commit 9032b00

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

questionary/prompts/common.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,16 @@ def append(index: int, choice: Choice):
472472
tokens.append(("class:text", "{}".format(indicator)))
473473

474474
if isinstance(choice.title, list):
475-
tokens.extend(choice.title)
475+
if selected:
476+
extra_style = " class:selected"
477+
elif index == self.pointed_at:
478+
extra_style = " class:highlighted"
479+
else:
480+
extra_style = ""
481+
tokens.extend(
482+
(fragment[0] + extra_style, fragment[1])
483+
for fragment in choice.title
484+
)
476485
elif selected:
477486
tokens.append(
478487
("class:selected", "{}{}".format(shortcut, choice.title))

tests/prompts/test_common.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,44 @@ def test_prompt_highlight_coexist():
137137
assert ic._get_choice_tokens() == expected_tokens
138138

139139

140+
def test_prompt_highlight_selected_with_custom_styles():
141+
# formatted-text (list) titles should keep their custom style and also
142+
# pick up the highlighted/selected style (see #151)
143+
ic = InquirerControl(
144+
[
145+
Choice(title=[("fg:red", "a")]),
146+
Choice(title=[("fg:blue", "b")], checked=True),
147+
]
148+
)
149+
150+
expected_tokens = [
151+
("class:pointer", " » "),
152+
("[SetCursorPosition]", ""),
153+
("class:text", "○ "),
154+
("fg:red class:highlighted", "a"),
155+
("", "\n"),
156+
("class:text", " "),
157+
("class:selected", "● "),
158+
("fg:blue class:selected", "b"),
159+
]
160+
assert ic.pointed_at == 0
161+
assert ic._get_choice_tokens() == expected_tokens
162+
163+
ic.select_next()
164+
expected_tokens = [
165+
("class:text", " "),
166+
("class:text", "○ "),
167+
("fg:red", "a"),
168+
("", "\n"),
169+
("class:pointer", " » "),
170+
("[SetCursorPosition]", ""),
171+
("class:selected", "● "),
172+
("fg:blue class:selected", "b"),
173+
]
174+
assert ic.pointed_at == 1
175+
assert ic._get_choice_tokens() == expected_tokens
176+
177+
140178
def test_prompt_show_answer_with_shortcuts():
141179
ic = InquirerControl(
142180
["a", Choice("b", shortcut_key=False), "c"],

0 commit comments

Comments
 (0)