Skip to content

Commit a7bb458

Browse files
committed
Integrate width awareness
- update display_linize to use curtsies 3.0 width functionality - correct cursor positioning on wrapped fullwidth chars by adding new method
1 parent 84d581a commit a7bb458

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def __init__(
402402
# so we're just using the same object
403403
self.interact = self.status_bar
404404

405-
# line currently being edited, without ps1 (usually '>>> ')
405+
# logical line currently being edited, without ps1 (usually '>>> ')
406406
self._current_line = ""
407407

408408
# current line of output - stdout and stdin go here
@@ -744,8 +744,9 @@ def process_key_event(self, e):
744744
)
745745
and self.config.curtsies_right_arrow_completion
746746
and self.cursor_offset == len(self.current_line)
747+
# if at end of current line and user presses RIGHT (to autocomplete)
747748
):
748-
749+
# then autocomplete
749750
self.current_line += self.current_suggestion
750751
self.cursor_offset = len(self.current_line)
751752
elif e in ("<UP>",) + key_dispatch[self.config.up_one_line_key]:
@@ -1435,6 +1436,23 @@ def current_output_line(self, value):
14351436
self.current_stdouterr_line = ""
14361437
self.stdin.current_line = "\n"
14371438

1439+
def number_of_padding_chars_on_current_cursor_line(self):
1440+
""" To avoid cutting off two-column characters at the end of lines where
1441+
there's only one column left, curtsies adds a padding char (u' ').
1442+
It's important to know about these for cursor positioning.
1443+
1444+
Should return zero unless there are fullwidth characters. """
1445+
full_line = self.current_cursor_line_without_suggestion
1446+
line_with_padding = "".join(
1447+
line.s
1448+
for line in paint.display_linize(
1449+
self.current_cursor_line_without_suggestion.s, self.width
1450+
)
1451+
)
1452+
1453+
# the difference in length here is how much padding there is
1454+
return len(line_with_padding) - len(full_line)
1455+
14381456
def paint(
14391457
self,
14401458
about_to_exit=False,
@@ -1620,7 +1638,8 @@ def move_screen_up(current_line_start_row):
16201638
wcswidth(self.current_cursor_line_without_suggestion.s)
16211639
- wcswidth(self.current_line)
16221640
+ wcswidth(self.current_line[: self.cursor_offset])
1623-
),
1641+
)
1642+
+ self.number_of_padding_chars_on_current_cursor_line(),
16241643
width,
16251644
)
16261645
assert cursor_column >= 0, (

bpython/curtsiesfrontend/replpainter.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,10 @@ def display_linize(msg, columns, blank_line=False):
2626
"""Returns lines obtained by splitting msg over multiple lines.
2727
2828
Warning: if msg is empty, returns an empty list of lines"""
29-
display_lines = (
30-
[
31-
msg[start:end]
32-
for start, end in zip(
33-
range(0, len(msg), columns),
34-
range(columns, len(msg) + columns, columns),
35-
)
36-
]
37-
if msg
38-
else ([""] if blank_line else [])
39-
)
40-
return display_lines
29+
if not msg:
30+
return [''] if blank_line else []
31+
msg = fmtstr(msg)
32+
return list(msg.width_aware_splitlines(columns))
4133

4234

4335
def paint_history(rows, columns, display_lines):

0 commit comments

Comments
 (0)