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) + 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