1+ from contextlib import contextmanager
2+
3+
14BACK_VALUE = "__BACK__"
5+ CHECKBOX_SELECTED_INDICATOR = "☑"
6+ CHECKBOX_UNSELECTED_INDICATOR = "☐"
27
38def get_style ():
49 """Custom style for questionary."""
@@ -47,10 +52,10 @@ def get_separator():
4752 import questionary
4853 return questionary .Separator ()
4954
50- def create_choice (title , value ):
55+ def create_choice (title , value , checked = False ):
5156 """Return a questionary Choice."""
5257 import questionary
53- return questionary .Choice (title = title , value = value )
58+ return questionary .Choice (title = title , value = value , checked = checked )
5459
5560def ask_select (message , choices , style = None ):
5661 """Ask a selection question with escape back support."""
@@ -64,6 +69,45 @@ def ask_select(message, choices, style=None):
6469 use_arrow_keys = True
6570 ))
6671
72+
73+ @contextmanager
74+ def checkbox_indicator_style ():
75+ """Temporarily render questionary checkboxes as checkbox glyphs."""
76+ import questionary .constants as constants
77+ import questionary .prompts .common as common
78+
79+ old_selected = constants .INDICATOR_SELECTED
80+ old_unselected = constants .INDICATOR_UNSELECTED
81+ old_common_selected = common .INDICATOR_SELECTED
82+ old_common_unselected = common .INDICATOR_UNSELECTED
83+
84+ constants .INDICATOR_SELECTED = CHECKBOX_SELECTED_INDICATOR
85+ constants .INDICATOR_UNSELECTED = CHECKBOX_UNSELECTED_INDICATOR
86+ common .INDICATOR_SELECTED = CHECKBOX_SELECTED_INDICATOR
87+ common .INDICATOR_UNSELECTED = CHECKBOX_UNSELECTED_INDICATOR
88+ try :
89+ yield
90+ finally :
91+ constants .INDICATOR_SELECTED = old_selected
92+ constants .INDICATOR_UNSELECTED = old_unselected
93+ common .INDICATOR_SELECTED = old_common_selected
94+ common .INDICATOR_UNSELECTED = old_common_unselected
95+
96+
97+ def ask_checkbox (message , choices , style = None , instruction = None ):
98+ """Ask a checkbox question with shared style and escape back support."""
99+ import questionary
100+ if style is None :
101+ style = get_style ()
102+ with checkbox_indicator_style ():
103+ return ask_with_escape_back (questionary .checkbox (
104+ message ,
105+ choices = choices ,
106+ style = style ,
107+ use_arrow_keys = True ,
108+ instruction = instruction ,
109+ ))
110+
67111def ask_text (message , default = "" , password = False , style = None ):
68112 """Ask a text question with escape back support."""
69113 import questionary
0 commit comments