def make_canvas(txt, attr, maxcol, fill_attr=None): processed_txt = [] processed_attr = [] processed_cs = [] for line, line_attr in zip(txt, attr): # filter out zero-length attrs line_attr = [(aname, l) for aname, l in line_attr if l > 0] diff = maxcol - len(line) if diff > 0: line += " "*diff line_attr.append((fill_attr, diff)) else: from urwid.util import rle_subseg line = line[:maxcol] line_attr = rle_subseg(line_attr, 0, maxcol) from urwid.util import apply_target_encoding line, line_cs = apply_target_encoding(line) processed_txt.append(line) processed_attr.append(line_attr) processed_cs.append(line_cs) return urwid.TextCanvas( processed_txt, processed_attr, processed_cs, maxcol=maxcol)
def attrrange( start_offs, end_offs, destw ): """ Add attributes based on attributes between start_offs and end_offs. """ if start_offs == end_offs: [(at,run)] = arange(start_offs,end_offs) rle_append_modify( linea, ( at, destw )) return if destw == end_offs-start_offs: for at, run in arange(start_offs,end_offs): rle_append_modify( linea, ( at, run )) return # encoded version has different width o = start_offs for at, run in arange(start_offs, end_offs): if o+run == end_offs: rle_append_modify( linea, ( at, destw )) return tseg = text[o:o+run] tseg, cs = apply_target_encoding( tseg ) segw = rle_len(cs) rle_append_modify( linea, ( at, segw )) o += run destw -= segw
def render(self, size, focus=False): from pudb.debugger import CONFIG render_line_nr = CONFIG["line_numbers"] maxcol = size[0] hscroll = self.dbg_ui.source_hscroll_start attrs = [] if self.is_current: crnt = ">" attrs.append("current") else: crnt = " " if self.has_breakpoint: bp = "*" attrs.append("breakpoint") else: bp = " " if focus: attrs.append("focused") elif self.highlight: if not self.has_breakpoint: attrs.append("highlighted") if render_line_nr: line_nr_len = len(self.line_nr) else: line_nr_len = 0 text = self.text if not attrs and self.attr is not None: attr = self.attr else: attr = [(" ".join(attrs+["source"]), hscroll+maxcol-2-line_nr_len)] from urwid.util import rle_subseg, rle_len if hscroll: text = text[hscroll:] attr = rle_subseg(attr, hscroll, rle_len(attr)) if render_line_nr: attr = [("line number", len(self.line_nr))] + attr text = self.line_nr + text text = crnt+bp+text attr = [("source", 1), ("bp_star", 1)] + attr # clipping ------------------------------------------------------------ if len(text) > maxcol: text = text[:maxcol] attr = rle_subseg(attr, 0, maxcol) # shipout ------------------------------------------------------------- from urwid.util import apply_target_encoding txt, cs = apply_target_encoding(text) return urwid.TextCanvas([txt], [attr], [cs], maxcol=maxcol)
def __init__(self, fill_char, cols, rows): Canvas.__init__(self) end, col = calc_text_pos(fill_char, 0, len(fill_char), 1) assert col == 1, "Invalid fill_char: %r" % fill_char self._text, cs = apply_target_encoding(fill_char[:end]) self._cs = cs[0][0] self.size = cols, rows self.cursor = None
def render(self, c): if c in self.canvas: return self.canvas[c] width, l = self.char[c] tl = [] csl = [] for d in l: t, cs = apply_target_encoding(d) tl.append(t) csl.append(cs) canv = TextCanvas(tl, None, csl, maxcol=width, check_width=False) self.canvas[c] = canv return canv
def make_canvas(txt, attr, maxcol, fill_attr=None): processed_txt = [] processed_attr = [] processed_cs = [] for line, line_attr in zip(txt, attr): # filter out zero-length attrs line_attr = [(aname, l) for aname, l in line_attr if l > 0] diff = maxcol - calc_width(line, 0, len(line)) if diff > 0: line += " " * diff line_attr.append((fill_attr, diff)) else: from urwid.util import rle_subseg line = line[:maxcol] line_attr = rle_subseg(line_attr, 0, maxcol) from urwid.util import apply_target_encoding encoded_line, line_cs = apply_target_encoding(line) # line_cs contains byte counts as requested by TextCanvas, but # line_attr still contains column counts at this point: let's fix this. def get_byte_line_attr(line, line_attr): i = 0 for label, column_count in line_attr: byte_count = len(line[i:i + column_count].encode(_target_encoding)) i += column_count yield label, byte_count line_attr = list(get_byte_line_attr(line, line_attr)) processed_txt.append(encoded_line) processed_attr.append(line_attr) processed_cs.append(line_cs) return urwid.TextCanvas(processed_txt, processed_attr, processed_cs, maxcol=maxcol)
def make_canvas(txt, attr, maxcol, fill_attr=None): processed_txt = [] processed_attr = [] processed_cs = [] for line, line_attr in zip(txt, attr): # filter out zero-length attrs line_attr = [(aname, l) for aname, l in line_attr if l > 0] diff = maxcol - len(line) if diff > 0: line += " "*diff line_attr.append((fill_attr, diff)) else: from urwid.util import rle_subseg line = line[:maxcol] line_attr = rle_subseg(line_attr, 0, maxcol) from urwid.util import apply_target_encoding encoded_line, line_cs = apply_target_encoding(line) # line_cs contains byte counts as requested by TextCanvas, but # line_attr still contains column counts at this point: let's fix this. def get_byte_line_attr(line, line_attr): i = 0 for label, column_count in line_attr: byte_count = len(line[i:i+column_count].encode(_target_encoding)) i += column_count yield label, byte_count line_attr = list(get_byte_line_attr(line, line_attr)) processed_txt.append(encoded_line) processed_attr.append(line_attr) processed_cs.append(line_cs) return urwid.TextCanvas( processed_txt, processed_attr, processed_cs, maxcol=maxcol)
def render(self, size, focus=False): from pudb.debugger import CONFIG render_line_nr = CONFIG["line_numbers"] maxcol = size[0] hscroll = self.dbg_ui.source_hscroll_start # attrs is a list of words like 'focused' and 'breakpoint' attrs = [] if self.is_current: crnt = ">" attrs.append("current") else: crnt = " " if self.has_breakpoint: bp = "*" attrs.append("breakpoint") else: bp = " " if focus: attrs.append("focused") elif self.highlight: if not self.has_breakpoint: attrs.append("highlighted") text = self.text if not attrs and self.attr is not None: attr = self.attr + [("source", None)] else: attr = [(" ".join(attrs+["source"]), None)] from urwid.util import apply_target_encoding, trim_text_attr_cs # build line prefix --------------------------------------------------- line_prefix = "" line_prefix_attr = [] if render_line_nr and self.line_nr: line_prefix_attr = [("line number", len(self.line_nr))] line_prefix = self.line_nr line_prefix = crnt+bp+line_prefix line_prefix_attr = [("source", 1), ("breakpoint marker", 1)] \ + line_prefix_attr # assume rendered width is same as len line_prefix_len = len(line_prefix) encoded_line_prefix, line_prefix_cs = apply_target_encoding(line_prefix) assert len(encoded_line_prefix) == len(line_prefix) # otherwise we'd have to adjust line_prefix_attr... :/ # shipout, encoding --------------------------------------------------- cs = [] encoded_text_segs = [] encoded_attr = [] i = 0 for seg_attr, seg_len in attr: if seg_len is None: # means: gobble up remainder of text and rest of line # and fill with attribute rowlen = hscroll+maxcol remaining_text = text[i:] encoded_seg_text, seg_cs = apply_target_encoding( remaining_text + rowlen*" ") encoded_attr.append((seg_attr, len(remaining_text)+rowlen)) else: unencoded_seg_text = text[i:i+seg_len] encoded_seg_text, seg_cs = apply_target_encoding(unencoded_seg_text) adjustment = len(encoded_seg_text) - len(unencoded_seg_text) encoded_attr.append((seg_attr, seg_len + adjustment)) i += seg_len encoded_text_segs.append(encoded_seg_text) cs.extend(seg_cs) encoded_text = b"".join(encoded_text_segs) encoded_text, encoded_attr, cs = trim_text_attr_cs( encoded_text, encoded_attr, cs, hscroll, hscroll+maxcol-line_prefix_len) encoded_text = encoded_line_prefix + encoded_text encoded_attr = line_prefix_attr + encoded_attr cs = line_prefix_cs + cs return urwid.TextCanvas([encoded_text], [encoded_attr], [cs], maxcol=maxcol)
rle_len(attr)) if render_line_nr: text = self.line_nr + text text = crnt + bp + text attr = [("source", 1), ("bp_star", 1)] + attr # clipping ------------------------------------------------------------ if len(text) > maxcol: text = text[:maxcol] attr = rle_subseg(attr, 0, maxcol) # shipout ------------------------------------------------------------- from urwid.util import apply_target_encoding txt, cs = apply_target_encoding(text) return urwid.TextCanvas([txt], [attr], [cs], maxcol=maxcol) def keypress(self, size, key): return key def format_source(debugger_ui, lines, breakpoints): lineno_format = "%%%dd " % (len(str(len(lines)))) try: import pygments except ImportError: return [ SourceLine(debugger_ui, line.rstrip("\n\r").expandtabs(TABSTOP),
def apply_text_layout(text, attr, ls, maxcol): t = [] a = [] c = [] class AttrWalk: pass aw = AttrWalk aw.k = 0 # counter for moving through elements of a aw.off = 0 # current offset into text of attr[ak] def arange( start_offs, end_offs ): """Return an attribute list for the range of text specified.""" if start_offs < aw.off: aw.k = 0 aw.off = 0 o = [] while aw.off < end_offs: if len(attr)<=aw.k: # run out of attributes o.append((None,end_offs-max(start_offs,aw.off))) break at,run = attr[aw.k] if aw.off+run <= start_offs: # move forward through attr to find start_offs aw.k += 1 aw.off += run continue if end_offs <= aw.off+run: o.append((at, end_offs-max(start_offs,aw.off))) break o.append((at, aw.off+run-max(start_offs, aw.off))) aw.k += 1 aw.off += run return o for line_layout in ls: # trim the line to fit within maxcol line_layout = trim_line( line_layout, text, 0, maxcol ) line = [] linea = [] linec = [] def attrrange( start_offs, end_offs, destw ): """ Add attributes based on attributes between start_offs and end_offs. """ if start_offs == end_offs: [(at,run)] = arange(start_offs,end_offs) rle_append_modify( linea, ( at, destw )) return if destw == end_offs-start_offs: for at, run in arange(start_offs,end_offs): rle_append_modify( linea, ( at, run )) return # encoded version has different width o = start_offs for at, run in arange(start_offs, end_offs): if o+run == end_offs: rle_append_modify( linea, ( at, destw )) return tseg = text[o:o+run] tseg, cs = apply_target_encoding( tseg ) segw = rle_len(cs) rle_append_modify( linea, ( at, segw )) o += run destw -= segw for seg in line_layout: #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 render(self, size, focus=False): from pudb.debugger import CONFIG render_line_nr = CONFIG["line_numbers"] maxcol = size[0] hscroll = self.dbg_ui.source_hscroll_start # attrs is a list of words like "focused" and "breakpoint" attrs = [] if self.is_current: crnt = ">" attrs.append("current") else: crnt = " " if self.has_breakpoint: bp = "*" attrs.append("breakpoint") else: bp = " " if focus: attrs.append("focused") elif self.highlight: if not self.has_breakpoint: attrs.append("highlighted") text = self.text if not attrs and self.attr is not None: attr = self.attr + [("source", None)] else: attr = [(" ".join(attrs+["source"]), None)] from urwid.util import apply_target_encoding, trim_text_attr_cs # build line prefix --------------------------------------------------- line_prefix = "" line_prefix_attr = [] if render_line_nr and self.line_nr: line_prefix_attr = [("line number", len(self.line_nr))] line_prefix = self.line_nr line_prefix = crnt+bp+line_prefix line_prefix_attr = [("source", 1), ("breakpoint marker", 1)] \ + line_prefix_attr # assume rendered width is same as len line_prefix_len = len(line_prefix) encoded_line_prefix, line_prefix_cs = apply_target_encoding(line_prefix) assert len(encoded_line_prefix) == len(line_prefix) # otherwise we'd have to adjust line_prefix_attr... :/ # shipout, encoding --------------------------------------------------- cs = [] encoded_text_segs = [] encoded_attr = [] i = 0 for seg_attr, seg_len in attr: if seg_len is None: # means: gobble up remainder of text and rest of line # and fill with attribute rowlen = hscroll+maxcol remaining_text = text[i:] encoded_seg_text, seg_cs = apply_target_encoding( remaining_text + rowlen*" ") encoded_attr.append((seg_attr, len(remaining_text)+rowlen)) else: unencoded_seg_text = text[i:i+seg_len] encoded_seg_text, seg_cs = apply_target_encoding(unencoded_seg_text) adjustment = len(encoded_seg_text) - len(unencoded_seg_text) encoded_attr.append((seg_attr, seg_len + adjustment)) i += seg_len encoded_text_segs.append(encoded_seg_text) cs.extend(seg_cs) encoded_text = b"".join(encoded_text_segs) encoded_text, encoded_attr, cs = trim_text_attr_cs( encoded_text, encoded_attr, cs, hscroll, hscroll+maxcol-line_prefix_len) encoded_text = encoded_line_prefix + encoded_text encoded_attr = line_prefix_attr + encoded_attr cs = line_prefix_cs + cs return urwid.TextCanvas([encoded_text], [encoded_attr], [cs], maxcol=maxcol)
rle_len(attr)) if render_line_nr: text = self.line_nr + text text = crnt+bp+text attr = [("source", 1), ("bp_star", 1)] + attr # clipping ------------------------------------------------------------ if len(text) > maxcol: text = text[:maxcol] attr = rle_subseg(attr, 0, maxcol) # shipout ------------------------------------------------------------- from urwid.util import apply_target_encoding txt, cs = apply_target_encoding(text) return urwid.TextCanvas([txt], [attr], [cs], maxcol=maxcol) def keypress(self, size, key): return key def format_source(debugger_ui, lines, breakpoints): lineno_format = "%%%dd "%(len(str(len(lines)))) try: import pygments except ImportError: