Add static method truncate()

This commit is contained in:
belono 2024-05-30 17:58:32 +02:00
parent 3ec5cf9973
commit 0e794f4bf0

View File

@ -919,6 +919,16 @@ class Escpos(object, metaclass=ABCMeta):
return text return text
@staticmethod
def truncate(text: str, width: int, placeholder: str = ".") -> str:
"""Truncate an string at a max width or leave it untouched.
Add a placeholder at the end of the output text if it has been truncated.
"""
ph_len = len(placeholder)
max_len = width - ph_len
return f"{text[:max_len]}{placeholder}" if len(text) > width else text
def set( def set(
self, self,
align: Optional[str] = None, align: Optional[str] = None,