Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def render(self, size, focus=False):
"""
Render edit widget and return canvas. Include cursor when in
focus.
>>> c = Edit("? ","yes").render((10,), focus=True)
>>> c.text # ... = b in Python 3
[...'? yes ']
>>> c.cursor
(5, 0)
"""
(maxcol,) = size
self._shift_view_to_cursor = bool(focus)
canv = Text.render(self,(maxcol,))
if focus:
canv = CompositeCanvas(canv)
canv.cursor = self.get_cursor_coords((maxcol,))
# .. will need to FIXME if I want highlight to work again
#if self.highlight:
# hstart, hstop = self.highlight_coords()
# d.coords['highlight'] = [ hstart, hstop ]
return canv
while i < len(text):
j = text.find('\n', i)
if j == -1:
j = len(text)
c = calc_width(text, i, j)
if c>cols:
cols = c
i = j+1
return (cols, text.count('\n') + 1)
class EditError(TextError):
pass
class Edit(Text):
"""
Text editing widget implements cursor movement, text insertion and
deletion. A caption may prefix the editing area. Uses text class
for text layout.
Users of this class may listen for ``"change"`` or ``"postchange"``
events. See :func:``connect_signal``.
* ``"change"`` is sent just before the value of edit_text changes.
It receives the new text as an argument. Note that ``"change"`` cannot
change the text in question as edit_text changes the text afterwards.
* ``"postchange"`` is sent after the value of edit_text changes.
It receives the old value of the text as an argument and thus is
appropriate for changing the text. It is possible for a ``"postchange"``
event handler to get into a loop of changing the text and then being
called when the event is re-emitted. It is up to the event
if tline:
tline = Divider(tline)
if bline:
bline = Divider(bline)
if lline:
lline = SolidFill(lline)
if rline:
rline = SolidFill(rline)
tlcorner, trcorner = Text(tlcorner), Text(trcorner)
blcorner, brcorner = Text(blcorner), Text(brcorner)
if not tline and title:
raise ValueError('Cannot have a title when tline is empty string')
self.title_widget = Text(self.format_title(title))
if tline:
if title_align not in ('left', 'center', 'right'):
raise ValueError('title_align must be one of "left", "right", or "center"')
if title_align == 'left':
tline_widgets = [('flow', self.title_widget), tline]
else:
tline_widgets = [tline, ('flow', self.title_widget)]
if title_align == 'center':
tline_widgets.append(tline)
self.tline_widget = Columns(tline_widgets)
top = Columns([
('fixed', 1, tlcorner),
self.tline_widget,
('fixed', 1, trcorner)
])
If empty string is specified for one of the lines/corners, then no
character will be output there. This allows for seamless use of
adjoining LineBoxes.
"""
if tline:
tline = Divider(tline)
if bline:
bline = Divider(bline)
if lline:
lline = SolidFill(lline)
if rline:
rline = SolidFill(rline)
tlcorner, trcorner = Text(tlcorner), Text(trcorner)
blcorner, brcorner = Text(blcorner), Text(brcorner)
if not tline and title:
raise ValueError('Cannot have a title when tline is empty string')
self.title_widget = Text(self.format_title(title))
if tline:
if title_align not in ('left', 'center', 'right'):
raise ValueError('title_align must be one of "left", "right", or "center"')
if title_align == 'left':
tline_widgets = [('flow', self.title_widget), tline]
else:
tline_widgets = [tline, ('flow', self.title_widget)]
if title_align == 'center':
tline_widgets.append(tline)
self.tline_widget = Columns(tline_widgets)
def get_line_translation(self, maxcol, ta=None ):
trans = Text.get_line_translation(self, maxcol, ta)
if not self._shift_view_to_cursor:
return trans
text, ignore = self.get_text()
x,y = calc_coords( text, trans,
self.edit_pos + len(self.caption) )
if x < 0:
return ( trans[:y]
+ [shift_line(trans[y],-x)]
+ trans[y+1:] )
elif x >= maxcol:
return ( trans[:y]
+ [shift_line(trans[y],-(x-maxcol+1))]
+ trans[y+1:] )
return trans
def render(self, size, focus=False):
"""
Render the progress bar.
"""
(maxcol,) = size
txt = Text(self.get_text(), self.text_align, CLIP)
c = txt.render((maxcol,))
cf = float(self.current) * maxcol / self.done
ccol_dirty = int(cf)
ccol = len(c._text[0][:ccol_dirty].decode(
'utf-8', 'ignore'
).encode(
'utf-8'
))
cs = 0
if self.satt is not None:
cs = int((cf - ccol) * 8)
if ccol < 0 or (ccol == 0 and cs == 0):
c._attr = [[(self.normal, maxcol)]]
elif ccol >= maxcol:
c._attr = [[(self.complete, maxcol)]]
brcorner: bottom right corner
If empty string is specified for one of the lines/corners, then no
character will be output there. This allows for seamless use of
adjoining LineBoxes.
"""
if tline:
tline = Divider(tline)
if bline:
bline = Divider(bline)
if lline:
lline = SolidFill(lline)
if rline:
rline = SolidFill(rline)
tlcorner, trcorner = Text(tlcorner), Text(trcorner)
blcorner, brcorner = Text(blcorner), Text(brcorner)
if not tline and title:
raise ValueError('Cannot have a title when tline is empty string')
self.title_widget = Text(self.format_title(title))
if tline:
if title_align not in ('left', 'center', 'right'):
raise ValueError('title_align must be one of "left", "right", or "center"')
if title_align == 'left':
tline_widgets = [('flow', self.title_widget), tline]
else:
tline_widgets = [tline, ('flow', self.title_widget)]
if title_align == 'center':
tline_widgets.append(tline)
def set_scale(self, labels, top):
"""
set_scale( [(label1 position, label1 markup),...], top )
label position -- 0 < position < top for the y position
label markup -- text markup for this label
top -- top y position
"""
labels = labels[:] # shallow copy
labels.sort()
labels.reverse()
self.pos = []
self.txt = []
for y, markup in labels:
self.pos.append(y)
self.txt.append(Text(markup))
self.top = top
if type(bar_type) == tuple:
if len(bar_type) == 3:
# vertical eighths
fg, bg, k = bar_type
a = self.satt[(fg, bg)]
t = self.eighths[k] * width
else:
# horizontal lines
bg, k = bar_type
a = self.hatt[bg]
t = self.hlines[k] * width
else:
a = self.attr[bar_type]
t = self.char[bar_type] * width
l.append((a, t))
c = Text(l).render((maxcol,))
assert c.rows() == 1, "Invalid characters in BarGraph!"
combinelist += [(c, None, False)] * y_count
canv = CanvasCombine(combinelist)
return canv