2
2
"""
3
3
`checkbox` type question
4
4
"""
5
- from __future__ import print_function , unicode_literals
6
5
from prompt_toolkit .application import Application
7
- from prompt_toolkit .key_binding .manager import KeyBindingManager
8
- from prompt_toolkit .keys import Keys
9
- from prompt_toolkit .layout .containers import Window
6
+ from prompt_toolkit .key_binding import KeyBindings
10
7
from prompt_toolkit .filters import IsDone
11
- from prompt_toolkit .layout .controls import TokenListControl
8
+ from prompt_toolkit .layout .controls import FormattedTextControl
12
9
from prompt_toolkit .layout .containers import ConditionalContainer , \
13
- ScrollOffsets , HSplit
10
+ ScrollOffsets , HSplit , Window , WindowAlign
14
11
from prompt_toolkit .layout .dimension import LayoutDimension as D
15
- from prompt_toolkit .token import Token
12
+ from prompt_toolkit .layout import Layout
16
13
17
14
from .. import PromptParameterException
18
15
from ..separator import Separator
19
16
from .common import setup_simple_validator , default_style , if_mousedown
20
17
21
18
22
- # custom control based on TokenListControl
19
+ # custom control based on FormattedTextControl
23
20
24
21
25
- class InquirerControl (TokenListControl ):
22
+ class InquirerControl (FormattedTextControl ):
26
23
def __init__ (self , choices , pointer_index , ** kwargs ):
27
24
self .pointer_index = pointer_index
28
25
self .pointer_sign = kwargs .pop ("pointer_sign" , "\u276f " )
@@ -31,8 +28,7 @@ def __init__(self, choices, pointer_index, **kwargs):
31
28
self .selected_options = [] # list of names
32
29
self .answered = False
33
30
self ._init_choices (choices )
34
- super (InquirerControl , self ).__init__ (self ._get_choice_tokens ,
35
- ** kwargs )
31
+ super ().__init__ (self ._get_choice_tokens , ** kwargs )
36
32
37
33
def _init_choices (self , choices ):
38
34
# helper to convert from question format to internal format
@@ -57,48 +53,47 @@ def _init_choices(self, choices):
57
53
def choice_count (self ):
58
54
return len (self .choices )
59
55
60
- def _get_choice_tokens (self , cli ):
56
+ def _get_choice_tokens (self ):
61
57
tokens = []
62
- T = Token
63
58
64
59
def append (index , line ):
65
60
if isinstance (line , Separator ):
66
- tokens .append ((T . Separator , ' %s\n ' % line ))
61
+ tokens .append (('class: Separator' , ' %s\n ' % line ))
67
62
else :
68
63
line_name = line [0 ]
69
64
line_value = line [1 ]
70
65
selected = (line_value in self .selected_options ) # use value to check if option has been selected
71
66
pointed_at = (index == self .pointer_index )
72
67
73
68
@if_mousedown
74
- def select_item (cli , mouse_event ):
69
+ def select_item (mouse_event ):
75
70
# bind option with this index to mouse event
76
71
if line_value in self .selected_options :
77
72
self .selected_options .remove (line_value )
78
73
else :
79
74
self .selected_options .append (line_value )
80
75
81
76
if pointed_at :
82
- tokens .append ((T . Pointer , ' {}' .format (self .pointer_sign ), select_item )) # ' >'
77
+ tokens .append (('class:pointer' , ' {}' .format (self .pointer_sign ), select_item )) # ' >'
83
78
else :
84
- tokens .append ((T , ' ' , select_item ))
79
+ tokens .append (('' , ' ' , select_item ))
85
80
# 'o ' - FISHEYE
86
81
if choice [2 ]: # disabled
87
- tokens .append ((T , '- %s (%s)' % (choice [0 ], choice [2 ])))
82
+ tokens .append (('' , '- %s (%s)' % (choice [0 ], choice [2 ])))
88
83
else :
89
84
if selected :
90
- tokens .append ((T . Selected , '{} ' .format (self .selected_sign ), select_item ))
85
+ tokens .append (('class:selected' , '{} ' .format (self .selected_sign ), select_item ))
91
86
else :
92
- tokens .append ((T , '{} ' .format (self .unselected_sign ), select_item ))
87
+ tokens .append (('' , '{} ' .format (self .unselected_sign ), select_item ))
93
88
94
89
if pointed_at :
95
- tokens .append ((Token . SetCursorPosition , '' ))
90
+ tokens .append (('[ SetCursorPosition]' , '' ))
96
91
97
92
if choice [3 ]: # description
98
- tokens .append ((T , "%s - %s" % (line_name , choice [3 ])))
93
+ tokens .append (('' , "%s - %s" % (line_name , choice [3 ])))
99
94
else :
100
- tokens .append ((T , line_name , select_item ))
101
- tokens .append ((T , '\n ' ))
95
+ tokens .append (('' , line_name , select_item ))
96
+ tokens .append (('' , '\n ' ))
102
97
103
98
# prepare the select choices
104
99
for i , choice in enumerate (self .choices ):
@@ -142,30 +137,31 @@ def question(message, **kwargs):
142
137
ic = InquirerControl (choices , pointer_index , ** additional_parameters )
143
138
qmark = kwargs .pop ('qmark' , '?' )
144
139
145
- def get_prompt_tokens (cli ):
140
+ def get_prompt_tokens ():
146
141
tokens = []
147
142
148
- tokens .append ((Token . QuestionMark , qmark ))
149
- tokens .append ((Token . Question , ' %s ' % message ))
143
+ tokens .append (('class:questionmark' , qmark ))
144
+ tokens .append (('class:question' , ' %s ' % message ))
150
145
if ic .answered :
151
146
nbr_selected = len (ic .selected_options )
152
147
if nbr_selected == 0 :
153
- tokens .append ((Token . Answer , ' done' ))
148
+ tokens .append (('class:answer' , ' done' ))
154
149
elif nbr_selected == 1 :
155
- tokens .append ((Token . Answer , ' [%s]' % ic .selected_options [0 ]))
150
+ tokens .append (('class: Answer' , ' [%s]' % ic .selected_options [0 ]))
156
151
else :
157
- tokens .append ((Token . Answer ,
152
+ tokens .append (('class:answer' ,
158
153
' done (%d selections)' % nbr_selected ))
159
154
else :
160
- tokens .append ((Token . Instruction ,
155
+ tokens .append (('class:instruction' ,
161
156
' (<up>, <down> to move, <space> to select, <a> '
162
157
'to toggle, <i> to invert)' ))
163
158
return tokens
164
159
165
160
# assemble layout
166
161
layout = HSplit ([
167
162
Window (height = D .exact (1 ),
168
- content = TokenListControl (get_prompt_tokens , align_center = False )
163
+ content = FormattedTextControl (get_prompt_tokens ),
164
+ align = WindowAlign .CENTER ,
169
165
),
170
166
ConditionalContainer (
171
167
Window (
@@ -179,31 +175,31 @@ def get_prompt_tokens(cli):
179
175
])
180
176
181
177
# key bindings
182
- manager = KeyBindingManager . for_prompt ()
178
+ kb = KeyBindings ()
183
179
184
- @manager . registry . add_binding ( Keys . ControlQ , eager = True )
185
- @manager . registry . add_binding ( Keys . ControlC , eager = True )
180
+ @kb . add ( 'c-q' , eager = True )
181
+ @kb . add ( 'c-c' , eager = True )
186
182
def _ (event ):
187
183
raise KeyboardInterrupt ()
188
- # event.cli.set_return_value( None)
184
+ # event.app.exit(result= None)
189
185
190
- @manager . registry . add_binding (' ' , eager = True )
186
+ @kb . add (' ' , eager = True )
191
187
def toggle (event ):
192
188
pointed_choice = ic .choices [ic .pointer_index ][1 ] # value
193
189
if pointed_choice in ic .selected_options :
194
190
ic .selected_options .remove (pointed_choice )
195
191
else :
196
192
ic .selected_options .append (pointed_choice )
197
193
198
- @manager . registry . add_binding ('i' , eager = True )
194
+ @kb . add ('i' , eager = True )
199
195
def invert (event ):
200
196
inverted_selection = [c [1 ] for c in ic .choices if
201
197
not isinstance (c , Separator ) and
202
198
c [1 ] not in ic .selected_options and
203
199
not c [2 ]]
204
200
ic .selected_options = inverted_selection
205
201
206
- @manager . registry . add_binding ('a' , eager = True )
202
+ @kb . add ('a' , eager = True )
207
203
def all (event ):
208
204
all_selected = True # all choices have been selected
209
205
for c in ic .choices :
@@ -214,7 +210,7 @@ def all(event):
214
210
if all_selected :
215
211
ic .selected_options = []
216
212
217
- @manager . registry . add_binding ( Keys . Down , eager = True )
213
+ @kb . add ( 'down' , eager = True )
218
214
def move_cursor_down (event ):
219
215
def _next ():
220
216
ic .pointer_index = ((ic .pointer_index + 1 ) % ic .line_count )
@@ -223,7 +219,7 @@ def _next():
223
219
ic .choices [ic .pointer_index ][2 ]:
224
220
_next ()
225
221
226
- @manager . registry . add_binding ( Keys . Up , eager = True )
222
+ @kb . add ( 'up' , eager = True )
227
223
def move_cursor_up (event ):
228
224
def _prev ():
229
225
ic .pointer_index = ((ic .pointer_index - 1 ) % ic .line_count )
@@ -232,15 +228,15 @@ def _prev():
232
228
ic .choices [ic .pointer_index ][2 ]:
233
229
_prev ()
234
230
235
- @manager . registry . add_binding ( Keys . Enter , eager = True )
231
+ @kb . add ( 'enter' , eager = True )
236
232
def set_answer (event ):
237
233
ic .answered = True
238
234
# TODO use validator
239
- event .cli . set_return_value ( ic .get_selected_values ())
235
+ event .app . exit ( result = ic .get_selected_values ())
240
236
241
237
return Application (
242
- layout = layout ,
243
- key_bindings_registry = manager . registry ,
238
+ layout = Layout ( layout ) ,
239
+ key_bindings = kb ,
244
240
mouse_support = True ,
245
241
style = style
246
242
)
0 commit comments