From 0b1a56005a8a89536bbdaba8b778c408de605310 Mon Sep 17 00:00:00 2001 From: delameter Date: Thu, 21 Apr 2022 16:40:11 +0300 Subject: [PATCH] - Added `ljust_fmtd`, `rjust_fmtd`, `center_fmtd` util functions to align strings with SGRs correctly. --- .env.dist | 2 +- README.md | 4 ++++ dev/readme/README.tpl.md | 4 ++++ pytermor/__init__.py | 7 +++++-- pytermor/util.py | 20 ++++++++++++++++++++ setup.cfg | 2 +- 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/.env.dist b/.env.dist index 11962322..bd67eb12 100644 --- a/.env.dist +++ b/.env.dist @@ -1,3 +1,3 @@ -VERSION=1.7.1 +VERSION=1.7.2 PYPI_USERNAME=__token__ PYPI_PASSWORD= #api token diff --git a/README.md b/README.md index 54f5c8d8..2d79e6e1 100644 --- a/README.md +++ b/README.md @@ -815,6 +815,10 @@ You can of course create your own sequences and formats, but with one limitation ## Changelog +### v1.7.2 + +- Added `ljust_fmtd`, `rjust_fmtd`, `center_fmtd` util functions to align strings with SGRs correctly. + ### v1.7.1 - Print reset sequence as `\e[m` instead of `\e[0m`. diff --git a/dev/readme/README.tpl.md b/dev/readme/README.tpl.md index b4396c8a..06966a04 100644 --- a/dev/readme/README.tpl.md +++ b/dev/readme/README.tpl.md @@ -720,6 +720,10 @@ You can of course create your own sequences and formats, but with one limitation ## Changelog +### v1.7.2 + +- Added `ljust_fmtd`, `rjust_fmtd`, `center_fmtd` util functions to align strings with SGRs correctly. + ### v1.7.1 - Print reset sequence as `\e[m` instead of `\e[0m`. diff --git a/pytermor/__init__.py b/pytermor/__init__.py index a52f9f30..a0cc15a4 100644 --- a/pytermor/__init__.py +++ b/pytermor/__init__.py @@ -4,7 +4,7 @@ # ----------------------------------------------------------------------------- from .seq import build, build_c256, build_rgb, SequenceSGR from .fmt import autof, Format -from .util import apply_filters, StringFilter, ReplaceCSI, ReplaceSGR, ReplaceNonAsciiBytes +from .util import * __all__ = [ 'build', @@ -16,9 +16,12 @@ 'Format', 'apply_filters', + 'ljust_fmtd', + 'rjust_fmtd', + 'center_fmtd', 'StringFilter', 'ReplaceCSI', 'ReplaceSGR', 'ReplaceNonAsciiBytes', ] -__version__ = '1.7.1' +__version__ = '1.7.2' diff --git a/pytermor/util.py b/pytermor/util.py index 666c8ed4..fccdab94 100644 --- a/pytermor/util.py +++ b/pytermor/util.py @@ -40,3 +40,23 @@ def __init__(self, repl: bytes = b'?'): def apply_filters(string: AnyStr, *args: StringFilter|Type[StringFilter]) -> AnyStr: filters = map(lambda t: t() if isinstance(t, type) else t, args) return reduce(lambda s, f: f.apply(s), filters, string) + + +def ljust_fmtd(s: str, width: int, fillchar: str = ' ') -> str: + sanitized = ReplaceSGR().apply(s) + return s + fillchar * max(0, width - len(sanitized)) + + +def rjust_fmtd(s: str, width: int, fillchar: str = ' ') -> str: + sanitized = ReplaceSGR().apply(s) + return fillchar * max(0, width - len(sanitized)) + s + + +def center_fmtd(s: str, width: int, fillchar: str = ' ') -> str: + sanitized = ReplaceSGR().apply(s) + fill_len = max(0, width - len(sanitized)) + if fill_len == 0: + return s + right_fill_len = fill_len // 2 + left_fill_len = fill_len - right_fill_len + return (fillchar * left_fill_len) + s + (fillchar * right_fill_len) diff --git a/setup.cfg b/setup.cfg index a7dd9670..10ec78a4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = pytermor -version = 1.7.1 +version = 1.7.2 author = Aleksandr Shavykin author_email = 0.delameter@gmail.com description = ANSI formatted terminal output library