当前位置: 首页>>代码示例>>Python>>正文


Python wcwidth.wcwidth方法代码示例

本文整理汇总了Python中wcwidth.wcwidth方法的典型用法代码示例。如果您正苦于以下问题:Python wcwidth.wcwidth方法的具体用法?Python wcwidth.wcwidth怎么用?Python wcwidth.wcwidth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在wcwidth的用法示例。


在下文中一共展示了wcwidth.wcwidth方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _enforce_width

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def _enforce_width(text, width, unicode_aware=True):
    """
    Enforce a displayed piece of text to be a certain number of cells wide.  This takes into
    account double-width characters used in CJK languages.

    :param text: The text to be truncated
    :param width: The screen cell width to enforce
    :return: The resulting truncated text
    """
    # Double-width strings cannot be more than twice the string length, so no need to try
    # expensive truncation if this upper bound isn't an issue.
    if (2 * len(text) < width) or (len(text) < width and not unicode_aware):
        return text

    # Can still optimize performance if we are not handling unicode characters.
    if unicode_aware:
        size = 0
        for i, c in enumerate(str(text)):
            w = wcwidth(c) if ord(c) >= 256 else 1
            if size + w > width:
                return text[0:i]
            size += w
    elif len(text) + 1 > width:
        return text[0:width]
    return text 
开发者ID:peterbrittain,项目名称:asciimatics,代码行数:27,代码来源:widgets.py

示例2: test_hello_jp

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def test_hello_jp():
    u"""
    Width of Japanese phrase: コンニチハ, セカイ!

    Given a phrase of 5 and 3 Katakana ideographs, joined with
    3 English-ASCII punctuation characters, totaling 11, this
    phrase consumes 19 cells of a terminal emulator.
    """
    # given,
    phrase = u'コンニチハ, セカイ!'
    expect_length_each = (2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1)
    expect_length_phrase = sum(expect_length_each)

    # exercise,
    length_each = tuple(map(wcwidth.wcwidth, phrase))
    length_phrase = wcwidth.wcswidth(phrase)

    # verify,
    assert length_each == expect_length_each
    assert length_phrase == expect_length_phrase 
开发者ID:Wramberg,项目名称:TerminalView,代码行数:22,代码来源:test_core.py

示例3: test_wcswidth_substr

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def test_wcswidth_substr():
    """
    Test wcswidth() optional 2nd parameter, ``n``.

    ``n`` determines at which position of the string
    to stop counting length.
    """
    # given,
    phrase = u'コンニチハ, セカイ!'
    end = 7
    expect_length_each = (2, 2, 2, 2, 2, 1, 1,)
    expect_length_phrase = sum(expect_length_each)

    # exercise,
    length_phrase = wcwidth.wcswidth(phrase, end)

    # verify,
    assert length_phrase == expect_length_phrase 
开发者ID:Wramberg,项目名称:TerminalView,代码行数:20,代码来源:test_core.py

示例4: _get_offset

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def _get_offset(text, visible_width, unicode_aware=True):
    """
    Find the character offset within some text for a given visible offset (taking into account the
    fact that some character glyphs are double width).

    :param text: The text to analyze
    :param visible_width: The required location within that text (as seen on screen).
    :return: The offset within text (as a character offset within the string).
    """
    result = 0
    width = 0
    if unicode_aware:
        for c in text:
            if visible_width - width <= 0:
                break
            result += 1
            width += wcwidth(c)
        if visible_width - width < 0:
            result -= 1
    else:
        result = min(len(text), visible_width)
    return result 
开发者ID:peterbrittain,项目名称:asciimatics,代码行数:24,代码来源:widgets.py

示例5: length

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def length(self):
        r"""
        Return the printable length of string containing sequences.

        Strings containing ``term.left`` or ``\b`` will cause "overstrike",
        but a length less than 0 is not ever returned. So ``_\b+`` is a
        length of 1 (displays as ``+``), but ``\b`` alone is simply a
        length of 0.

        Some characters may consume more than one cell, mainly those CJK
        Unified Ideographs (Chinese, Japanese, Korean) defined by Unicode
        as half or full-width characters.
        """
        # because control characters may return -1, "clip" their length to 0.
        clip = functools.partial(max, 0)
        return sum(clip(wcwidth.wcwidth(w_char))
                   for w_char in self.strip_seqs())

    # we require ur"" for the docstring, but it is not supported by all
    # python versions. 
开发者ID:QData,项目名称:deepWordBug,代码行数:22,代码来源:sequences.py

示例6: rev_wcwidth

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def rev_wcwidth(text, width):
    """
    Given a text, return the location such that the substring has width `width`.
    """
    if width == 0:
        return -1

    w = 0
    i = -1
    # loop over to check for double width chars
    for i, c in enumerate(text):
        w += wcwidth(c)
        if w >= width:
            break
    if w >= width:
        return i
    else:
        return i + width - w 
开发者ID:randy3k,项目名称:Terminus,代码行数:20,代码来源:utils.py

示例7: character_width

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def character_width(char):
    r"""
    Determine the width that a character is likely to be displayed as in
    a monospaced terminal. The width for a printable character will
    always be 0, 1, or 2.

    Nonprintable or control characters will return -1, a convention that comes
    from wcwidth.

    >>> character_width('車')
    2
    >>> character_width('A')
    1
    >>> character_width('\N{ZERO WIDTH JOINER}')
    0
    >>> character_width('\n')
    -1
    """
    return wcwidth(char) 
开发者ID:morpheus65535,项目名称:bazarr,代码行数:21,代码来源:formatting.py

