def write_overflow(row): overflow = [''] * len(self.columns) column_idx = 0 for (col, value) in zip(self.columns, row): if column_idx > 0: fp.write(' ' * self.spacing) if isinstance(value, str): pass else: value = str(value) if (ansi_len(value) <= col.width): fp.write(ansi_ljust(value, col.width)) else: wrapped_line = [ line for line in wrap_line(value, col.width) ] if len(wrapped_line) > 1: overflow[column_idx] = ' '.join(wrapped_line[1:]) fp.write(wrapped_line[0]) # Move to next column column_idx += 1 fp.write('\n') # deal with overflowed data if ''.join(overflow): write_overflow(overflow)
def write_overflow(row): overflow = [''] * len(self.columns) column_idx = 0 for (col, value) in zip(self.columns, row): if column_idx > 0: fp.write(' ' * self.spacing) if isinstance(value, str): pass else: value = str(value) if(ansi_len(value) <= col.width): fp.write(ansi_ljust(value, col.width)) else: wrapped_line = [ line for line in wrap_line(value, col.width) ] if len(wrapped_line) > 1: overflow[column_idx] = ' '.join(wrapped_line[1:]) fp.write(wrapped_line[0]) # Move to next column column_idx += 1 fp.write('\n') # deal with overflowed data if ''.join(overflow): write_overflow(overflow)
def write_row(self, fp, *args): ''' Print a single row. :param file fp: the output file stream (usually sys.stdout or sys.stderr) :param list args: the column values for the row ''' for (width, value) in zip(self.widths, args): fp.write(ansi_ljust(value, width)) fp.write('\n')
def title_str(title, width=80, align='left', hr='=', box=False): lines = [] if box: border = '+' + ('-'*(width-2)) + '+' t = None if align == 'left': t = ansi_ljust(title, width-4) elif align == 'center': t = ansi_center(title, width-4) else: t = ansi_rjust(title, width-4) lines.append(border) lines.append('| ' + t + ' |') lines.append(border) else: if align == 'left': lines.append(title) elif align == 'center': lines.append(ansi_center(title, width)) elif align == 'right': lines.append(ansi_rjust(title, width)) lines.append(hr * width) return '\n'.join(lines)
def title_str(title, width=80, align='left', hr='=', box=False): lines = [] if box: border = '+' + ('-' * (width - 2)) + '+' t = None if align == 'left': t = ansi_ljust(title, width - 4) elif align == 'center': t = ansi_center(title, width - 4) else: t = ansi_rjust(title, width - 4) lines.append(border) lines.append('| ' + t + ' |') lines.append(border) else: if align == 'left': lines.append(title) elif align == 'center': lines.append(ansi_center(title, width)) elif align == 'right': lines.append(ansi_rjust(title, width)) lines.append(hr * width) return '\n'.join(lines)
def write(self, fp): ''' Print the table to a specified file stream. :param file fp: output stream ''' def write_overflow(row): overflow = [''] * len(self.columns) column_idx = 0 for (col, value) in zip(self.columns, row): if column_idx > 0: fp.write(' ' * self.spacing) if isinstance(value, str): pass else: value = str(value) if(ansi_len(value) <= col.width): fp.write(ansi_ljust(value, col.width)) else: wrapped_line = [ line for line in wrap_line(value, col.width) ] if len(wrapped_line) > 1: overflow[column_idx] = ' '.join(wrapped_line[1:]) fp.write(wrapped_line[0]) # Move to next column column_idx += 1 fp.write('\n') # deal with overflowed data if ''.join(overflow): write_overflow(overflow) total = sum([col.width for col in self.columns]) # Resize columns if last too wide # TODO: Smarter column resizing, maybe pick widest column if (total + self.spacing * (len(self.columns)-1)) > self.width: self.columns[-1].mode = Column.Grow for col in self.columns: if col.mode == Column.Grow: remaining = ( self.width - ((len(self.columns) - 1) * self.spacing) - total ) col.width += remaining if self.header: i = 0 for col in self.columns: if i > 0: fp.write(' ' * self.spacing) fp.write(ansi_ljust(col.text, col.width)) i += 1 fp.write('\n') fp.write('='*self.width) fp.write('\n') for row in self.rows: write_overflow(row) return 0
def write(self, fp): ''' Print the table to a specified file stream. :param file fp: output stream ''' def write_overflow(row): overflow = [''] * len(self.columns) column_idx = 0 for (col, value) in zip(self.columns, row): if column_idx > 0: fp.write(' ' * self.spacing) if isinstance(value, str): pass else: value = str(value) if (ansi_len(value) <= col.width): fp.write(ansi_ljust(value, col.width)) else: wrapped_line = [ line for line in wrap_line(value, col.width) ] if len(wrapped_line) > 1: overflow[column_idx] = ' '.join(wrapped_line[1:]) fp.write(wrapped_line[0]) # Move to next column column_idx += 1 fp.write('\n') # deal with overflowed data if ''.join(overflow): write_overflow(overflow) total = sum([col.width for col in self.columns]) # Resize columns if last too wide # TODO: Smarter column resizing, maybe pick widest column if (total + self.spacing * (len(self.columns) - 1)) > self.width: self.columns[-1].mode = Column.Grow for col in self.columns: if col.mode == Column.Grow: remaining = (self.width - ((len(self.columns) - 1) * self.spacing) - total) col.width += remaining if self.header: i = 0 for col in self.columns: if i > 0: fp.write(' ' * self.spacing) fp.write(ansi_ljust(col.text, col.width)) i += 1 fp.write('\n') fp.write('=' * self.width) fp.write('\n') for row in self.rows: write_overflow(row) return 0