Skip to content

Commit 7cf0565

Browse files
committed
Added alignment option and test
1 parent 48d8882 commit 7cf0565

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

table2ascii/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(
2929
body: Optional[List[List]],
3030
footer: Optional[List],
3131
column_widths: Optional[List[int]],
32+
alignments: Optional[List[Alignment]],
3233
options: Options,
3334
):
3435
"""Validate arguments and initialize fields"""
@@ -37,6 +38,8 @@ def __init__(
3738
self.__body = body
3839
self.__footer = footer
3940
self.__options = options
41+
42+
# calculate number of columns
4043
self.__columns = self.__count_columns()
4144

4245
# check if footer has a different number of columns
@@ -50,7 +53,7 @@ def __init__(
5053
"All rows in body must have the same number of columns as the other rows"
5154
)
5255

53-
# calculate column widths
56+
# calculate or use given column widths
5457
self.__column_widths = column_widths or self.__auto_column_widths()
5558

5659
# check if column widths specified have a different number of columns
@@ -64,6 +67,8 @@ def __init__(
6467
"All values in `column_widths` must be greater than or equal to 2"
6568
)
6669

70+
self.__alignments = alignments or [Alignment.CENTER] * self.__columns
71+
6772
"""
6873
╔═════╦═══════════════════════╗ ABBBBBCBBBBBDBBBBBDBBBBBDBBBBBE
6974
║ # ║ G H R S ║ F G H H H F
@@ -127,7 +132,7 @@ def __auto_column_widths(self) -> List[int]:
127132
column_widths.append(max(header_size, *body_size, footer_size) + 2)
128133
return column_widths
129134

130-
def __pad(self, text: str, width: int, alignment: Alignment = Alignment.CENTER):
135+
def __pad(self, text: str, width: int, alignment: Alignment):
131136
"""Pad a string of text to a given width with specified alignment"""
132137
if alignment == Alignment.LEFT:
133138
# pad with spaces on the end
@@ -163,7 +168,9 @@ def __row_to_ascii(
163168
filler * self.__column_widths[i]
164169
if isinstance(filler, str)
165170
# otherwise, use the column content
166-
else self.__pad(str(filler[i]), self.__column_widths[i])
171+
else self.__pad(
172+
str(filler[i]), self.__column_widths[i], self.__alignments[i]
173+
)
167174
)
168175
# column seperator
169176
sep = column_seperator
@@ -273,6 +280,7 @@ def table2ascii(
273280
body: Optional[List[List]] = None,
274281
footer: Optional[List] = None,
275282
column_widths: Optional[List[int]] = None,
283+
alignments: Optional[List[Alignment]] = None,
276284
**options,
277285
) -> str:
278286
"""Convert a 2D Python table to ASCII text
@@ -282,8 +290,12 @@ def table2ascii(
282290
:param body: :class:`Optional[List[List]]` 2-dimensional list of values in the table's body
283291
:param footer: :class:`Optional[List]` List of column values in the table's footer row
284292
:param column_widths: :class:`Optional[List[int]]` List of widths in characters for each column (defaults to auto-sizing)
285-
:param footer: :class:`Optional[List]` List of column values in the table's footer row
293+
:param alignments: :class:`Optional[List[Alignment]]` List of alignments (ex. `[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT]`)
294+
295+
### Additional options
296+
:param first_col_heading: :class:`Optional[bool]` Whether to add a header column separator after the first column
297+
:param last_col_heading: :class:`Optional[bool]` Whether to add a header column separator before the last column
286298
"""
287299
return TableToAscii(
288-
header, body, footer, column_widths, Options(**options)
300+
header, body, footer, column_widths, alignments, Options(**options)
289301
).to_ascii()

tests/test_alignments.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from table2ascii import table2ascii as t2a, Alignment
2+
3+
4+
def test_first_left_four_right():
5+
text = t2a(
6+
header=["#", "G", "H", "R", "S"],
7+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
8+
footer=["SUM", "130", "140", "135", "130"],
9+
first_col_heading=True,
10+
alignments=[Alignment.LEFT] + [Alignment.RIGHT] * 4,
11+
)
12+
expected = (
13+
"╔═════╦═══════════════════════╗\n"
14+
"║ # ║ G H R S ║\n"
15+
"╟─────╫───────────────────────╢\n"
16+
"║ 1 ║ 30 40 35 30 ║\n"
17+
"║ 2 ║ 30 40 35 30 ║\n"
18+
"╟─────╫───────────────────────╢\n"
19+
"║ SUM ║ 130 140 135 130 ║\n"
20+
"╚═════╩═══════════════════════╝\n"
21+
)
22+
assert text == expected

0 commit comments

Comments
 (0)