File: cli.py

package info (click to toggle)
prompt-toolkit 1.0.9-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,452 kB
  • sloc: python: 15,352; makefile: 3
file content (395 lines) | stat: -rw-r--r-- 9,224 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
Filters that accept a `CommandLineInterface` as argument.
"""
from __future__ import unicode_literals
from .base import Filter
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.key_binding.vi_state import InputMode as ViInputMode
from prompt_toolkit.cache import memoized

__all__ = (
    'HasArg',
    'HasCompletions',
    'HasFocus',
    'InFocusStack',
    'HasSearch',
    'HasSelection',
    'HasValidationError',
    'IsAborting',
    'IsDone',
    'IsMultiline',
    'IsReadOnly',
    'IsReturning',
    'RendererHeightIsKnown',
    'InEditingMode',

    # Vi modes.
    'ViMode',
    'ViNavigationMode',
    'ViInsertMode',
    'ViInsertMultipleMode',
    'ViReplaceMode',
    'ViSelectionMode',
    'ViWaitingForTextObjectMode',
    'ViDigraphMode',

    # Emacs modes.
    'EmacsMode',
    'EmacsInsertMode',
    'EmacsSelectionMode',
)


@memoized()
class HasFocus(Filter):
    """
    Enable when this buffer has the focus.
    """
    def __init__(self, buffer_name):
        self._buffer_name = buffer_name

    @property
    def buffer_name(self):
        " The given buffer name. (Read-only) "
        return self._buffer_name

    def __call__(self, cli):
        return cli.current_buffer_name == self.buffer_name

    def __repr__(self):
        return 'HasFocus(%r)' % self.buffer_name


@memoized()
class InFocusStack(Filter):
    """
    Enable when this buffer appears on the focus stack.
    """
    def __init__(self, buffer_name):
        self._buffer_name = buffer_name

    @property
    def buffer_name(self):
        " The given buffer name. (Read-only) "
        return self._buffer_name

    def __call__(self, cli):
        return self.buffer_name in cli.buffers.focus_stack

    def __repr__(self):
        return 'InFocusStack(%r)' % self.buffer_name


@memoized()
class HasSelection(Filter):
    """
    Enable when the current buffer has a selection.
    """
    def __call__(self, cli):
        return bool(cli.current_buffer.selection_state)

    def __repr__(self):
        return 'HasSelection()'


@memoized()
class HasCompletions(Filter):
    """
    Enable when the current buffer has completions.
    """
    def __call__(self, cli):
        return cli.current_buffer.complete_state is not None

    def __repr__(self):
        return 'HasCompletions()'


@memoized()
class IsMultiline(Filter):
    """
    Enable in multiline mode.
    """
    def __call__(self, cli):
        return cli.current_buffer.is_multiline()

    def __repr__(self):
        return 'IsMultiline()'


@memoized()
class IsReadOnly(Filter):
    """
    True when the current buffer is read only.
    """
    def __call__(self, cli):
        return cli.current_buffer.read_only()

    def __repr__(self):
        return 'IsReadOnly()'


@memoized()
class HasValidationError(Filter):
    """
    Current buffer has validation error.
    """
    def __call__(self, cli):
        return cli.current_buffer.validation_error is not None

    def __repr__(self):
        return 'HasValidationError()'


@memoized()
class HasArg(Filter):
    """
    Enable when the input processor has an 'arg'.
    """
    def __call__(self, cli):
        return cli.input_processor.arg is not None

    def __repr__(self):
        return 'HasArg()'


@memoized()
class HasSearch(Filter):
    """
    Incremental search is active.
    """
    def __call__(self, cli):
        return cli.is_searching

    def __repr__(self):
        return 'HasSearch()'


@memoized()
class IsReturning(Filter):
    """
    When a return value has been set.
    """
    def __call__(self, cli):
        return cli.is_returning

    def __repr__(self):
        return 'IsReturning()'


@memoized()
class IsAborting(Filter):
    """
    True when aborting. (E.g. Control-C pressed.)
    """
    def __call__(self, cli):
        return cli.is_aborting

    def __repr__(self):
        return 'IsAborting()'


@memoized()
class IsExiting(Filter):
    """
    True when exiting. (E.g. Control-D pressed.)
    """
    def __call__(self, cli):
        return cli.is_exiting

    def __repr__(self):
        return 'IsExiting()'


@memoized()
class IsDone(Filter):
    """
    True when the CLI is returning, aborting or exiting.
    """
    def __call__(self, cli):
        return cli.is_done

    def __repr__(self):
        return 'IsDone()'


@memoized()
class RendererHeightIsKnown(Filter):
    """
    Only True when the renderer knows it's real height.

    (On VT100 terminals, we have to wait for a CPR response, before we can be
    sure of the available height between the cursor position and the bottom of
    the terminal. And usually it's nicer to wait with drawing bottom toolbars
    until we receive the height, in order to avoid flickering -- first drawing
    somewhere in the middle, and then again at the bottom.)
    """
    def __call__(self, cli):
        return cli.renderer.height_is_known

    def __repr__(self):
        return 'RendererHeightIsKnown()'


@memoized()
class InEditingMode(Filter):
    """
    Check whether a given editing mode is active. (Vi or Emacs.)
    """
    def __init__(self, editing_mode):
        self._editing_mode = editing_mode

    @property
    def editing_mode(self):
        " The given editing mode. (Read-only) "
        return self._editing_mode

    def __call__(self, cli):
        return cli.editing_mode == self.editing_mode

    def __repr__(self):
        return 'InEditingMode(%r)' % (self.editing_mode, )


@memoized()
class ViMode(Filter):
    def __call__(self, cli):
        return cli.editing_mode == EditingMode.VI

    def __repr__(self):
        return 'ViMode()'


@memoized()
class ViNavigationMode(Filter):
    """
    Active when the set for Vi navigation key bindings are active.
    """
    def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state):
            return False

        return (cli.vi_state.input_mode == ViInputMode.NAVIGATION or
                cli.current_buffer.read_only())

    def __repr__(self):
        return 'ViNavigationMode()'


@memoized()
class ViInsertMode(Filter):
    def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state
                or cli.current_buffer.read_only()):
            return False

        return cli.vi_state.input_mode == ViInputMode.INSERT

    def __repr__(self):
        return 'ViInputMode()'


@memoized()
class ViInsertMultipleMode(Filter):
    def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state
                or cli.current_buffer.read_only()):
            return False

        return cli.vi_state.input_mode == ViInputMode.INSERT_MULTIPLE

    def __repr__(self):
        return 'ViInsertMultipleMode()'


@memoized()
class ViReplaceMode(Filter):
    def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state
                or cli.current_buffer.read_only()):
            return False

        return cli.vi_state.input_mode == ViInputMode.REPLACE

    def __repr__(self):
        return 'ViReplaceMode()'


@memoized()
class ViSelectionMode(Filter):
    def __call__(self, cli):
        if cli.editing_mode != EditingMode.VI:
            return False

        return bool(cli.current_buffer.selection_state)

    def __repr__(self):
        return 'ViSelectionMode()'


@memoized()
class ViWaitingForTextObjectMode(Filter):
    def __call__(self, cli):
        if cli.editing_mode != EditingMode.VI:
            return False

        return cli.vi_state.operator_func is not None

    def __repr__(self):
        return 'ViWaitingForTextObjectMode()'


@memoized()
class ViDigraphMode(Filter):
    def __call__(self, cli):
        if cli.editing_mode != EditingMode.VI:
            return False

        return cli.vi_state.waiting_for_digraph

    def __repr__(self):
        return 'ViDigraphMode()'


@memoized()
class EmacsMode(Filter):
    " When the Emacs bindings are active. "
    def __call__(self, cli):
        return cli.editing_mode == EditingMode.EMACS

    def __repr__(self):
        return 'EmacsMode()'


@memoized()
class EmacsInsertMode(Filter):
    def __call__(self, cli):
        if (cli.editing_mode != EditingMode.EMACS
                or cli.current_buffer.selection_state
                or cli.current_buffer.read_only()):
            return False
        return True

    def __repr__(self):
        return 'EmacsInsertMode()'


@memoized()
class EmacsSelectionMode(Filter):
    def __call__(self, cli):
        return (cli.editing_mode == EditingMode.EMACS
                and cli.current_buffer.selection_state)

    def __repr__(self):
        return 'EmacsSelectionMode()'