Skip to content

Commit ee047a2

Browse files
Add cursor shape support.
1 parent ea6b2c5 commit ee047a2

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

ptpython/layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def goto_next(mouse_event: MouseEvent) -> None:
151151
append_category(category)
152152

153153
for option in category.options:
154-
append(i, option.title, "%s" % option.get_current_value())
154+
append(i, option.title, "%s" % (option.get_current_value(),))
155155
i += 1
156156

157157
tokens.pop() # Remove last newline.

ptpython/python_input.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
ThreadedCompleter,
3535
merge_completers,
3636
)
37+
from prompt_toolkit.cursor_shapes import (
38+
AnyCursorShapeConfig,
39+
CursorShape,
40+
DynamicCursorShapeConfig,
41+
ModalCursorShapeConfig,
42+
)
3743
from prompt_toolkit.document import Document
3844
from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
3945
from prompt_toolkit.filters import Condition
@@ -325,6 +331,18 @@ def __init__(
325331
self.search_buffer: Buffer = Buffer()
326332
self.docstring_buffer: Buffer = Buffer(read_only=True)
327333

334+
# Cursor shapes.
335+
self.cursor_shape_config = "Block"
336+
self.all_cursor_shape_configs: Dict[str, AnyCursorShapeConfig] = {
337+
"Block": CursorShape.BLOCK,
338+
"Underline": CursorShape.UNDERLINE,
339+
"Beam": CursorShape.BEAM,
340+
"Modal (vi)": ModalCursorShapeConfig(),
341+
"Blink block": CursorShape.BLINKING_BLOCK,
342+
"Blink under": CursorShape.BLINKING_UNDERLINE,
343+
"Blink beam": CursorShape.BLINKING_BEAM,
344+
}
345+
328346
# Tokens to be shown at the prompt.
329347
self.prompt_style: str = "classic" # The currently active style.
330348

@@ -584,6 +602,16 @@ def get_values() -> dict[str, Callable[[], bool]]:
584602
"Vi": lambda: enable("vi_mode"),
585603
},
586604
),
605+
Option(
606+
title="Cursor shape",
607+
description="Change the cursor style, possibly according "
608+
"to the Vi input mode.",
609+
get_current_value=lambda: self.cursor_shape_config,
610+
get_values=lambda: dict(
611+
(s, partial(enable, "cursor_shape_config", s))
612+
for s in self.all_cursor_shape_configs
613+
),
614+
),
587615
simple_option(
588616
title="Paste mode",
589617
description="When enabled, don't indent automatically.",
@@ -896,6 +924,9 @@ def _create_application(
896924
style_transformation=self.style_transformation,
897925
include_default_pygments_style=False,
898926
reverse_vi_search_direction=True,
927+
cursor=DynamicCursorShapeConfig(
928+
lambda: self.all_cursor_shape_configs[self.cursor_shape_config]
929+
),
899930
input=input,
900931
output=output,
901932
)

0 commit comments

Comments
 (0)