Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Added ability to add separators as a row inside the table. #51

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from colorclass import Color, Windows

from terminaltables import SingleTable
from terminaltables import SEPARATOR, SingleTable


def table_server_timings():
Expand Down Expand Up @@ -60,6 +60,17 @@ def table_abcd():
return '\n'.join(combined)


def table_separators():
"""Return table string to be printed, including separators."""
table_data = [["X", "Y"]]
for x in range(0, 3):
for y in range(0, 5):
table_data.append([Color("{autoyellow}" + str(x) + "{/autoyellow}"), Color(str(y))])
table_data.append([SEPARATOR])
table_instance = SingleTable(table_data, "Separators")
return table_instance.table


def main():
"""Main function."""
Windows.enable(auto_colors=True, reset_atexit=True) # Does nothing if not on Windows.
Expand All @@ -76,6 +87,10 @@ def main():
print(table_abcd())
print()

# Separators
print(table_separators())
print()

# Instructions.
table_instance = SingleTable([['Obey Obey Obey Obey']], 'Instructions')
print(table_instance.table)
Expand Down
1 change: 1 addition & 0 deletions terminaltables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from terminaltables.other_tables import DoubleTable # noqa
from terminaltables.other_tables import SingleTable # noqa
from terminaltables.other_tables import PorcelainTable # noqa
from terminaltables.width_and_alignment import SEPARATOR # noqa

__author__ = '@Robpol86'
__license__ = 'MIT'
Expand Down
9 changes: 5 additions & 4 deletions terminaltables/base_table.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Base table class. Define just the bare minimum to build tables."""

from terminaltables.build import build_border, build_row, flatten
from terminaltables.width_and_alignment import align_and_pad_cell, max_dimensions
from terminaltables.width_and_alignment import align_and_pad_cell, max_dimensions, SEPARATOR


class BaseTable(object):
Expand Down Expand Up @@ -191,8 +191,9 @@ def gen_table(self, inner_widths, inner_heights, outer_widths):
style = 'footing'
else:
style = 'row'
for line in self.gen_row_lines(row, style, inner_widths, inner_heights[i]):
yield line
if SEPARATOR not in row:
for line in self.gen_row_lines(row, style, inner_widths, inner_heights[i]):
yield line
# If this is the last row then break. No separator needed.
if i == last_row_index:
break
Expand All @@ -203,7 +204,7 @@ def gen_table(self, inner_widths, inner_heights, outer_widths):
elif self.inner_footing_row_border and i == before_last_row_index:
yield self.horizontal_border('footing', outer_widths)
# Yield row separator.
elif self.inner_row_border:
elif self.inner_row_border or SEPARATOR in row:
yield self.horizontal_border('row', outer_widths)

# Yield bottom border.
Expand Down
6 changes: 6 additions & 0 deletions terminaltables/width_and_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from terminaltables.terminal_io import terminal_size


SEPARATOR = "<terminaltables.separator>"
RE_COLOR_ANSI = re.compile(r'(\033\[[\d;]+m)')


Expand All @@ -20,6 +22,10 @@ def visible_width(string):
:return: String's width.
:rtype: int
"""
# separator has no width
if SEPARATOR in string:
return 0

if '\033' in string:
string = RE_COLOR_ANSI.sub('', string)

Expand Down
40 changes: 40 additions & 0 deletions tests/test_base_table/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from termcolor import colored

from terminaltables.base_table import BaseTable
from terminaltables.width_and_alignment import SEPARATOR


def test_ascii():
Expand All @@ -32,6 +33,45 @@ def test_ascii():
assert actual == expected


def test_separators():
"""Test with SEPARATORs thrown in the mix."""
table_data = [
# vegetables
['Name', 'Color', 'Type'],
['Lettuce', 'green', 'vegetable'],
['Potato', 'yellow', 'vegetable'],
['Carrot', 'red', 'vegetable'],
# fruits
[SEPARATOR],
['Tomato', 'red', 'fruit'],
['Durian', 'yellow', 'fruit'],
# nuts
[SEPARATOR],
['Avocado', 'green', 'nut'],
['Wallnut', 'brown', 'nut'],
]

table = BaseTable(table_data)
actual = table.table
expected = (
'+---------+--------+-----------+\n'
'| Name | Color | Type |\n'
'+---------+--------+-----------+\n'
'| Lettuce | green | vegetable |\n'
'| Potato | yellow | vegetable |\n'
'| Carrot | red | vegetable |\n'
'+---------+--------+-----------+\n'
'| Tomato | red | fruit |\n'
'| Durian | yellow | fruit |\n'
'+---------+--------+-----------+\n'
'| Avocado | green | nut |\n'
'| Wallnut | brown | nut |\n'
'+---------+--------+-----------+'
)

assert actual == expected


def test_int():
"""Test with integers instead of strings."""
table_data = [
Expand Down
13 changes: 12 additions & 1 deletion tests/test_width_and_alignment/test_max_dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from colorclass import Color
from termcolor import colored

from terminaltables.width_and_alignment import max_dimensions
from terminaltables.width_and_alignment import max_dimensions, SEPARATOR


@pytest.mark.parametrize('table_data,expected_w,expected_h', [
Expand Down Expand Up @@ -43,6 +43,17 @@ def test_single_line():
assert max_dimensions(table_data, 2, 2) == ([10, 5, 9], [1, 1, 1, 1, 1], [14, 9, 13], [1, 1, 1, 1, 1])


def test_separator():
"""Test separator inside table."""
table_data = [
['Name', 'Color', 'Type'],
[SEPARATOR],
['Avocado', 'green', 'nut'],
]

assert max_dimensions(table_data, 1, 1) == ([7, 5, 4], [1, 1, 1], [9, 7, 6], [1, 1, 1])


def test_multi_line():
"""Test heights."""
table_data = [
Expand Down
3 changes: 2 additions & 1 deletion tests/test_width_and_alignment/test_table_width.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Test function in module."""

from terminaltables.width_and_alignment import max_dimensions, table_width
from terminaltables.width_and_alignment import max_dimensions, SEPARATOR, table_width


def test_empty():
"""Test with zero-length cells."""
assert table_width(max_dimensions([['']])[2], 0, 0) == 0
assert table_width(max_dimensions([[SEPARATOR]])[2], 0, 0) == 0
assert table_width(max_dimensions([['', '', '']])[2], 0, 0) == 0
assert table_width(max_dimensions([['', '', ''], ['', '', '']])[2], 0, 0) == 0

Expand Down
5 changes: 4 additions & 1 deletion tests/test_width_and_alignment/test_visible_width.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from colorclass import Color
from termcolor import colored

from terminaltables.width_and_alignment import visible_width
from terminaltables.width_and_alignment import SEPARATOR, visible_width


@pytest.mark.parametrize('string,expected', [
Expand All @@ -18,6 +18,9 @@
('معرب', 4),
('hello 世界', 10),

# separator
(SEPARATOR, 0),

# str+ansi
('\x1b[34mhello, world\x1b[39m', 12),
('\x1b[34m世界你好\x1b[39m', 8),
Expand Down