Skip to content

Commit 49dc5a6

Browse files
committed
cursor shape changes with vi editing mode
1 parent 86e1571 commit 49dc5a6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

ptpython/key_bindings.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
from prompt_toolkit.application import get_app
24
from prompt_toolkit.document import Document
35
from prompt_toolkit.enums import DEFAULT_BUFFER
@@ -11,6 +13,7 @@
1113
)
1214
from prompt_toolkit.key_binding import KeyBindings
1315
from prompt_toolkit.keys import Keys
16+
from prompt_toolkit.key_binding.vi_state import InputMode, ViState
1417

1518
from .utils import document_is_multiline_python
1619

@@ -319,3 +322,32 @@ def auto_newline(buffer):
319322
if current_line[-1:] == ":":
320323
for x in range(4):
321324
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

Comments
 (0)