Skip to content

Commit

Permalink
use wcwidth (if available)
Browse files Browse the repository at this point in the history
  • Loading branch information
obfusk committed Feb 25, 2021
1 parent 39a6bda commit eba676a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
31 changes: 25 additions & 6 deletions pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,29 @@ def decode(x, enc = sys.stdout.encoding):
decode = lambda x, _ = None: x


def width(c):
return 2 if unicodedata.east_asian_width(c) in "FW" else 1
def wlen(s):
return sum(map(width, s))
try:
from wcwidth import width
except ImportError:
try:
from wcwidth import wcwidth
except ImportError:
import unicodedata

def wcwidth(c):
if unicodedata.east_asian_width(c) in "FW":
return 2
if unicodedata.category(c)[0] == "C":
return 0
return 1

try:
from functools import lru_cache
except ImportError:
lru_cache = lambda *a, **k: lambda f: f

@lru_cache(maxsize=1000)
def width(s):
return sum(max(0, wcwidth(c)) for c in s)


_r_csi_seq = re.compile(r"\033\[[ -@]*[A-~]")
Expand Down Expand Up @@ -303,7 +322,7 @@ def calc_screen(self):
p -= ll + 1
prompt, lp = self.process_prompt(prompt)
l, l2 = disp_str(line)
wrapcount = (wlen(l) + lp) // w
wrapcount = (width(l) + lp) // w
if wrapcount == 0:
screen.append(prompt + l)
screeninfo.append((lp, l2 + [1]))
Expand Down Expand Up @@ -347,7 +366,7 @@ def process_prompt(self, prompt):
# They are CSI (or ANSI) sequences ( ESC [ ... LETTER )

out_prompt = ''
l = wlen(prompt)
l = sum(max(1, width(c)) for c in prompt) # FIXME
pos = 0
while True:
s = prompt.find('\x01', pos)
Expand Down
38 changes: 28 additions & 10 deletions pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import termios, select, os, struct, errno
import signal, re, time, sys
import unicodedata
from fcntl import ioctl
from . import curses
from .fancy_termios import tcgetattr, tcsetattr
Expand All @@ -44,10 +43,29 @@ class InvalidTerminal(RuntimeError):
unicode = str


def width(c):
return 2 if unicodedata.east_asian_width(c) in "FW" else 1
def wlen(s):
return sum(map(width, s))
try:
from wcwidth import width
except ImportError:
try:
from wcwidth import wcwidth
except ImportError:
import unicodedata

def wcwidth(c):
if unicodedata.east_asian_width(c) in "FW":
return 2
if unicodedata.category(c)[0] == "C":
return 0
return 1

try:
from functools import lru_cache
except ImportError:
lru_cache = lambda *a, **k: lambda f: f

@lru_cache(maxsize=1000)
def width(s):
return sum(max(0, wcwidth(c)) for c in s)


_error = (termios.error, curses.error, InvalidTerminal)
Expand Down Expand Up @@ -266,7 +284,7 @@ def __write_changed_line(self, y, oldline, newline, px):
# avoid writing code generators these days...)
x = 0
i = 0
minlen = min(wlen(oldline), wlen(newline))
minlen = min(width(oldline), width(newline))
pi = 0
xx = 0
for c in oldline:
Expand Down Expand Up @@ -295,8 +313,8 @@ def __write_changed_line(self, y, oldline, newline, px):
self.__move(x, y)
self.__write(newline[i])
self.__posxy = x + width(newline[i]), y
elif (self.dch1 and self.ich1 and wlen(newline) == self.width
and x < wlen(newline) - 2
elif (self.dch1 and self.ich1 and width(newline) == self.width
and x < width(newline) - 2
and newline[i+1:] == oldline[i:-1]):
cw = width(newline[i])
self.__hide_cursor()
Expand All @@ -310,10 +328,10 @@ def __write_changed_line(self, y, oldline, newline, px):
else:
self.__hide_cursor()
self.__move(x, y)
if wlen(oldline) > wlen(newline):
if width(oldline) > width(newline):
self.__write_code(self._el)
self.__write(newline[i:])
self.__posxy = wlen(newline), y
self.__posxy = width(newline), y

if '\x1b' in newline:
# ANSI escape characters are present, so we can't assume
Expand Down

0 comments on commit eba676a

Please sign in to comment.