Skip to content

Commit 36a8418

Browse files
committed
allow exit with ctrl+c
1 parent 0879434 commit 36a8418

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

mpqp/tools/choice_tree.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,28 @@ def run_choice_tree(question: QuestionNode):
5050
Args:
5151
question: Root question from which the choice tree will start
5252
"""
53-
# 3M-TODO: allow exit with Ctrl+C and Q
53+
# 3M-TODO: allow exit with Q
5454
prev_args = []
5555
prev_message = ""
5656
next_question = question
57-
while True:
58-
option, _ = pick(
59-
list(map(lambda a: a.label, next_question.answers)) + ["Exit"],
60-
prev_message + "\n\n" + next_question.label,
61-
indicator="=>",
62-
)
63-
if option == "Exit":
64-
return
65-
selected_answer = find(next_question.answers, lambda a: a.label == option)
66-
prev_message, prev_args = selected_answer.action(*prev_args)
67-
next_question = selected_answer.next_question
68-
if next_question is None:
69-
pick(["Press 'Enter' to continue"], prev_message, indicator="")
70-
return
57+
try:
58+
while True:
59+
option, _ = pick(
60+
list(map(lambda a: a.label, next_question.answers)) + ["Exit"],
61+
prev_message + "\n\n" + next_question.label,
62+
indicator="=>",
63+
)
64+
if option == "Exit":
65+
return
66+
selected_answer = find(next_question.answers, lambda a: a.label == option)
67+
prev_message, prev_args = selected_answer.action(*prev_args)
68+
next_question = selected_answer.next_question
69+
if next_question is None:
70+
pick(["Press 'Enter' to continue"], prev_message, indicator="")
71+
return
72+
73+
except KeyboardInterrupt:
74+
pass
7175

7276

7377
# Example:

0 commit comments

Comments
 (0)