Exemple #1
0
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)
Exemple #2
0
def test_alternating_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
    at = AlternatingTable([column_1, column_2])
    table = at.generate_table(row_data)
    assert table == ('╔═════════════════╤═════════════════╗\n'
                     '║ Col 1           │ Col 2           ║\n'
                     '╠═════════════════╪═════════════════╣\n'
                     '║ Col 1 Row 1     │ Col 2 Row 1     ║\n'
                     '\x1b[100m║ \x1b[49m\x1b[0m\x1b[100mCol 1 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m │ \x1b[49m\x1b[0m\x1b[100mCol 2 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m ║\x1b[49m\n'
                     '╚═════════════════╧═════════════════╝')

    # Other bg colors
    at = AlternatingTable([column_1, column_2], bg_odd=ansi.bg.bright_blue, bg_even=ansi.bg.green)
    table = at.generate_table(row_data)
    assert table == ('╔═════════════════╤═════════════════╗\n'
                     '║ Col 1           │ Col 2           ║\n'
                     '╠═════════════════╪═════════════════╣\n'
                     '\x1b[104m║ \x1b[49m\x1b[0m\x1b[104mCol 1 Row 1\x1b[49m\x1b[0m\x1b[104m \x1b[49m\x1b[104m \x1b[49m\x1b[104m \x1b[49m\x1b[104m \x1b[49m\x1b[0m\x1b[104m │ \x1b[49m\x1b[0m\x1b[104mCol 2 Row 1\x1b[49m\x1b[0m\x1b[104m \x1b[49m\x1b[104m \x1b[49m\x1b[104m \x1b[49m\x1b[104m \x1b[49m\x1b[0m\x1b[104m ║\x1b[49m\n'
                     '\x1b[42m║ \x1b[49m\x1b[0m\x1b[42mCol 1 Row 2\x1b[49m\x1b[0m\x1b[42m \x1b[49m\x1b[42m \x1b[49m\x1b[42m \x1b[49m\x1b[42m \x1b[49m\x1b[0m\x1b[42m │ \x1b[49m\x1b[0m\x1b[42mCol 2 Row 2\x1b[49m\x1b[0m\x1b[42m \x1b[49m\x1b[42m \x1b[49m\x1b[42m \x1b[49m\x1b[42m \x1b[49m\x1b[0m\x1b[42m ║\x1b[49m\n'
                     '╚═════════════════╧═════════════════╝')

    # No column borders
    at = AlternatingTable([column_1, column_2], column_borders=False)
    table = at.generate_table(row_data)
    assert table == ('╔══════════════════════════════════╗\n'
                     '║ Col 1            Col 2           ║\n'
                     '╠══════════════════════════════════╣\n'
                     '║ Col 1 Row 1      Col 2 Row 1     ║\n'
                     '\x1b[100m║ \x1b[49m\x1b[0m\x1b[100mCol 1 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m  \x1b[49m\x1b[0m\x1b[100mCol 2 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m ║\x1b[49m\n'
                     '╚══════════════════════════════════╝')

    # No header
    at = AlternatingTable([column_1, column_2])
    table = at.generate_table(row_data, include_header=False)
    assert table == ('╔═════════════════╤═════════════════╗\n'
                     '║ Col 1 Row 1     │ Col 2 Row 1     ║\n'
                     '\x1b[100m║ \x1b[49m\x1b[0m\x1b[100mCol 1 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m │ \x1b[49m\x1b[0m\x1b[100mCol 2 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m ║\x1b[49m\n'
                     '╚═════════════════╧═════════════════╝')

    # Non-default padding
    at = AlternatingTable([column_1, column_2], padding=2)
    table = at.generate_table(row_data)
    assert table == ('╔═══════════════════╤═══════════════════╗\n'
                     '║  Col 1            │  Col 2            ║\n'
                     '╠═══════════════════╪═══════════════════╣\n'
                     '║  Col 1 Row 1      │  Col 2 Row 1      ║\n'
                     '\x1b[100m║  \x1b[49m\x1b[0m\x1b[100mCol 1 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m  │  \x1b[49m\x1b[0m\x1b[100mCol 2 Row 2\x1b[49m\x1b[0m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[100m \x1b[49m\x1b[0m\x1b[100m  ║\x1b[49m\n'
                     '╚═══════════════════╧═══════════════════╝')

    # Invalid padding
    with pytest.raises(ValueError) as excinfo:
        AlternatingTable([column_1, column_2], padding=-1)
    assert "Padding cannot be less than 0" in str(excinfo.value)