support for combining characters (now we have full unicode support)

This commit is contained in:
Mattias Andrée 2012-08-22 01:34:06 +02:00
parent 2227c0c5b7
commit 08fb7cc7e6

67
ponysay
View file

@ -271,11 +271,11 @@ class Ponysay():
self.__ucsise(ponies) self.__ucsise(ponies)
ponies.sort() ponies.sort()
width = len(max(ponies, key = len)) + 2 # Get the longest ponyfilename lenght + 2 spaces width = UCS.dispLen(max(ponies, key = UCS.dispLen)) + 2 # Get the longest ponyfilename lenght + 2 spaces
x = 0 x = 0
for pony in ponies: for pony in ponies:
spacing = ' ' * (width - len(pony)) spacing = ' ' * (width - UCS.dispLen(pony))
print(('\033[1m' + pony + '\033[21m' if (pony in quoters) else pony) + spacing, end='') # Print ponyfilename print(('\033[1m' + pony + '\033[21m' if (pony in quoters) else pony) + spacing, end='') # Print ponyfilename
x += width x += width
if x > (termsize[1] - width): # If too wide, make new line if x > (termsize[1] - width): # If too wide, make new line
@ -323,7 +323,7 @@ class Ponysay():
ponies = [] ponies = []
widths = [] widths = []
for pony in ponymap: for pony in ponymap:
w = len(pony) w = UCS.dispLen(pony)
item = '\033[1m' + pony + '\033[21m' if (pony in quoters) else pony item = '\033[1m' + pony + '\033[21m' if (pony in quoters) else pony
syms = ponymap[pony] syms = ponymap[pony]
if len(syms) > 0: if len(syms) > 0:
@ -331,7 +331,7 @@ class Ponysay():
item += ' (' item += ' ('
first = True first = True
for sym in syms: for sym in syms:
w += len(sym) w += UCS.dispLen(sym)
if not first: if not first:
item += ' ' item += ' '
else: else:
@ -420,11 +420,11 @@ class Ponysay():
balloons = list(balloonset) balloons = list(balloonset)
balloons.sort() balloons.sort()
width = len(max(balloons, key = len)) + 2 width = UCS.dispLen(max(balloons, key = UCS.dispLen)) + 2
x = 0 x = 0
for balloon in balloons: for balloon in balloons:
spacing = ' ' * (width - len(balloon)) spacing = ' ' * (width - UCS.dispLen(balloon))
print(balloon + spacing, end='') print(balloon + spacing, end='')
x += width x += width
if x > (termsize[1] - width): if x > (termsize[1] - width):
@ -961,13 +961,13 @@ class Balloon():
(self.sse, self.s, self.ssw) = (sse, s, ssw) (self.sse, self.s, self.ssw) = (sse, s, ssw)
(self.sww, self.w, self.nww) = (sww, w, nww) (self.sww, self.w, self.nww) = (sww, w, nww)
_ne = max(ne, key = len) _ne = max(ne, key = UCS.dispLen)
_nw = max(nw, key = len) _nw = max(nw, key = UCS.dispLen)
_se = max(se, key = len) _se = max(se, key = UCS.dispLen)
_sw = max(sw, key = len) _sw = max(sw, key = UCS.dispLen)
minE = len(max([_ne, nee, e, see, _se, ee], key = len)) minE = UCS.dispLen(max([_ne, nee, e, see, _se, ee], key = UCS.dispLen))
minW = len(max([_nw, nww, e, sww, _sw, ww], key = len)) minW = UCS.dispLen(max([_nw, nww, e, sww, _sw, ww], key = UCS.dispLen))
minN = len(max([ne, nne, n, nnw, nw], key = len)) minN = len(max([ne, nne, n, nnw, nw], key = len))
minS = len(max([se, sse, s, ssw, sw], key = len)) minS = len(max([se, sse, s, ssw, sw], key = len))
@ -1282,7 +1282,8 @@ class Backend():
i += len(self.__getcolour(input, i)) i += len(self.__getcolour(input, i))
else: else:
i += 1 i += 1
rc += 1 if not UCS.isCombining(c):
rc += 1
return rc return rc
@ -1355,7 +1356,8 @@ class Backend():
indentc += 1 indentc += 1
b[bi] = d b[bi] = d
bi += 1 bi += 1
cols += 1 if not UCS.isCombining(d):
cols += 1
map[cols] = bi map[cols] = bi
else: else:
mm = 0 mm = 0
@ -1492,6 +1494,43 @@ class ColourStack():
'''
UCS utility class
'''
class UCS():
'''
Checks whether a character is a combining character
'''
@staticmethod
def isCombining(char):
o = ord(char)
if (0x0300 <= o) and (o <= 0x036F): return True
if (0x20D0 <= o) and (o <= 0x20FF): return True
if (0x1DC0 <= o) and (o <= 0x1DFF): return True
if (0xFE20 <= o) and (o <= 0xFE2F): return True
return False
'''
Gets the number of combining characters in a string
'''
@staticmethod
def countCombining(string):
rc = 0
for char in string:
if UCS.isCombining(char):
rc += 1
return rc
'''
Gets length of a string not counting combining characters
'''
@staticmethod
def dispLen(string):
return len(string) - UCS.countCombining(string)
''' '''
The user's home directory The user's home directory