parameters: (inp, cols)
return_statement: return out
docstring: Wrap RichTextLines according to maximum number of columns.
  
    Produces a new RichTextLines object with the text lines, font_attr_segs and
    annotations properly wrapped. This ought to be used sparingly, as in most
    cases, command handlers producing RichTextLines outputs should know the
    screen/panel width via the screen_info kwarg and should produce properly
    length-limited lines in the output accordingly.
  
    Args:
      inp: Input RichTextLines object.
      cols: Number of columns, as an int.
  
    Returns:
      A new instance of RichTextLines, with line lengths limited to cols.
  
    Raises:
      ValueError: If inputs have invalid types.
docstring_summary: Wrap RichTextLines according to maximum number of columns.
docstring_tokens: Wrap RichTextLines according to maximum number of columns .
function: def wrap_rich_text_lines(inp, cols):
    """Wrap RichTextLines according to maximum number of columns.
  
    Produces a new RichTextLines object with the text lines, font_attr_segs and
    annotations properly wrapped. This ought to be used sparingly, as in most
    cases, command handlers producing RichTextLines outputs should know the
    screen/panel width via the screen_info kwarg and should produce properly
    length-limited lines in the output accordingly.
  
    Args:
      inp: Input RichTextLines object.
      cols: Number of columns, as an int.
  
    Returns:
      A new instance of RichTextLines, with line lengths limited to cols.
  
    Raises:
      ValueError: If inputs have invalid types.
    """
  
    if not isinstance(inp, RichTextLines):
      raise ValueError("Invalid type of input screen_output")
  
    if not isinstance(cols, int):
      raise ValueError("Invalid type of input cols")
  
    out = RichTextLines([])
  
    row_counter = 0  # Counter for new row index
    for i in xrange(len(inp.lines)):
      line = inp.lines[i]
  
      if i in inp.annotations:
        out.annotations[row_counter] = inp.annotations[i]
  
      if len(line) <= cols:
        # No wrapping.
        out.lines.append(line)
        if i in inp.font_attr_segs:
          out.font_attr_segs[row_counter] = inp.font_attr_segs[i]
  
        row_counter += 1
      else:
        # Wrap.
        wlines = []  # Wrapped lines.
  
        osegs = []
        if i in inp.font_attr_segs:
          osegs = inp.font_attr_segs[i]
  
        idx = 0
        while idx < len(line):
          if idx + cols > len(line):
            rlim = len(line)
          else:
            rlim = idx + cols
  
          wlines.append(line[idx:rlim])
          for seg in osegs:
            if (seg[0] < rlim) and (seg[1] >= idx):
              # Calculate left bound within wrapped line.
              if seg[0] >= idx:
                lb = seg[0] - idx
              else:
                lb = 0
  
              # Calculate right bound within wrapped line.
              if seg[1] < rlim:
                rb = seg[1] - idx
              else:
                rb = rlim - idx
  
              if rb > lb:  # Omit zero-length segments.
                wseg = (lb, rb, seg[2])
                if row_counter not in out.font_attr_segs:
                  out.font_attr_segs[row_counter] = [wseg]
                else:
                  out.font_attr_segs[row_counter].append(wseg)
  
          idx += cols
          row_counter += 1
  
        out.lines.extend(wlines)
  
    return out
function_tokens: def wrap_rich_text_lines ( inp , cols ) : if not isinstance ( inp , RichTextLines ) : raise ValueError ( "Invalid type of input screen_output" ) if not isinstance ( cols , int ) : raise ValueError ( "Invalid type of input cols" ) out = RichTextLines ( [ ] ) row_counter = 0 # Counter for new row index for i in xrange ( len ( inp . lines ) ) : line = inp . lines [ i ] if i in inp . annotations : out . annotations [ row_counter ] = inp . annotations [ i ] if len ( line ) <= cols : # No wrapping. out . lines . append ( line ) if i in inp . font_attr_segs : out . font_attr_segs [ row_counter ] = inp . font_attr_segs [ i ] row_counter += 1 else : # Wrap. wlines = [ ] # Wrapped lines. osegs = [ ] if i in inp . font_attr_segs : osegs = inp . font_attr_segs [ i ] idx = 0 while idx < len ( line ) : if idx + cols > len ( line ) : rlim = len ( line ) else : rlim = idx + cols wlines . append ( line [ idx : rlim ] ) for seg in osegs : if ( seg [ 0 ] < rlim ) and ( seg [ 1 ] >= idx ) : # Calculate left bound within wrapped line. if seg [ 0 ] >= idx : lb = seg [ 0 ] - idx else : lb = 0 # Calculate right bound within wrapped line. if seg [ 1 ] < rlim : rb = seg [ 1 ] - idx else : rb = rlim - idx if rb > lb : # Omit zero-length segments. wseg = ( lb , rb , seg [ 2 ] ) if row_counter not in out . font_attr_segs : out . font_attr_segs [ row_counter ] = [ wseg ] else : out . font_attr_segs [ row_counter ] . append ( wseg ) idx += cols row_counter += 1 out . lines . extend ( wlines ) return out