Skip to content
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
26 changes: 24 additions & 2 deletions src/escpos/escpos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""
from __future__ import annotations

import re
import textwrap
import time
import warnings
Expand Down Expand Up @@ -108,7 +109,7 @@
for name in barcode.PROVIDED_BARCODES
}

Alignment = Union[Literal["center", "left", "right"], str]
Alignment = Union[Literal["center", "left", "right", "justify"], str]


class Escpos(object, metaclass=ABCMeta):
Expand Down Expand Up @@ -920,7 +921,21 @@ def block_text(self, txt, font="0", columns=None) -> None:
self.text(textwrap.fill(txt, col_count))

@staticmethod
def _justify(txt: str, width: int) -> str:
"""Justify-text on left AND right sides by padding spaces.

code by: Georgina Skibinski https://stackoverflow.com/a/66087666
suggested by agordon @https://github.com/python-escpos/python-escpos/pull/652
"""
prev_txt = txt
while (length := width - len(txt)) > 0:
txt = re.sub(r"(\s+)", r"\1 ", txt, count=length)
if txt == prev_txt:
break
return txt.rjust(width)

def _padding(
self,
text: str,
width: int,
align: Alignment = "center",
Expand All @@ -936,6 +951,10 @@ def _padding(
text = f"{text:<{width}}"
elif align == "right":
text = f"{text:>{width}}"
elif align == "justify":
text = self._justify(text, width)
else:
raise ValueError("Expected a valid alignment: center|left|right|justify")

return text

Expand Down Expand Up @@ -972,7 +991,7 @@ def _rearrange_into_cols(self, text_list: list, widths: list[int]) -> list:
textwrap.wrap(text, widths[i], break_long_words=False)
for i, text in enumerate(text_list)
]
max_len = max(*[len(text_group) for text_group in wrapped])
max_len = max(0, *[len(text_group) for text_group in wrapped])
text_colums = []
for i in range(max_len):
row = ["" for _ in range(n_cols)]
Expand Down Expand Up @@ -1013,6 +1032,9 @@ def software_columns(
If the list of alignment items is shorter than the list of strings then
the last alignment of the list will be applied till the last string (column).
"""
if not all([text_list, widths, align]):
raise TypeError("Value can't be of type None")

n_cols = len(text_list)

if isinstance(widths, int):
Expand Down
12 changes: 6 additions & 6 deletions test/test_functions/test_function_software_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ def test_add_padding_into_cols(driver) -> None:
"""

output = driver._add_padding_into_cols(
text_list=["col1", "col2", "col3"],
widths=[6, 6, 6],
align=["center", "left", "right"],
text_list=["col1", "col2", "col3", "col 4"],
widths=[6, 6, 6, 6],
align=["center", "left", "right", "justify"],
)
assert output == [" col1 ", "col2 ", " col3"]
assert output == [" col1 ", "col2 ", " col3", "col 4"]


@pytest.mark.parametrize("text_list", ["", [], None])
Expand All @@ -55,7 +55,7 @@ def test_software_columns_invalid_args(driver, text_list, widths, align) -> None

bad_args = [bad_text_list, bad_widths, bad_align]
for kwargs in bad_args:
with pytest.raises(Exception):
with pytest.raises((TypeError, ValueError)):
driver.software_columns(**kwargs)
driver.close()

Expand All @@ -69,7 +69,7 @@ def test_software_columns_invalid_args(driver, text_list, widths, align) -> None
],
)
@pytest.mark.parametrize("widths", [[10, 10, 10], [10], 30])
@pytest.mark.parametrize("align", [["center", "left", "right"], ["center"], "center"])
@pytest.mark.parametrize("align", [["center", "left", "right"], ["center"], "justify"])
def test_software_columns_valid_args(driver, text_list, widths, align) -> None:
"""
GIVEN a dummy printer object
Expand Down
Loading