def main(): # Default to terminal mode so redirecting to a file won't include the ANSI style sequences ansi.allow_style = ansi.STYLE_TERMINAL st = SimpleTable(columns) table = st.generate_table(data_list) ansi_print(table) bt = BorderedTable(columns) table = bt.generate_table(data_list) ansi_print(table) at = AlternatingTable(columns) table = at.generate_table(data_list) ansi_print(table)
def print_pivot_table(self): bold = functools.partial(ansi.style, bold=True) columns: List[Column] = list() columns.append(Column(bold("ID"), width=5)) columns.append(Column(bold("Alive"), width=6)) columns.append(Column(bold("Socks Port"), width=12)) columns.append(Column(bold("PID"), width=10)) columns.append(Column(bold("User"), width=25)) columns.append(Column(bold("Computer"), width=25)) columns.append(Column(bold("Last"), width=12)) data_list: List[List[Any]] = list() for pivot in ProxyPivots.instances.values(): data_list.append(pivot.get_list()) bt = BorderedTable(columns, column_borders=False) table = bt.generate_table(data_list) ansi.style_aware_write(sys.stdout, table + '\n\n')
def print_generic_table(self, data): columns: List[Column] = list() data_list: List[List[Any]] = list() max_len0 = 0 max_len1 = 0 for row in data: col_len = len(row[0]) if col_len > max_len0: max_len0 = col_len col_len = len(row[1]) if col_len > max_len1: max_len1 = col_len data_list.append(row) columns.append(Column("", width=max_len0)) columns.append(Column("", width=max_len1)) bt = BorderedTable(columns) table = bt.generate_table(data_list, include_header=False) ansi.style_aware_write(sys.stdout, table + '\n\n')
def test_bordered_table_width(): # Default behavior (column_borders=True, padding=1) assert BorderedTable.base_width(1) == 4 assert BorderedTable.base_width(2) == 7 assert BorderedTable.base_width(3) == 10 # No column borders assert BorderedTable.base_width(1, column_borders=False) == 4 assert BorderedTable.base_width(2, column_borders=False) == 6 assert BorderedTable.base_width(3, column_borders=False) == 8 # No padding assert BorderedTable.base_width(1, padding=0) == 2 assert BorderedTable.base_width(2, padding=0) == 3 assert BorderedTable.base_width(3, padding=0) == 4 # Extra padding assert BorderedTable.base_width(1, padding=3) == 8 assert BorderedTable.base_width(2, padding=3) == 15 assert BorderedTable.base_width(3, padding=3) == 22 # Invalid num_cols value with pytest.raises(ValueError) as excinfo: BorderedTable.base_width(0) assert "Column count cannot be less than 1" in str(excinfo.value) # Total width column_1 = Column("Col 1", width=15) column_2 = Column("Col 2", width=15) row_data = list() row_data.append(["Col 1 Row 1", "Col 2 Row 1"]) row_data.append(["Col 1 Row 2", "Col 2 Row 2"]) bt = BorderedTable([column_1, column_2]) assert bt.total_width() == 37
def test_bordered_table_creation(): column_1 = Column("Col 1", width=15) column_2 = Column("Col 2", width=15) row_data = list() row_data.append(["Col 1 Row 1", "Col 2 Row 1"]) row_data.append(["Col 1 Row 2", "Col 2 Row 2"]) # Default options bt = BorderedTable([column_1, column_2]) table = bt.generate_table(row_data) assert table == ('╔═════════════════╤═════════════════╗\n' '║ Col 1 │ Col 2 ║\n' '╠═════════════════╪═════════════════╣\n' '║ Col 1 Row 1 │ Col 2 Row 1 ║\n' '╟─────────────────┼─────────────────╢\n' '║ Col 1 Row 2 │ Col 2 Row 2 ║\n' '╚═════════════════╧═════════════════╝') # No column borders bt = BorderedTable([column_1, column_2], column_borders=False) table = bt.generate_table(row_data) assert table == ('╔══════════════════════════════════╗\n' '║ Col 1 Col 2 ║\n' '╠══════════════════════════════════╣\n' '║ Col 1 Row 1 Col 2 Row 1 ║\n' '╟──────────────────────────────────╢\n' '║ Col 1 Row 2 Col 2 Row 2 ║\n' '╚══════════════════════════════════╝') # No header bt = BorderedTable([column_1, column_2]) table = bt.generate_table(row_data, include_header=False) assert table == ('╔═════════════════╤═════════════════╗\n' '║ Col 1 Row 1 │ Col 2 Row 1 ║\n' '╟─────────────────┼─────────────────╢\n' '║ Col 1 Row 2 │ Col 2 Row 2 ║\n' '╚═════════════════╧═════════════════╝') # Non-default padding bt = BorderedTable([column_1, column_2], padding=2) table = bt.generate_table(row_data) assert table == ('╔═══════════════════╤═══════════════════╗\n' '║ Col 1 │ Col 2 ║\n' '╠═══════════════════╪═══════════════════╣\n' '║ Col 1 Row 1 │ Col 2 Row 1 ║\n' '╟───────────────────┼───────────────────╢\n' '║ Col 1 Row 2 │ Col 2 Row 2 ║\n' '╚═══════════════════╧═══════════════════╝') # Invalid padding with pytest.raises(ValueError) as excinfo: BorderedTable([column_1, column_2], padding=-1) assert "Padding cannot be less than 0" in str(excinfo.value)