From b4a006ff16d2c4099b064a4a370b6bf65577fa4c Mon Sep 17 00:00:00 2001 From: Leta Montopoli Date: Wed, 19 Nov 2014 14:24:43 -0500 Subject: [PATCH 1/2] Added the combining-character-browser for checking observed width in terminal --- bin/wcwidth-browser.py | 7 +++---- bin/wcwidth-combining-browser.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 bin/wcwidth-combining-browser.py diff --git a/bin/wcwidth-browser.py b/bin/wcwidth-browser.py index 69b0d8f..93f73e5 100755 --- a/bin/wcwidth-browser.py +++ b/bin/wcwidth-browser.py @@ -389,17 +389,16 @@ def text_entry(self, ucs, name): return fmt.format(name_len=style.name_len, delimiter=delimiter, name=name, ucs=ucs, - value=ord(ucs)) + value=ord(ucs if len(ucs) == 1 else ucs[1])) -def main(): +def main(character_factory): term = Terminal() style = Style(heading=term.magenta, hint=term.bright_cyan, delimiter=u'|', ) if term.number_of_colors else Style() screen = Screen(term, style) - character_factory = WcWideCharacterGenerator pager = Pager(term, screen, character_factory) if term.is_a_tty: signal.signal(signal.SIGWINCH, pager.on_resize) @@ -410,4 +409,4 @@ def main(): pager.run(writer=echo, reader=term.inkey) if __name__ == '__main__': - main() + main(WcWideCharacterGenerator) diff --git a/bin/wcwidth-combining-browser.py b/bin/wcwidth-combining-browser.py new file mode 100644 index 0000000..a6babb8 --- /dev/null +++ b/bin/wcwidth-combining-browser.py @@ -0,0 +1,32 @@ +browser = __import__('wcwidth-browser') +import string +import unicodedata +from wcwidth import wcwidth, table_comb + + +class WcCombinedCharacterGenerator(object): + def __init__(self, width=1): + self.characters = [] + for boundaries in table_comb.NONZERO_COMBINING: + for i in range(boundaries[0], boundaries[1]+1): + self.characters.append(u'o' + browser.unichr(i)) + self.characters.reverse() + + def __next__(self): + while True: + if not self.characters: + raise StopIteration + ucs = self.characters.pop() + try: + name = string.capwords(unicodedata.name(ucs[1])) + except ValueError: + continue + return (ucs, name) + + # python 2.6 - 3.3 compatibility + next = __next__ + + +if __name__ == '__main__': + browser.main(WcCombinedCharacterGenerator) + From 195c4ab71173e2ffc3a68c514a2126337badadb4 Mon Sep 17 00:00:00 2001 From: Thomas Ballinger Date: Wed, 19 Nov 2014 14:37:00 -0500 Subject: [PATCH 2/2] take combining characters into account --- wcwidth/wcwidth.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wcwidth/wcwidth.py b/wcwidth/wcwidth.py index d7fe1db..8ed518c 100644 --- a/wcwidth/wcwidth.py +++ b/wcwidth/wcwidth.py @@ -182,6 +182,9 @@ def wcswidth(pwcs, n=None): for char in pwcs[idx]: wcw = wcwidth(char) if wcw < 0: + ucs = ord(char) + if _bisearch(ucs, NONZERO_COMBINING): + continue return -1 else: width += wcw