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

Commit 13a293c

Browse files
committed
1.0.2: New attributes, ok and table_width.
1 parent 6161df0 commit 13a293c

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

terminaltables.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
__author__ = '@Robpol86'
2727
__license__ = 'MIT'
28-
__version__ = '1.0.1'
28+
__version__ = '1.0.2'
2929

3030
DEFAULT_TERMINAL_HEIGHT = None
3131
DEFAULT_TERMINAL_WIDTH = None
@@ -166,7 +166,7 @@ def column_max_width(self, column_number):
166166
borders_padding = (len(column_widths) * self.padding_left) + (len(column_widths) * self.padding_right)
167167
if self.outer_border:
168168
borders_padding += 2
169-
if self.inner_column_border:
169+
if self.inner_column_border and column_widths:
170170
borders_padding += len(column_widths) - 1
171171
other_column_widths = sum(column_widths) - column_widths[column_number]
172172
return terminal_width() - other_column_widths - borders_padding
@@ -188,6 +188,11 @@ def column_widths(self):
188188

189189
return widths
190190

191+
@property
192+
def ok(self):
193+
"""Returns True if the table fits within the terminal width, False if the table breaks."""
194+
return self.table_width <= terminal_width()
195+
191196
@property
192197
def padded_table_data(self):
193198
"""Returns a list of lists of strings. It's self.table_data but with the cells padded with spaces and newlines.
@@ -265,6 +270,17 @@ def table(self):
265270

266271
return '\n'.join(final_table_data)
267272

273+
@property
274+
def table_width(self):
275+
"""Returns the width of the table including padding and borders."""
276+
column_widths = self.column_widths
277+
borders_padding = (len(column_widths) * self.padding_left) + (len(column_widths) * self.padding_right)
278+
if self.outer_border:
279+
borders_padding += 2
280+
if self.inner_column_border and column_widths:
281+
borders_padding += len(column_widths) - 1
282+
return sum(column_widths) + borders_padding
283+
268284

269285
class UnixTable(AsciiTable):
270286
"""Draw a table using box-drawing characters on Unix platforms. Table borders won't have any gaps between lines.

tests/test_table_width_ok.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from textwrap import dedent
2+
3+
import pytest
4+
5+
import terminaltables
6+
from terminaltables import AsciiTable, UnixTable
7+
8+
9+
@pytest.mark.parametrize('cls', [AsciiTable, UnixTable])
10+
def test_empty(cls):
11+
terminaltables.DEFAULT_TERMINAL_WIDTH = 80
12+
table = cls([])
13+
assert 2 == table.table_width
14+
assert table.ok is True
15+
16+
table = cls([[]])
17+
assert 2 == table.table_width
18+
assert table.ok is True
19+
20+
table = cls([['']])
21+
assert 4 == table.table_width
22+
assert table.ok is True
23+
24+
table = cls([[' ']])
25+
assert 5 == table.table_width
26+
assert table.ok is True
27+
28+
29+
@pytest.mark.parametrize('cls', [AsciiTable, UnixTable])
30+
def test_simple(cls):
31+
terminaltables.DEFAULT_TERMINAL_WIDTH = 80
32+
table_data = [
33+
['Name', 'Color', 'Type'],
34+
['Avocado', 'green', 'nut'],
35+
['Tomato', 'red', 'fruit'],
36+
['Lettuce', 'green', 'vegetable'],
37+
]
38+
table = cls(table_data)
39+
40+
assert 31 == table.table_width
41+
assert table.ok is True
42+
43+
table_data.append(['Watermelon', 'green', 'fruit'])
44+
assert 34 == table.table_width
45+
assert table.ok is True
46+
47+
terminaltables.DEFAULT_TERMINAL_WIDTH = 34
48+
assert table.ok is True
49+
50+
terminaltables.DEFAULT_TERMINAL_WIDTH = 33
51+
assert table.ok is False
52+
53+
54+
@pytest.mark.parametrize('cls', [AsciiTable, UnixTable])
55+
def test_multi_line(cls):
56+
terminaltables.DEFAULT_TERMINAL_WIDTH = 100
57+
table_data = [
58+
['Show', 'Characters'],
59+
['Rugrats', dedent('Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles,\n'
60+
'Susie Carmichael, Dil Pickles, Kimi Finster, Spike')],
61+
['South Park', 'Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick']
62+
]
63+
table = cls(table_data)
64+
assert 100 == table.table_width
65+
assert table.ok is True
66+
67+
68+
def test_attributes():
69+
table_data = [
70+
['Name', 'Color', 'Type'],
71+
['Avocado', 'green', 'nut'],
72+
['Tomato', 'red', 'fruit'],
73+
['Lettuce', 'green', 'vegetable'],
74+
]
75+
table = AsciiTable(table_data)
76+
77+
assert 31 == max(len(r) for r in table.table.splitlines())
78+
assert 31 == table.table_width
79+
80+
table.outer_border = False
81+
assert 29 == max(len(r) for r in table.table.splitlines())
82+
assert 29 == table.table_width
83+
84+
table.inner_column_border = False
85+
assert 27 == max(len(r) for r in table.table.splitlines())
86+
assert 27 == table.table_width
87+
88+
table.padding_left = 0
89+
assert 24 == max(len(r) for r in table.table.splitlines())
90+
assert 24 == table.table_width
91+
92+
table.padding_right = 0
93+
assert 21 == max(len(r) for r in table.table.splitlines())
94+
assert 21 == table.table_width

0 commit comments

Comments
 (0)