Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Unicode wide characters #85

Open
keatonLiu opened this issue Dec 19, 2022 · 2 comments
Open

Add support for Unicode wide characters #85

keatonLiu opened this issue Dec 19, 2022 · 2 comments

Comments

@keatonLiu
Copy link

keatonLiu commented Dec 19, 2022

Modify MenuComponent class:

def _format_content(self, content='', align='left'):
    # remove color str which is invisible and get the actual len of content(wide char counts for 2)
    len_visible_chars = sum(get_width(ord(ch)) for ch in strip_color(content))
    len_invisible_chars = len(content) - len_visible_chars
    return '{lp}{text:{al}{width}}{rp}'.format(lp=' ' * self.padding.left,
                                               rp=' ' * self.padding.right,
                                               text=content, al=self._alignment_char(align),
                                               width=(self.calculate_border_width() - self.padding.left -
                                                      self.padding.right - 2 + len_invisible_chars))
@aegirhall
Copy link
Owner

Hi @keatonLiu,

On this line:

len_visible_chars = sum(get_width(ord(ch)) for ch in strip_color(content))

what package is get_width() from?

@keatonLiu
Copy link
Author

from ansiwrap import strip_color
from urwid import calc_width


def getVisibleLength(text: str):
   text = strip_color(text)
   return calc_width(text, 0, len(text))


def _format_content(self, content='', align='left'):
   # remove color str which is invisible and get the actual len of content(wide char counts for 2)
   len_visible_chars = getVisibleLength(content)
   len_invisible_chars = len(content) - len_visible_chars
   return '{lp}{text:{al}{width}}{rp}'.format(lp=' ' * self.padding.left,
                                              rp=' ' * self.padding.right,
                                              text=content, al=self._alignment_char(align),
                                              width=(self.calculate_border_width() - self.padding.left -
                                                     self.padding.right - 2 + len_invisible_chars))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants