|
| 1 | +import sys |
| 2 | + |
1 | 3 | from prompt_toolkit.application import get_app
|
2 | 4 | from prompt_toolkit.document import Document
|
3 | 5 | from prompt_toolkit.enums import DEFAULT_BUFFER
|
|
11 | 13 | )
|
12 | 14 | from prompt_toolkit.key_binding import KeyBindings
|
13 | 15 | from prompt_toolkit.keys import Keys
|
| 16 | +from prompt_toolkit.key_binding.vi_state import InputMode, ViState |
14 | 17 |
|
15 | 18 | from .utils import document_is_multiline_python
|
16 | 19 |
|
@@ -319,3 +322,32 @@ def auto_newline(buffer):
|
319 | 322 | if current_line[-1:] == ":":
|
320 | 323 | for x in range(4):
|
321 | 324 | insert_text(" ")
|
| 325 | + |
| 326 | + |
| 327 | +def get_input_mode(self): |
| 328 | + if sys.version_info[0] == 3: |
| 329 | + # Decrease input flush timeout from 500ms to 10ms. |
| 330 | + app = get_app() |
| 331 | + app.ttimeoutlen = 0.01 |
| 332 | + # Decrease handler call timeout from 1s to 400ms |
| 333 | + app.timeoutlen = 0.4 |
| 334 | + |
| 335 | + return self._input_mode |
| 336 | + |
| 337 | + |
| 338 | +def set_input_mode(self, mode): |
| 339 | + shape = {InputMode.NAVIGATION: 2, InputMode.REPLACE: 4}.get(mode, 6) |
| 340 | + cursor = "\x1b[{} q".format(shape) |
| 341 | + |
| 342 | + if hasattr(sys.stdout, "_cli"): |
| 343 | + write = sys.stdout._cli.output.write_raw |
| 344 | + else: |
| 345 | + write = sys.stdout.write |
| 346 | + |
| 347 | + write(cursor) |
| 348 | + sys.stdout.flush() |
| 349 | + |
| 350 | + self._input_mode = mode |
| 351 | + |
| 352 | +ViState._input_mode = InputMode.INSERT |
| 353 | +ViState.input_mode = property(get_input_mode, set_input_mode) |
0 commit comments