def pretty_print(self, printer: Optional[Printer] = None, align: int = ALIGN_CENTER, border: bool = False): """ Pretty prints the table. :param printer: The printer to print with. :param align: The alignment of the cells(Table.ALIGN_CENTER/ALIGN_LEFT/ALIGN_RIGHT) :param border: Whether to add a border around the table """ if printer is None: printer = get_printer() table_string = self._get_pretty_table(indent=printer.indents_sum, align=align, border=border).get_string() if table_string != '': first_line = table_string.splitlines()[0] first_line_length = len(first_line) - len(re.findall(Printer._ANSI_REGEXP, first_line)) * \ Printer._ANSI_COLOR_LENGTH if self.title_align == self.ALIGN_CENTER: title = '{}{}'.format( ' ' * (first_line_length // 2 - len(self.title) // 2), self.title) elif self.title_align == self.ALIGN_LEFT: title = self.title else: title = '{}{}'.format( ' ' * (first_line_length - len(self.title)), self.title) printer.write_line(printer.YELLOW + title) # We split the table to lines in order to keep the indentation. printer.write_line(table_string)
def pretty_print(self, printer=None, min_width=1, min_unit_width=1): """ Prints the file size (and it's unit), reserving places for longer sizes and units. For example: min_unit_width = 1: 793 B 100 KB min_unit_width = 2: 793 B 100 KB min_unit_width = 3: 793 B 100 KB """ unit, unit_divider = self._unit_info() unit_color = self.SIZE_COLORS[unit] # Multiply and then divide by 100 in order to have only two decimal places. size_in_unit = (self.size * 100) / unit_divider / 100 # Add spaces to align the units. unit = ' ' * (min_unit_width - len(unit)) + unit size_string = '{0:.1f}'.format(size_in_unit) total_len = len(size_string) + 1 + len(unit) if printer is None: printer = pyprinter.get_printer() spaces_count = min_width - total_len if spaces_count > 0: printer.write(' ' * spaces_count) printer.write(size_string + ' ' + unit_color + unit)
def pretty_print(self, printer=None, align=ALIGN_CENTER, border=False): """ Pretty prints the table. :param printer: The printer to print with. :param align: The alignment of the cells(Table.ALIGN_CENTER/ALIGN_LEFT/ALIGN_RIGHT) :param border: Whether to add a border around the table """ if printer is None: printer = pyprinter.get_printer() table_string = self._get_pretty_table(indent=printer.indents_sum, align=align, border=border).get_string() if table_string != '': first_line = table_string.splitlines()[0] first_line_length = len(first_line) - len(re.findall(pyprinter.Printer._ANSI_REGEXP, first_line)) * \ pyprinter.Printer._ANSI_COLOR_LENGTH if self.title_align == self.ALIGN_CENTER: title = ' ' * (first_line_length // 2 - len(self.name) // 2) + self.name elif self.title_align == self.ALIGN_LEFT: title = self.name else: title = ' ' * (first_line_length - len(self.name)) + self.name printer.write_line(printer.YELLOW + title) # We split the table to lines in order to keep the indentation. printer.write_line(table_string)