Skip to content

Commit 8e25e01

Browse files
authored
Corrected cursor positioning on inputs with full-width (2 column) characters (#817)
- Added functionality for full-width character input - Works with normal input and stdin input - There still are issues with line wrapping when using full-width - Fix wide chars on input('...')
1 parent 367c2f8 commit 8e25e01

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from bpython._py3compat import PythonLexer
2020
from pygments.formatters import TerminalFormatter
2121

22+
from wcwidth import wcswidth
23+
2224
import blessings
2325

2426
import curtsies
@@ -1359,7 +1361,11 @@ def display_line_with_prompt(self):
13591361

13601362
@property
13611363
def current_cursor_line_without_suggestion(self):
1362-
"""Current line, either output/input or Python prompt + code"""
1364+
"""
1365+
Current line, either output/input or Python prompt + code
1366+
1367+
:returns: FmtStr
1368+
"""
13631369
value = self.current_output_line + (
13641370
"" if self.coderunner.running else self.display_line_with_prompt
13651371
)
@@ -1556,7 +1562,8 @@ def move_screen_up(current_line_start_row):
15561562

15571563
if self.stdin.has_focus:
15581564
cursor_row, cursor_column = divmod(
1559-
len(self.current_stdouterr_line) + self.stdin.cursor_offset,
1565+
wcswidth(self.current_stdouterr_line)
1566+
+ wcswidth(self.stdin.current_line[: self.stdin.cursor_offset]),
15601567
width,
15611568
)
15621569
assert cursor_column >= 0, cursor_column
@@ -1574,12 +1581,12 @@ def move_screen_up(current_line_start_row):
15741581
len(self.current_line),
15751582
self.cursor_offset,
15761583
)
1577-
else:
1584+
else: # Common case for determining cursor position
15781585
cursor_row, cursor_column = divmod(
15791586
(
1580-
len(self.current_cursor_line_without_suggestion)
1581-
- len(self.current_line)
1582-
+ self.cursor_offset
1587+
wcswidth(self.current_cursor_line_without_suggestion.s)
1588+
- wcswidth(self.current_line)
1589+
+ wcswidth(self.current_line[: self.cursor_offset])
15831590
),
15841591
width,
15851592
)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def initialize_options(self):
228228
"curtsies >=0.1.18",
229229
"greenlet",
230230
"six >=1.5",
231+
"wcwidth"
231232
]
232233

233234
extras_require = {

0 commit comments

Comments
 (0)