Skip to content

Commit 602e467

Browse files
committed
cursor shape changes with vi editing mode
1 parent 86e1571 commit 602e467

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-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

@@ -201,6 +204,35 @@ def _(event):
201204
" Abort when Control-C has been pressed. "
202205
event.app.exit(exception=KeyboardInterrupt, style="class:aborting")
203206

207+
208+
209+
def get_input_mode(self):
210+
if sys.version_info[0] == 3:
211+
app = get_app()
212+
app.ttimeoutlen = python_input.ttimeoutlen
213+
app.timeoutlen = python_input.timeoutlen
214+
215+
return self._input_mode
216+
217+
218+
def set_input_mode(self, mode):
219+
shape = {InputMode.NAVIGATION: 2, InputMode.REPLACE: 4}.get(mode, 6)
220+
cursor = "\x1b[{} q".format(shape)
221+
222+
if hasattr(sys.stdout, "_cli"):
223+
write = sys.stdout._cli.output.write_raw
224+
else:
225+
write = sys.stdout.write
226+
227+
write(cursor)
228+
sys.stdout.flush()
229+
230+
self._input_mode = mode
231+
232+
if python_input.enable_modal_cursor:
233+
ViState._input_mode = InputMode.INSERT
234+
ViState.input_mode = property(get_input_mode, set_input_mode)
235+
204236
return bindings
205237

206238

ptpython/python_input.py

+3
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ def __init__(
234234
self.enable_system_bindings: bool = True
235235
self.enable_input_validation: bool = True
236236
self.enable_auto_suggest: bool = False
237+
self.enable_modal_cursor: bool = True
238+
self.ttimeoutlen: float = 0.01
239+
self.timeoutlen: float = 0.5
237240
self.enable_mouse_support: bool = False
238241
self.enable_history_search: bool = False # When True, like readline, going
239242
# back in history will filter the

0 commit comments

Comments
 (0)