本文整理汇总了Python中wcwidth.wcswidth方法的典型用法代码示例。如果您正苦于以下问题:Python wcwidth.wcswidth方法的具体用法?Python wcwidth.wcswidth怎么用?Python wcwidth.wcswidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wcwidth
的用法示例。
在下文中一共展示了wcwidth.wcswidth方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __str__
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [as 别名]
def __str__(self):
answer = ""
skip_next = False
for i, line in enumerate(self.field):
for j, c in enumerate(line):
fg_ansi = ""
bg_ansi = ""
stop = ""
if self.field[i][j].foreground:
fg_ansi = '\033[38;2;%s;%s;%sm' % rgb_from_str(self.field[i][j].foreground)
stop = colored.attr("reset")
if self.field[i][j].background:
bg_ansi = '\033[48;2;%s;%s;%sm' % rgb_from_str(self.field[i][j].background)
stop = colored.attr("reset")
char = c.char or " "
if not skip_next:
answer += fg_ansi + bg_ansi + char.encode('utf-8') + stop
skip_next = wcswidth(char) == 2
# answer += "...\n"
answer += "\n"
return answer
示例2: test_hello_jp
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [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 wcswidth [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: centre
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [as 别名]
def centre(self, text, y, colour=7, attr=0, colour_map=None):
"""
Centre the text on the specified line (y) using the optional colour and attributes.
:param text: The (single line) text to be printed.
:param y: The line (y coord) for the start of the text.
:param colour: The colour of the text to be displayed.
:param attr: The cell attribute of the text to be displayed.
:param colour_map: Colour/attribute list for multi-colour text.
The colours and attributes are the COLOUR_xxx and A_yyy constants
defined in the Screen class.
"""
if self._unicode_aware:
x = (self.width - wcswidth(text)) // 2
else:
x = (self.width - len(text)) // 2
self.paint(text, x, y, colour, attr, colour_map=colour_map)
示例5: print_status
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [as 别名]
def print_status(*args, fmt=fmt_status, wrap=True):
if not stderr_tty():
return
out = ""
out += "\033[1G" # cursor to column 1
#out += "\033[K" # erase to right (XXX: this was used in _truncated)
out += "\033[0J" # erase below
if args:
msg = " ".join(args)
msg = msg.replace("\n", " ")
if wrap:
out += fmt_status(msg)
lines = math.ceil(wcswidth(msg) / stderr_width())
if lines > 1:
out += "\033[%dA" % (lines-1) # cursor up 1
else:
msg = wctruncate(msg, stderr_width())
out += fmt_status(msg)
sys.stderr.write(out)
if not args:
sys.stderr.flush()
示例6: test_null_width_0
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [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
示例7: test_control_c0_width_negative_1
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [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
示例8: test_combining_width_negative_1
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [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
示例9: test_combining_enclosing
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [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
示例10: test_combining_spacing
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [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
示例11: _find_min_start
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [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
示例12: register_frame
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [as 别名]
def register_frame(self, frame):
"""
Register the Frame that owns this Widget.
:param frame: The owning Frame.
"""
self._frame = frame
self.string_len = wcswidth if self._frame.canvas.unicode_aware else len
示例13: _get_line_with_reprcrash_message
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [as 别名]
def _get_line_with_reprcrash_message(config, rep, termwidth):
"""Get summary line for a report, trying to add reprcrash message."""
from wcwidth import wcswidth
verbose_word = rep._get_verbose_word(config)
pos = _get_pos(config, rep)
line = "{} {}".format(verbose_word, pos)
len_line = wcswidth(line)
ellipsis, len_ellipsis = "...", 3
if len_line > termwidth - len_ellipsis:
# No space for an additional message.
return line
try:
msg = rep.longrepr.reprcrash.message
except AttributeError:
pass
else:
# Only use the first line.
i = msg.find("\n")
if i != -1:
msg = msg[:i]
len_msg = wcswidth(msg)
sep, len_sep = " - ", 3
max_len_msg = termwidth - len_line - len_sep
if max_len_msg >= len_ellipsis:
if len_msg > max_len_msg:
max_len_msg -= len_ellipsis
msg = msg[:max_len_msg]
while wcswidth(msg) > max_len_msg:
msg = msg[:-1]
msg += ellipsis
line += sep + msg
return line
示例14: test_combining_cafe
# 需要导入模块: import wcwidth [as 别名]
# 或者: from wcwidth import wcswidth [as 别名]
def test_combining_cafe():
u"""Phrase cafe + COMBINING ACUTE ACCENT is café of length 4."""
phrase = u"cafe\u0301"
expect_length_each = (1, 1, 1, 1, 0)
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