'-': [0, 7, 40, 36], # separator line between editor header and content
}
+wcswidth_cache = {}
+def cached_wcswidth(s):
+ if s not in wcswidth_cache:
+ wcswidth_cache[s] = wcwidth.wcswidth(s)
+ return wcswidth_cache[s]
+
class ColouredString:
def __init__(self, string, colour=' '):
if isinstance(string, ColouredString):
assert len(colour) == 1, "Colour ids are single characters"
colour = colour * len(string)
self.s, self.c = string, colour
+ self.width = cached_wcswidth(self.s)
def __add__(self, rhs):
rhs = type(self)(rhs)
def __len__(self):
return len(self.s)
- @property
- def width(self):
- return wcwidth.wcswidth(self.s)
-
def __str__(self):
return self.s
colour = self.c[pos]
fraglen = len(self.c) - pos - len(self.c[pos:].lstrip(colour))
frag = self.s[pos:pos+fraglen]
- yield frag, colour, wcwidth.wcswidth(frag)
+ yield frag, colour, cached_wcswidth(frag)
pos += fraglen
def split(self, width):