本文整理汇总了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
示例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
示例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
示例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
示例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.
示例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
示例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)
示例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)
示例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
示例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
示例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
示例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
示例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
示例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
示例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