Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
if _use_dec_special and type(s) == text_type:
# first convert drawing characters
try:
s = s.translate( escape.DEC_SPECIAL_CHARMAP )
except NotImplementedError:
# python < 2.4 needs to do this the hard way..
for c, alt in zip(escape.DEC_SPECIAL_CHARS,
escape.ALT_DEC_SPECIAL_CHARS):
s = s.replace( c, escape.SO+alt+escape.SI )
if type(s) == text_type:
s = s.replace(escape.SI+escape.SO, u"") # remove redundant shifts
s = codecs.encode(s, _target_encoding, 'replace')
assert isinstance(s, bytes)
SO = escape.SO.encode('ascii')
SI = escape.SI.encode('ascii')
sis = s.split(SO)
assert isinstance(sis[0], bytes)
sis0 = sis[0].replace(SI, bytes())
sout = []
cout = []
if sis0:
sout.append( sis0 )
cout.append( (None,len(sis0)) )
if len(sis)==1:
return sis0, cout
def decode_one( text, pos ):
"""
Return (ordinal at pos, next position) for UTF-8 encoded text.
"""
assert isinstance(text, bytes), text
b1 = ord2(text[pos])
if not b1 & 0x80:
return b1, pos+1
error = ord("?"), pos+1
lt = len(text)
lt = lt-pos
if lt < 2:
return error
if b1 & 0xe0 == 0xc0:
b2 = ord2(text[pos+1])
if b2 & 0xc0 != 0x80:
return error
o = ((b1&0x1f)<<6)|(b2&0x3f)
if o < 0x80:
return error
return o, pos+2
def reset(self):
"""
Reset the terminal.
"""
self.escbuf = bytes()
self.within_escape = False
self.parsestate = 0
self.attrspec = None
self.charset = TermCharset()
self.saved_cursor = None
self.saved_attrs = None
self.is_rotten_cursor = False
self.reset_scroll()
self.init_tabstops()
# terminal modes
def __init__(self, width, height, widget):
Canvas.__init__(self)
self.width, self.height = width, height
self.widget = widget
self.modes = widget.term_modes
self.scrollback_buffer = TermScroller()
self.scrolling_up = 0
self.utf8_eat_bytes = None
self.utf8_buffer = bytes()
self.coords["cursor"] = (0, 0, None)
self.reset()
def leave_escape(self):
self.within_escape = False
self.parsestate = 0
self.escbuf = bytes()
def calculate_text_segments(self, text, width, wrap):
"""
Calculate the segments of text to display given width screen
columns to display them.
text - unicode text or byte string to display
width - number of available screen columns
wrap - wrapping mode used
Returns a layout structure without alignment applied.
"""
nl, nl_o, sp_o = "\n", "\n", " "
if PYTHON3 and isinstance(text, bytes):
nl = B(nl) # can only find bytes in python3 bytestrings
nl_o = ord(nl_o) # + an item of a bytestring is the ordinal value
sp_o = ord(sp_o)
b = []
p = 0
if wrap == 'clip':
# no wrapping to calculate, so it's easy.
while p<=len(text):
n_cr = text.find(nl, p)
if n_cr == -1:
n_cr = len(text)
sc = calc_width(text, p, n_cr)
l = [(0,n_cr)]
if p!=n_cr:
l = [(sc, p, n_cr)] + l
b.append(l)
#if seg is None: assert 0, ls
s = LayoutSegment(seg)
if s.end:
tseg, cs = apply_target_encoding(
text[s.offs:s.end])
line.append(tseg)
attrrange(s.offs, s.end, rle_len(cs))
rle_join_modify( linec, cs )
elif s.text:
tseg, cs = apply_target_encoding( s.text )
line.append(tseg)
attrrange( s.offs, s.offs, len(tseg) )
rle_join_modify( linec, cs )
elif s.offs:
if s.sc:
line.append(bytes().rjust(s.sc))
attrrange( s.offs, s.offs, s.sc )
else:
line.append(bytes().rjust(s.sc))
linea.append((None, s.sc))
linec.append((None, s.sc))
t.append(bytes().join(line))
a.append(linea)
c.append(linec)
return TextCanvas(t, a, c, maxcol=maxcol)
def python3_repr(v):
"""
Return strings and byte strings as they appear in Python 3
>>> python3_repr(u"text")
"'text'"
>>> python3_repr(bytes())
"b''"
"""
r = repr(v)
if not PYTHON3:
if isinstance(v, bytes):
return 'b' + r
if r.startswith('u'):
return r[1:]
return r