def cursor_left(self, n=1):
"""ESCnD CUB (Cursor Back)"""
# Commented out to save CPU (and the others below too)
#logging.debug('cursor_left(%s)' % n)
n = int(n)
# This logic takes care of double-width unicode characters
if self.double_width_left:
self.double_width_left = False
return
self.cursorX = max(0, self.cursorX - n) # Ensures positive value
try:
char = self.screen[self.cursorY][self.cursorX]
except IndexError: # Cursor is past the right-edge of the screen; ignore
char = u' ' # This is a safe default/fallback
if unicodedata.east_asian_width(char) == 'W':
# This lets us skip the next call (get called 2x for 2x width)
self.double_width_left = True
try:
for callback in self.callbacks[CALLBACK_CURSOR_POS].values():
callback()
except TypeError:
pass
python类east_asian_width()的实例源码
def cursor_right(self, n=1):
"""ESCnC CUF (Cursor Forward)"""
#logging.debug('cursor_right(%s)' % n)
if not n:
n = 1
n = int(n)
# This logic takes care of double-width unicode characters
if self.double_width_right:
self.double_width_right = False
return
self.cursorX += n
try:
char = self.screen[self.cursorY][self.cursorX]
except IndexError: # Cursor is past the right-edge of the screen; ignore
char = u' ' # This is a safe default/fallback
if unicodedata.east_asian_width(char) == 'W':
# This lets us skip the next call (get called 2x for 2x width)
self.double_width_right = True
try:
for callback in self.callbacks[CALLBACK_CURSOR_POS].values():
callback()
except TypeError:
pass
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
roberteldersoftwarediff.py 文件源码
项目:roberteldersoftwarediff
作者: RobertElderSoftware
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def get_east_asian_width(unicode_str):
r = unicodedata.east_asian_width(unicode_str)
if r == "F": # Fullwidth
return 1
elif r == "H": # Half-width
return 1
elif r == "W": # Wide
return 2
elif r == "Na": # Narrow
return 1
elif r == "A": # Ambiguous, go with 2
return 1
elif r == "N": # Neutral
return 1
else:
return 1
def pad_double_width(self, pad_char):
"""
Pad all double-width characters in self by appending `pad_char` to each.
For East Asian language support.
"""
if hasattr(unicodedata, 'east_asian_width'):
east_asian_width = unicodedata.east_asian_width
else:
return # new in Python 2.4
for i in range(len(self.data)):
line = self.data[i]
if isinstance(line, unicode):
new = []
for char in line:
new.append(char)
if east_asian_width(char) in 'WF': # 'W'ide & 'F'ull-width
new.append(pad_char)
self.data[i] = ''.join(new)
def pad_double_width(self, pad_char):
"""
Pad all double-width characters in self by appending `pad_char` to each.
For East Asian language support.
"""
if hasattr(unicodedata, 'east_asian_width'):
east_asian_width = unicodedata.east_asian_width
else:
return # new in Python 2.4
for i in range(len(self.data)):
line = self.data[i]
if isinstance(line, unicode):
new = []
for char in line:
new.append(char)
if east_asian_width(char) in 'WF': # 'W'ide & 'F'ull-width
new.append(pad_char)
self.data[i] = ''.join(new)
def pad_double_width(self, pad_char):
"""
Pad all double-width characters in self by appending `pad_char` to each.
For East Asian language support.
"""
if hasattr(unicodedata, 'east_asian_width'):
east_asian_width = unicodedata.east_asian_width
else:
return # new in Python 2.4
for i in range(len(self.data)):
line = self.data[i]
if isinstance(line, unicode):
new = []
for char in line:
new.append(char)
if east_asian_width(char) in 'WF': # 'W'ide & 'F'ull-width
new.append(pad_char)
self.data[i] = ''.join(new)
def cursor_left(self, n=1):
"""ESCnD CUB (Cursor Back)"""
# Commented out to save CPU (and the others below too)
#logging.debug('cursor_left(%s)' % n)
n = int(n)
# This logic takes care of double-width unicode characters
if self.double_width_left:
self.double_width_left = False
return
self.cursorX = max(0, self.cursorX - n) # Ensures positive value
try:
char = self.screen[self.cursorY][self.cursorX]
except IndexError: # Cursor is past the right-edge of the screen; ignore
char = u' ' # This is a safe default/fallback
if unicodedata.east_asian_width(char) == 'W':
# This lets us skip the next call (get called 2x for 2x width)
self.double_width_left = True
try:
for callback in self.callbacks[CALLBACK_CURSOR_POS].values():
callback()
except TypeError:
pass
def cursor_right(self, n=1):
"""ESCnC CUF (Cursor Forward)"""
#logging.debug('cursor_right(%s)' % n)
if not n:
n = 1
n = int(n)
# This logic takes care of double-width unicode characters
if self.double_width_right:
self.double_width_right = False
return
self.cursorX += n
try:
char = self.screen[self.cursorY][self.cursorX]
except IndexError: # Cursor is past the right-edge of the screen; ignore
char = u' ' # This is a safe default/fallback
if unicodedata.east_asian_width(char) == 'W':
# This lets us skip the next call (get called 2x for 2x width)
self.double_width_right = True
try:
for callback in self.callbacks[CALLBACK_CURSOR_POS].values():
callback()
except TypeError:
pass
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
def pad_double_width(self, pad_char):
"""
Pad all double-width characters in self by appending `pad_char` to each.
For East Asian language support.
"""
if hasattr(unicodedata, 'east_asian_width'):
east_asian_width = unicodedata.east_asian_width
else:
return # new in Python 2.4
for i in range(len(self.data)):
line = self.data[i]
if isinstance(line, str):
new = []
for char in line:
new.append(char)
if east_asian_width(char) in 'WF': # 'W'ide & 'F'ull-width
new.append(pad_char)
self.data[i] = ''.join(new)
def pad_double_width(self, pad_char):
"""
Pad all double-width characters in self by appending `pad_char` to each.
For East Asian language support.
"""
if hasattr(unicodedata, 'east_asian_width'):
east_asian_width = unicodedata.east_asian_width
else:
return # new in Python 2.4
for i in range(len(self.data)):
line = self.data[i]
if isinstance(line, unicode):
new = []
for char in line:
new.append(char)
if east_asian_width(char) in 'WF': # 'W'ide & 'F'ull-width
new.append(pad_char)
self.data[i] = ''.join(new)
def sub_str(value, length, suffix=r'...'):
result = []
strlen = 0
for val in value:
if(unicodedata.east_asian_width(val) in (r'A',r'F',r'W')):
strlen += 2
else:
strlen += 1
if(strlen > length):
if(suffix):
result.append(suffix)
break
result.append(val)
return r''.join(result)
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
StylisticFingerprinting.py 文件源码
项目:StylisticFingerprinting
作者: robertyuyang
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def GetLineWidth(line):
"""Determines the width of the line in column positions.
Args:
line: A string, which may be a Unicode string.
Returns:
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
width += 2
elif not unicodedata.combining(uc):
width += 1
return width
else:
return len(line)
statemachine.py 文件源码
项目:tf_aws_ecs_instance_draining_on_scale_in
作者: terraform-community-modules
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def pad_double_width(self, pad_char):
"""
Pad all double-width characters in self by appending `pad_char` to each.
For East Asian language support.
"""
if hasattr(unicodedata, 'east_asian_width'):
east_asian_width = unicodedata.east_asian_width
else:
return # new in Python 2.4
for i in range(len(self.data)):
line = self.data[i]
if isinstance(line, unicode):
new = []
for char in line:
new.append(char)
if east_asian_width(char) in 'WF': # 'W'ide & 'F'ull-width
new.append(pad_char)
self.data[i] = ''.join(new)
def width(s):
asian = sum(unicodedata.east_asian_width(c) in {'W', 'F'} for c in s)
return len(s) + asian
def string_width(string):
"""Get the visible width of a unicode string.
Some CJK unicode characters are more than one byte unlike ASCII and latin unicode characters.
From: https://github.com/Robpol86/terminaltables/pull/9
:param str string: String to measure.
:return: String's width.
:rtype: int
"""
# Colorclass instance.
if hasattr(string, 'value_no_colors'):
string = string.value_no_colors
# Convert to unicode.
try:
decoded = string.decode('u8')
except (AttributeError, UnicodeEncodeError):
decoded = string
width = 0
for char in decoded:
if unicodedata.east_asian_width(char) in ('F', 'W'):
width += 2
else:
width += 1
return width
def get_ascii_char_width(unicode_str, east_asian_ambiguous_width=1):
import unicodedata
width = 0
for char in unicode_str:
char_width = unicodedata.east_asian_width(char)
if char_width in "WF":
width += 2
elif char_width == "A":
_validate_eaaw(east_asian_ambiguous_width)
width += east_asian_ambiguous_width
else:
width += 1
return width
def strwidth_ucs_4(width_data, string):
return sum(((
(
0
) if combining(symbol) else (
width_data[east_asian_width(symbol)]
)
) for symbol in string))
def strwidth_ucs_2(width_data, string):
return sum(((
(
width_data[east_asian_width(string[i - 1] + symbol)]
) if 0xDC00 <= ord(symbol) <= 0xDFFF else (
0
) if combining(symbol) or 0xD800 <= ord(symbol) <= 0xDBFF else (
width_data[east_asian_width(symbol)]
)
) for i, symbol in enumerate(string)))
roberteldersoftwarediff.py 文件源码
项目:roberteldersoftwarediff
作者: RobertElderSoftware
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def make_character_presentable(c, rp):
if len(c) == 0:
return c, 0 # The result of an ignored failed decode from an invalid character.
# A character at this point should be a list of integers.
for b in c:
assert(type(b) == int)
if rp.pretty_output:
if len(c) == 1 and ((c[0] > 31 and c[0] < 127) or c[0] == ord('\t')):
# Standard ascii character
if c[0] == ord('\t'):
return [ord(u" "),ord(u" "),ord(u" "),ord(u" ")], 4
else:
return [c[0]], 1
else:
# Extended ASCII characer or multi-byte character.
rtn = []
for byte in c:
rtn += [py23_ord(b) for b in (b"\\x" + as_byte_string(format(byte, '02X'), rp.output_encoding, "internal"))]
return rtn, len(rtn)
else:
# This is not precise at all, but it is the best that can be done
char_as_unicode = e_decode(int_array_as_byte_string(c), rp.output_encoding, "internal")
if len(char_as_unicode) == 0:
return [], 0 # Happens sometimes due to decode failure on invalid characters.
east_asian_width = get_east_asian_width(char_as_unicode)
replacement_chars = get_replacement_char(char_as_unicode)
if replacement_chars is None:
return c, east_asian_width
else:
ls = [get_east_asian_width(c) for c in replacement_chars]
return [py23_ord(b) for b in as_byte_string(replacement_chars, rp.output_encoding, "internal")], sum(ls)
def lenNg(iterable):
"""Redefining len here so it will be able to work with non-ASCII characters
"""
if isinstance(iterable, bytes_type) or isinstance(iterable, unicode_type):
unicode_data = obj2unicode(iterable)
if hasattr(unicodedata, 'east_asian_width'):
w = unicodedata.east_asian_width
return sum([w(c) in 'WF' and 2 or 1 for c in unicode_data])
else:
return unicode_data.__len__()
else:
return iterable.__len__()
def column_width(text):
"""Return the column width of text.
Correct ``len(text)`` for wide East Asian and combining Unicode chars.
"""
if isinstance(text, str) and sys.version_info < (3,0):
return len(text)
try:
width = sum([east_asian_widths[unicodedata.east_asian_width(c)]
for c in text])
except AttributeError: # east_asian_width() New in version 2.4.
width = len(text)
# correction for combining chars:
width -= len(find_combining_chars(text))
return width
def column_width(text):
"""Return the column width of text.
Correct ``len(text)`` for wide East Asian and combining Unicode chars.
"""
if isinstance(text, str) and sys.version_info < (3,0):
return len(text)
try:
width = sum([east_asian_widths[unicodedata.east_asian_width(c)]
for c in text])
except AttributeError: # east_asian_width() New in version 2.4.
width = len(text)
# correction for combining chars:
width -= len(find_combining_chars(text))
return width
def get_text_length(text):
# `len(unichar)` measures the number of characters, so we use
# `unicodedata.east_asian_width` to measure the length of characters.
# Following responses are considered to be full-width length.
# * A(Ambiguous)
# * F(Fullwidth)
# * W(Wide)
text = six.text_type(text)
return sum(2 if unicodedata.east_asian_width(char) in 'WFA' else 1
for char in text)