From 3ec5cf99735a46cbbdde50d6fba357f577cb53f0 Mon Sep 17 00:00:00 2001 From: belono Date: Thu, 30 May 2024 17:56:43 +0200 Subject: [PATCH] Add static method padding() --- src/escpos/escpos.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 1f6adeb..3ce90e2 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -899,6 +899,26 @@ class Escpos(object, metaclass=ABCMeta): col_count = self.profile.get_columns(font) if columns is None else columns self.text(textwrap.fill(txt, col_count)) + @staticmethod + def padding( + text: str, + width: int, + align: Alignment = "center", + ) -> str: + """Add fill space to meet the width. + + The align parameter sets the alignment of the text in space. + """ + align = align.lower() + if align == "center": + text = f"{text:^{width}}" + elif align == "left": + text = f"{text:<{width}}" + elif align == "right": + text = f"{text:>{width}}" + + return text + def set( self, align: Optional[str] = None,