Ejemplo n.º 1
0
def wrap_text(textlist, wraplength=50, indent=0, firstline=0):
    """
  It takes a block of text and wraps it nicely.  It accounts for
  indenting lines of text, wraplengths, and wrapping around ANSI 
  colors.

  We break on carriage returns (those are easy) and if no carriage
  returns are available we break on spaces.

  If the actual line is longer than the wraplength, then we'll break
  in the line at the wraplength--this will cut words in two.

  Note: we don't expand tabs or backspaces.  Both count as one
  character.

  @param textlist: either a string of text needing to be formatted and
      wrapped, or a textlist needing to be formatted and wrapped.
  @type  textlist: string or list of strings

  @param wraplength: the maximum length any line can be.  we'll wrap
      at an index equal to or less than this length.
  @type  wraplength: int

  @param indent: how many spaces to indent every line.
  @type  indent: int

  @param firstline: 0 if we shouldn't indent the first line, 1 if we 
      should
  @type  firstline: boolean

  @returns: the wrapped text string
  @rtype: string
  """
    wrapcount = 0  # how much we've got on the line so far
    linecount = 0  # which line we're on

    if wraplength > 2:
        wraplength = wraplength - 2

    # split the formatting from the text
    if type(textlist) == types.StringType:
        textlist = ansi.split_ansi_from_text(textlist)

    for i in range(0, len(textlist)):
        # if this is a color token, we gloss over it
        if ansi.is_color_token(textlist[i]):
            continue

        # if this is a text token, then we need to factor it into the word
        # wrapping
        marker = 0

        # this handles the offset for not indenting the first line (if that's
        # the sort of thing we're into).
        if firstline:
            x = _find_next_break(textlist[i], marker, wrapcount,
                                 wraplength - indent)
        else:
            x = _find_next_break(textlist[i], marker, wrapcount, wraplength)

        # go through finding breaks and sticking in carriage returns and indents
        # and things for this text token
        while x != -1:
            # insert the carriage return, any indent, and lstrip the line as well
            # print "'" + textlist[i] + "'", len(textlist[i]), x
            if textlist[i][x] == "\n":
                if indent:
                    textlist[i] = (textlist[i][:x + 1] + (indent * ' ') +
                                   textlist[i][x + 1:].lstrip())
                else:
                    textlist[i] = (textlist[i][:x + 1] + textlist[i][x + 1:])
            else:
                if indent:
                    textlist[i] = (textlist[i][:x + 1] + '\n' +
                                   (indent * ' ') +
                                   textlist[i][x + 1:].lstrip())
                else:
                    textlist[i] = (textlist[i][:x + 1] + '\n' +
                                   textlist[i][x + 1:])

            marker = x + indent + 2
            wrapcount = 0

            x = _find_next_break(textlist[i], marker, wrapcount,
                                 wraplength - indent)

        wrapcount = len(textlist[i]) - marker + wrapcount

    # this next line joins the list with no separator
    if firstline:
        return (indent * " ") + ''.join(textlist)
    else:
        return ''.join(textlist)
