def diffstat(self):
    """ calculate diffstat and return as a string
        Notes:
          - original diffstat ouputs target filename
          - single + or - shouldn't escape histogram
    """
    names = []
    insert = []
    delete = []
    delta = 0    # size change in bytes
    namelen = 0
    maxdiff = 0  # max number of changes for single file
                 # (for histogram width calculation)
    for patch in self.items:
      i,d = 0,0
      for hunk in patch.hunks:
        for line in hunk.text:
          if line.startswith(b'+'):
            i += 1
            delta += len(line)-1
          elif line.startswith(b'-'):
            d += 1
            delta -= len(line)-1
      names.append(patch.target)
      insert.append(i)
      delete.append(d)
      namelen = max(namelen, len(patch.target))
      maxdiff = max(maxdiff, i+d)
    output = ''
    statlen = len(str(maxdiff))  # stats column width
    for i,n in enumerate(names):
      # %-19s | %-4d %s
      format = " %-" + str(namelen) + "s | %" + str(statlen) + "s %s\n"

      hist = ''
      # -- calculating histogram --
      width = len(format % ('', '', ''))
      histwidth = max(2, 80 - width)
      if maxdiff < histwidth:
        hist = "+"*insert[i] + "-"*delete[i]
      else:
        iratio = (float(insert[i]) / maxdiff) * histwidth
        dratio = (float(delete[i]) / maxdiff) * histwidth

        # make sure every entry gets at least one + or -
        iwidth = 1 if 0 < iratio < 1 else int(iratio)
        dwidth = 1 if 0 < dratio < 1 else int(dratio)
        #print(iratio, dratio, iwidth, dwidth, histwidth)
        hist = "+"*int(iwidth) + "-"*int(dwidth)
      # -- /calculating +- histogram --
      output += (format % (tostr(names[i]), str(insert[i] + delete[i]), hist))

    output += (" %d files changed, %d insertions(+), %d deletions(-), %+d bytes"
               % (len(names), sum(insert), sum(delete), delta))
    return output
def diffstat ( self ) : names = [ ] insert = [ ] delete = [ ] delta = 0 namelen = 0 maxdiff = 0 for patch in self . items : i , d = 0 , 0 for hunk in patch . hunks : for line in hunk . text : if line . startswith ( b'+' ) : i += 1 delta += len ( line ) - 1 elif line . startswith ( b'-' ) : d += 1 delta -= len ( line ) - 1 names . append ( patch . target ) insert . append ( i ) delete . append ( d ) namelen = max ( namelen , len ( patch . target ) ) maxdiff = max ( maxdiff , i + d ) output = '' statlen = len ( str ( maxdiff ) ) for i , n in enumerate ( names ) : format = " %-" + str ( namelen ) + "s | %" + str ( statlen ) + "s %s\n" hist = '' width = len ( format % ( '' , '' , '' ) ) histwidth = max ( 2 , 80 - width ) if maxdiff < histwidth : hist = "+" * insert [ i ] + "-" * delete [ i ] else : iratio = ( float ( insert [ i ] ) / maxdiff ) * histwidth dratio = ( float ( delete [ i ] ) / maxdiff ) * histwidth iwidth = 1 if 0 < iratio < 1 else int ( iratio ) dwidth = 1 if 0 < dratio < 1 else int ( dratio ) hist = "+" * int ( iwidth ) + "-" * int ( dwidth ) output += ( format % ( tostr ( names [ i ] ) , str ( insert [ i ] + delete [ i ] ) , hist ) ) output += ( " %d files changed, %d insertions(+), %d deletions(-), %+d bytes" % ( len ( names ) , sum ( insert ) , sum ( delete ) , delta ) ) return output
calculate diffstat and return as a string
        Notes:
          - original diffstat ouputs target filename
          - single + or - shouldn't escape histogram