示例8: _wc_hard_wrap

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def _wc_hard_wrap(line, length):
    """
    Wrap text to length characters, breaking when target length is reached,
    taking into account character width.

    Used to wrap lines which cannot be wrapped on whitespace.
    """
    chars = []
    chars_len = 0
    for char in line:
        char_len = wcwidth(char)
        if chars_len + char_len > length:
            yield "".join(chars)
            chars = []
            chars_len = 0

        chars.append(char)
        chars_len += char_len

    if chars:
        yield "".join(chars) 
开发者ID:ihabunek,项目名称:toot,代码行数:23,代码来源:wcstring.py

示例9: __missing__

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def __missing__(self, string):
        # Note: We use the `max(0, ...` because some non printable control
        #       characters, like e.g. Ctrl-underscore get a -1 wcwidth value.
        #       It can be possible that these characters end up in the input
        #       text.
        if len(string) == 1:
            result = max(0, wcwidth(string))
        else:
            result = sum(max(0, wcwidth(c)) for c in string)

        # Cache for short strings.
        # (It's hard to tell what we can consider short...)
        if len(string) < 256:
            self[string] = result

        return result 
开发者ID:bkerler,项目名称:android_universal,代码行数:18,代码来源:utils.py

示例10: test_null_width_0

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def test_null_width_0():
    """NULL (0) reports width 0."""
    # given,
    phrase = u'abc\x00def'
    expect_length_each = (1, 1, 1, 0, 1, 1, 1)
    expect_length_phrase = sum(expect_length_each)

    # exercise,
    length_each = tuple(map(wcwidth.wcwidth, phrase))
    length_phrase = wcwidth.wcswidth(phrase, len(phrase))

    # verify,
    assert length_each == expect_length_each
    assert length_phrase == expect_length_phrase 
开发者ID:Wramberg,项目名称:TerminalView,代码行数:16,代码来源:test_core.py

示例11: test_control_c0_width_negative_1

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def test_control_c0_width_negative_1():
    """CSI (Control sequence initiate) reports width -1."""
    # given,
    phrase = u'\x1b[0m'
    expect_length_each = (-1, 1, 1, 1)
    expect_length_phrase = -1

    # exercise,
    length_each = tuple(map(wcwidth.wcwidth, phrase))
    length_phrase = wcwidth.wcswidth(phrase, len(phrase))

    # verify,
    assert length_each == expect_length_each
    assert length_phrase == expect_length_phrase 
开发者ID:Wramberg,项目名称:TerminalView,代码行数:16,代码来源:test_core.py

示例12: test_combining_width_negative_1

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def test_combining_width_negative_1():
    """Simple test combining reports total width of 4."""
    # given,
    phrase = u'--\u05bf--'
    expect_length_each = (1, 1, 0, 1, 1)
    expect_length_phrase = 4

    # exercise,
    length_each = tuple(map(wcwidth.wcwidth, phrase))
    length_phrase = wcwidth.wcswidth(phrase, len(phrase))

    # verify,
    assert length_each == expect_length_each
    assert length_phrase == expect_length_phrase 
开发者ID:Wramberg,项目名称:TerminalView,代码行数:16,代码来源:test_core.py

示例13: test_combining_enclosing

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def test_combining_enclosing():
    u"""CYRILLIC CAPITAL LETTER A + COMBINING CYRILLIC HUNDRED THOUSANDS SIGN is А҈ of length 1."""
    phrase = u"\u0410\u0488"
    expect_length_each = (1, 0)
    expect_length_phrase = 1

    # exercise,
    length_each = tuple(map(wcwidth.wcwidth, phrase))
    length_phrase = wcwidth.wcswidth(phrase, len(phrase))

    # verify,
    assert length_each == expect_length_each
    assert length_phrase == expect_length_phrase 
开发者ID:Wramberg,项目名称:TerminalView,代码行数:15,代码来源:test_core.py

示例14: test_combining_spacing

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def test_combining_spacing():
    u"""Balinese kapal (ship) is ᬓᬨᬮ᭄ of length 4."""
    phrase = u"\u1B13\u1B28\u1B2E\u1B44"
    expect_length_each = (1, 1, 1, 1)
    expect_length_phrase = 4

    # exercise,
    length_each = tuple(map(wcwidth.wcwidth, phrase))
    length_phrase = wcwidth.wcswidth(phrase, len(phrase))

    # verify,
    assert length_each == expect_length_each
    assert length_phrase == expect_length_phrase 
开发者ID:Wramberg,项目名称:TerminalView,代码行数:15,代码来源:test_core.py

示例15: _find_min_start

# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcwidth [as 别名]
def _find_min_start(text, max_width, unicode_aware=True, at_end=False):
    """
    Find the starting point in the string that will reduce it to be less than or equal to the
    specified width when displayed on screen.

    :param text: The text to analyze.
    :param max_width: The required maximum width
    :param at_end: At the end of the editable line, so allow spaced for cursor.

    :return: The offset within `text` to start at to reduce it to the required length.
    """
    # Is the solution trivial?  Worth optimizing for text heavy UIs...
    if 2 * len(text) < max_width:
        return 0

    # OK - do it the hard way...
    result = 0
    string_len = wcswidth if unicode_aware else len
    char_len = wcwidth if unicode_aware else lambda x: 1
    display_end = string_len(text)
    while display_end > max_width:
        result += 1
        display_end -= char_len(text[0])
        text = text[1:]
    if at_end and display_end == max_width:
        result += 1
    return result 
开发者ID:peterbrittain,项目名称:asciimatics,代码行数:29,代码来源:widgets.py


注:本文中的wcwidth.wcwidth方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。