Ejemplo n.º 2
0
def wrap_text(textlist, wraplength=50, indent=0, firstline=0):
  """
  It takes a block of text and wraps it nicely.  It accounts for
  indenting lines of text, wraplengths, and wrapping around ANSI 
  colors.

  We break on carriage returns (those are easy) and if no carriage
  returns are available we break on spaces.

  If the actual line is longer than the wraplength, then we'll break
  in the line at the wraplength--this will cut words in two.

  Note: we don't expand tabs or backspaces.  Both count as one
  character.

  @param textlist: either a string of text needing to be formatted and
      wrapped, or a textlist needing to be formatted and wrapped.
  @type  textlist: string or list of strings

  @param wraplength: the maximum length any line can be.  we'll wrap
      at an index equal to or less than this length.
  @type  wraplength: int

  @param indent: how many spaces to indent every line.
  @type  indent: int

  @param firstline: 0 if we shouldn't indent the first line, 1 if we 
      should
  @type  firstline: boolean

  @returns: the wrapped text string
  @rtype: string
  """
  wrapcount = 0           # how much we've got on the line so far
  linecount = 0           # which line we're on

  if wraplength > 2:
    wraplength = wraplength - 2

  # split the formatting from the text
  if type(textlist) == types.StringType:
    textlist = ansi.split_ansi_from_text(textlist)

  for i in range(0, len(textlist)):
    # if this is a color token, we gloss over it
    if ansi.is_color_token(textlist[i]):
      continue

    # if this is a text token, then we need to factor it into the word
    # wrapping
    marker = 0

    # this handles the offset for not indenting the first line (if that's
    # the sort of thing we're into).
    if firstline:
      x = _find_next_break(textlist[i], marker, wrapcount, wraplength - indent)
    else:
      x = _find_next_break(textlist[i], marker, wrapcount, wraplength)

    # go through finding breaks and sticking in carriage returns and indents
    # and things for this text token
    while x != -1:
      # insert the carriage return, any indent, and lstrip the line as well
      # print "'" + textlist[i] + "'", len(textlist[i]), x
      if textlist[i][x] == "\n":
        if indent:
          textlist[i] = (textlist[i][:x+1] + (indent * ' ') + textlist[i][x+1:].lstrip())
        else:
          textlist[i] = (textlist[i][:x+1] + textlist[i][x+1:])
      else:
        if indent:
          textlist[i] = (textlist[i][:x+1] + '\n' + (indent * ' ') + textlist[i][x+1:].lstrip())
        else:
          textlist[i] = (textlist[i][:x+1] + '\n' + textlist[i][x+1:])

      marker = x + indent + 2
      wrapcount = 0

      x = _find_next_break(textlist[i], marker, wrapcount, wraplength - indent)

    wrapcount = len(textlist[i]) - marker + wrapcount


  # this next line joins the list with no separator
  if firstline:
    return (indent * " ") + ''.join(textlist)
  else:
    return ''.join(textlist)
Ejemplo n.º 3
0
            ['#alias t3k #ses a localhost 3000'])
  _pass_fail("split commands 4", 
            split_commands('#alias gv {put all in vortex;get all}'),
            ['#alias gv {put all in vortex;get all}'])
  _pass_fail("split_commands 5", 
            split_commands('#alias sv {put all in vortex;get all};test'),
            ['#alias sv {put all in vortex;get all}', 'test'])
  _pass_fail("split commands 6",
            split_commands(r'#showme \{ blah;#showme another }'), 
            [r'#showme \{ blah', r'#showme another }'])

  print 

  from ansi import split_ansi_from_text
  _pass_fail("split_ansi_from_text 1",
            split_ansi_from_text("This is some text."),
            ["This is some text."])
  _pass_fail("split_ansi_from_text 2",
            split_ansi_from_text("\33[1;37mThis is\33[0m text."),
            ["\33[1;37m", "This is", "\33[0m", " text."])
  _pass_fail("split_ansi_from_text 3",
            split_ansi_from_text("Hi \33[1;37mThis is\33[0m text."),
            ["Hi ", "\33[1;37m", "This is", "\33[0m", " text."])
  _pass_fail("split_ansi_from_text 4",
            split_ansi_from_text("\33[1;37mThis is\33[0"),
            ["\33[1;37m", "This is", "\33[0"])

  print

  text = "This is a really long line to see if we're wrapping correctly.  Because it's way cool when we write code that works.  Yay!"
Ejemplo n.º 4
0
               ['#alias t3k #ses a localhost 3000'])
    _pass_fail("split commands 4",
               split_commands('#alias gv {put all in vortex;get all}'),
               ['#alias gv {put all in vortex;get all}'])
    _pass_fail("split_commands 5",
               split_commands('#alias sv {put all in vortex;get all};test'),
               ['#alias sv {put all in vortex;get all}', 'test'])
    _pass_fail("split commands 6",
               split_commands(r'#showme \{ blah;#showme another }'),
               [r'#showme \{ blah', r'#showme another }'])

    print

    from ansi import split_ansi_from_text
    _pass_fail("split_ansi_from_text 1",
               split_ansi_from_text("This is some text."),
               ["This is some text."])
    _pass_fail("split_ansi_from_text 2",
               split_ansi_from_text("\33[1;37mThis is\33[0m text."),
               ["\33[1;37m", "This is", "\33[0m", " text."])
    _pass_fail("split_ansi_from_text 3",
               split_ansi_from_text("Hi \33[1;37mThis is\33[0m text."),
               ["Hi ", "\33[1;37m", "This is", "\33[0m", " text."])
    _pass_fail("split_ansi_from_text 4",
               split_ansi_from_text("\33[1;37mThis is\33[0"),
               ["\33[1;37m", "This is", "\33[0"])

    print

    text = "This is a really long line to see if we're wrapping correctly.  Because it's way cool when we write code that works.  Yay!"