Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _last_row(self, row):
"""On the last row we need to slide the bottom right character
into place. Calculate the new line, attr and an insert sequence
to do that.
eg. last row:
XXXXXXXXXXXXXXXXXXXXYZ
Y will be drawn after Z, shifting Z into position.
"""
new_row = row[:-1]
z_attr, z_cs, last_text = row[-1]
last_cols = util.calc_width(last_text, 0, len(last_text))
last_offs, z_col = util.calc_text_pos(last_text, 0,
len(last_text), last_cols-1)
if last_offs == 0:
z_text = last_text
del new_row[-1]
# we need another segment
y_attr, y_cs, nlast_text = row[-2]
nlast_cols = util.calc_width(nlast_text, 0,
len(nlast_text))
z_col += nlast_cols
nlast_offs, y_col = util.calc_text_pos(nlast_text, 0,
len(nlast_text), nlast_cols-1)
y_text = nlast_text[nlast_offs:]
if nlast_offs:
new_row.append((y_attr, y_cs,
nlast_text[:nlast_offs]))
else:
closest_pos = s
s = closest_pos
if s is None:
return
if s.end is None:
return s.offs
return calc_text_pos( text, s.offs, s.end, s.sc-1)[0]
for seg in line_layout:
s = LayoutSegment(seg)
if s.offs is not None:
if s.end is not None:
if (current_sc <= pref_col and
pref_col < current_sc + s.sc):
# exact match within this segment
return calc_text_pos( text,
s.offs, s.end,
pref_col - current_sc )[0]
elif current_sc <= pref_col:
closest_sc = current_sc + s.sc - 1
closest_pos = s
if closest_sc is None or ( abs(pref_col-current_sc)
< abs(pref_col-closest_sc) ):
# this screen column is closer
closest_sc = current_sc
closest_pos = s.offs
if current_sc > closest_sc:
# we're moving past
break
current_sc += s.sc
def code_span( s, fg, bg, cursor = -1):
code_fg = _code_colours[ fg ]
code_bg = _code_colours[ bg ]
if cursor >= 0:
c_off, _ign = util.calc_text_pos(s, 0, len(s), cursor)
c2_off = util.move_next_char(s, c_off, len(s))
return ( code_fg + code_bg + s[:c_off] + "\n" +
code_bg + code_fg + s[c_off:c2_off] + "\n" +
code_fg + code_bg + s[c2_off:] + "\n")
else:
return code_fg + code_bg + s + "\n"
if bg_r is None:
bg_r, bg_g, bg_b = _d_bg_r, _d_bg_g, _d_bg_b
html_fg = "#%02x%02x%02x" % (fg_r, fg_g, fg_b)
html_bg = "#%02x%02x%02x" % (bg_r, bg_g, bg_b)
if aspec.standout:
html_fg, html_bg = html_bg, html_fg
extra = (";text-decoration:underline" * aspec.underline +
";font-weight:bold" * aspec.bold)
def html_span(fg, bg, s):
if not s: return ""
return ('<span style="color:%s;'
'background:%s%s">%s</span>' %
(fg, bg, extra, html_escape(s)))
if cursor >= 0:
c_off, _ign = util.calc_text_pos(s, 0, len(s), cursor)
c2_off = util.move_next_char(s, c_off, len(s))
return (html_span(html_fg, html_bg, s[:c_off]) +
html_span(html_bg, html_fg, s[c_off:c2_off]) +
html_span(html_fg, html_bg, s[c2_off:]))
else:
return html_span(html_fg, html_bg, s)
y_attr, y_cs, nlast_text = row[-2]
nlast_cols = util.calc_width(nlast_text, 0,
len(nlast_text))
z_col += nlast_cols
nlast_offs, y_col = util.calc_text_pos(nlast_text, 0,
len(nlast_text), nlast_cols-1)
y_text = nlast_text[nlast_offs:]
if nlast_offs:
new_row.append((y_attr, y_cs,
nlast_text[:nlast_offs]))
else:
z_text = last_text[last_offs:]
y_attr, y_cs = z_attr, z_cs
nlast_cols = util.calc_width(last_text, 0,
last_offs)
nlast_offs, y_col = util.calc_text_pos(last_text, 0,
last_offs, nlast_cols-1)
y_text = last_text[nlast_offs:last_offs]
if nlast_offs:
new_row.append((y_attr, y_cs,
last_text[:nlast_offs]))
new_row.append((z_attr, z_cs, z_text))
return new_row, z_col-y_col, (y_attr, y_cs, y_text)
n_cr = len(text)
sc = calc_width(text, p, n_cr)
if sc == 0:
# removed character hint
b.append([(0,n_cr)])
p = n_cr+1
continue
if sc <= width:
# this segment fits
b.append([(sc,p,n_cr),
# removed character hint
(0,n_cr)])
p = n_cr+1
continue
pos, sc = calc_text_pos( text, p, n_cr, width )
if pos == p: # pathological width=1 double-byte case
raise CanNotDisplayText(
"Wide character will not fit in 1-column width")
if wrap == 'any':
b.append([(sc,p,pos)])
p = pos
continue
assert wrap == 'space'
if text[pos] == sp_o:
# perfect space wrap
b.append([(sc,p,pos),
# removed character hint
(0,pos)])
p = pos+1
continue
if is_wide_char(text, pos):
if closest_sc is None or ( abs(pref_col-current_sc)
< abs(pref_col-closest_sc) ):
# this screen column is closer
closest_sc = current_sc
closest_pos = s.offs
if current_sc > closest_sc:
# we're moving past
break
current_sc += s.sc
if closest_pos is None or type(closest_pos) == int:
return closest_pos
# return the last positions in the segment "closest_pos"
s = closest_pos
return calc_text_pos( text, s.offs, s.end, s.sc-1)[0]