Skip to content

fix: convert integers to strings to allow in tables #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2021
Merged
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: 10 additions & 7 deletions table2ascii/table_to_ascii.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from math import ceil, floor
from typing import List, Optional, Union
from typing import Any, List, Optional, Union

from .alignment import Alignment
from .options import Options
Expand Down Expand Up @@ -93,27 +93,30 @@ def __auto_column_widths(self) -> List[int]:
column_widths = []
for i in range(self.__columns):
# number of characters in column of i of header, each body row, and footer
header_size = len(self.__header[i]) if self.__header else 0
header_size = len(str(self.__header[i])) if self.__header else 0
body_size = (
map(lambda row, i=i: len(row[i]), self.__body) if self.__body else [0]
map(lambda row, i=i: len(str(row[i])), self.__body)
if self.__body
else [0]
)
footer_size = len(self.__footer[i]) if self.__footer else 0
footer_size = len(str(self.__footer[i])) if self.__footer else 0
# get the max and add 2 for padding each side with a space
column_widths.append(max(header_size, *body_size, footer_size) + 2)
return column_widths

def __pad(self, text: str, width: int, alignment: Alignment) -> str:
def __pad(self, cell_value: Any, width: int, alignment: Alignment) -> str:
"""
Pad a string of text to a given width with specified alignment

Args:
text (str): The text to pad
cell_value (Any): The text in the cell to pad
width (int): The width in characters to pad to
alignment (Alignment): The alignment to use

Returns:
str: The padded text
"""
text = str(cell_value)
if alignment == Alignment.LEFT:
# pad with spaces on the end
return f" {text} " + (" " * (width - len(text) - 2))
Expand Down Expand Up @@ -152,7 +155,7 @@ def __row_to_ascii(
if isinstance(filler, str)
# otherwise, use the column content
else self.__pad(
str(filler[i]), self.__column_widths[i], self.__alignments[i]
filler[i], self.__column_widths[i], self.__alignments[i]
)
)
# column seperator
Expand Down
23 changes: 22 additions & 1 deletion tests/test_alignments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from table2ascii import table2ascii as t2a, Alignment
from table2ascii import alignment, table2ascii as t2a, Alignment

import pytest

Expand Down Expand Up @@ -44,3 +44,24 @@ def test_invalid_alignments():
first_col_heading=True,
alignments=[9999, -1, Alignment.RIGHT, Alignment.CENTER, Alignment.RIGHT],
)


def test_alignment_numeric_data():
text = t2a(
header=[1, "G", "H", "R", "S"],
body=[[1, 2, 3, 4, 5]],
footer=["A", "B", 1, 2, 3],
column_widths=[4, 5, 5, 4, 5],
alignments=[Alignment.RIGHT] + [Alignment.CENTER] * 4,
first_col_heading=True,
)
expected = (
"╔════╦══════════════════════╗\n"
"║ 1 ║ G H R S ║\n"
"╟────╫──────────────────────╢\n"
"║ 1 ║ 2 3 4 5 ║\n"
"╟────╫──────────────────────╢\n"
"║ A ║ B 1 2 3 ║\n"
"╚════╩══════════════════════╝"
)
assert text == expected
22 changes: 21 additions & 1 deletion tests/test_convert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from table2ascii import table2ascii as t2a
from table2ascii import alignment, table2ascii as t2a

import pytest

Expand Down Expand Up @@ -179,3 +179,23 @@ def test_empty_body():
"╚═══╩═══════════════╝"
)
assert text == expected


def test_numeric_data():
text = t2a(
header=[1, "G", "H", "R", "S"],
body=[[1, 2, 3, 4, 5]],
footer=["A", "B", 1, 2, 3],
column_widths=[4, 5, 5, 4, 5],
first_col_heading=True,
)
expected = (
"╔════╦══════════════════════╗\n"
"║ 1 ║ G H R S ║\n"
"╟────╫──────────────────────╢\n"
"║ 1 ║ 2 3 4 5 ║\n"
"╟────╫──────────────────────╢\n"
"║ A ║ B 1 2 3 ║\n"
"╚════╩══════════════════════╝"
)
assert text == expected