Skip to content

Commit

Permalink
- Added ljust_fmtd, rjust_fmtd, center_fmtd util functions to a…
Browse files Browse the repository at this point in the history
…lign strings with SGRs correctly.
  • Loading branch information
delameter committed Apr 21, 2022
1 parent 96bc981 commit 0b1a560
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION=1.7.1
VERSION=1.7.2
PYPI_USERNAME=__token__
PYPI_PASSWORD= #api token
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions dev/readme/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
7 changes: 5 additions & 2 deletions pytermor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'
20 changes: 20 additions & 0 deletions pytermor/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 0b1a560

Please sign in to comment.