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
3 changes: 3 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ languages.
History
=======

*next version*
* **Bugfix** our `center()`_ to better match padding of `str.center()`_.

0.3.3 *2026-01-24*
* **Performance** improvement in `width()`_. `PR #185`_.
* **Bugfix** missing ``py.typed``, ``Typing :: Typed``. `PR #184`_.
Expand Down
41 changes: 24 additions & 17 deletions tests/test_justify.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@


def test_ljust():
assert ljust('hi', 5) == 'hi '
assert ljust('', 5) == ' '
assert ljust('hello', 3) == 'hello'
assert ljust('hello', 5) == 'hello'
# our ljust() matches standard python ljust() for ascii
assert ljust('hi', 5) == 'hi ' == str.ljust('hi', 5)
assert ljust('', 5) == ' ' == str.ljust('', 5)
assert ljust('hello', 3) == 'hello' == str.ljust('hello', 3)
assert ljust('hello', 5) == 'hello' == str.ljust('hello', 5)
assert ljust('hi', 5, fillchar='-') == 'hi---' == str.ljust('hi', 5, '-')
# advanced capabilities
assert ljust('\x1b[31mhi\x1b[0m', 5) == '\x1b[31mhi\x1b[0m '
assert ljust('\u4e2d', 4) == '\u4e2d '
assert ljust('hi', 5, fillchar='-') == 'hi---'
assert ljust('hi', 5, fillchar='\u00b7') == 'hi\u00b7\u00b7\u00b7'
assert ljust(CJK_WORD, 8) == CJK_WORD + ' '
assert width(ljust(CJK_WORD, 8)) == 8
Expand All @@ -27,30 +29,35 @@ def test_ljust():


def test_rjust():
assert rjust('hi', 5) == ' hi'
assert rjust('', 5) == ' '
assert rjust('hello', 3) == 'hello'
assert rjust('hello', 5) == 'hello'
# our rjust() matches standard python rjust() for ascii
assert rjust('hi', 5) == ' hi' == str.rjust('hi', 5)
assert rjust('', 5) == ' ' == str.rjust('', 5)
assert rjust('hello', 3) == 'hello' == str.rjust('hello', 3)
assert rjust('hello', 5) == 'hello' == str.rjust('hello', 5)
assert rjust('hi', 5, fillchar='-') == '---hi' == str.rjust('hi', 5, '-')
# advanced capabilities
assert rjust('\x1b[31mhi\x1b[0m', 5) == ' \x1b[31mhi\x1b[0m'
assert rjust('\u4e2d', 4) == ' \u4e2d'
assert rjust('hi', 5, fillchar='-') == '---hi'
assert rjust('hi', 5, fillchar='\u00b7') == '\u00b7\u00b7\u00b7hi'
assert rjust(CJK_WORD, 8) == ' ' + CJK_WORD
assert width(rjust(CAFE_COMBINING, 8)) == 8
assert width(rjust(EMOJI_FAMILY, 6)) == 6


def test_center():
assert center('hi', 6) == ' hi '
assert center('hi', 5) == ' hi '
assert center('', 4) == ' '
assert center('hello', 3) == 'hello'
assert center('hello', 5) == 'hello'
# our center() matches standard python center() for ascii
assert center('hi', 6) == ' hi ' == str.center('hi', 6)
assert center('hi', 5) == ' hi ' == str.center('hi', 5)
assert center('ab', 7) == ' ab ' == str.center('ab', 7)
assert center('', 4) == ' ' == str.center('', 4)
assert center('hello', 3) == 'hello' == str.center('hello', 3)
assert center('hello', 5) == 'hello' == str.center('hello', 5)
assert center('hi', 6, fillchar='-') == '--hi--' == str.center('hi', 6, '-')
assert center('x', 4) == ' x ' == str.center('x', 4)
# advanced capabilities
assert center('\x1b[31mhi\x1b[0m', 6) == ' \x1b[31mhi\x1b[0m '
assert center('\u4e2d', 6) == ' \u4e2d '
assert center('hi', 6, fillchar='-') == '--hi--'
assert center('hi', 6, fillchar='\u00b7') == '\u00b7\u00b7hi\u00b7\u00b7'
assert center('x', 4) == ' x '
assert width(center(CJK_WORD, 8)) == 8
assert width(center(CAFE_COMBINING, 8)) == 8
assert width(center(EMOJI_FAMILY, 6)) == 6
3 changes: 2 additions & 1 deletion wcwidth/wcwidth.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,8 @@ def center(
else:
text_width = width(text, control_codes=control_codes, ambiguous_width=ambiguous_width)
total_padding = max(0, dest_width - text_width)
left_pad = total_padding // 2
# matching https://jazcap53.github.io/pythons-eccentric-strcenter.html
left_pad = total_padding // 2 + (total_padding & dest_width & 1)
right_pad = total_padding - left_pad
return fillchar * left_pad + text + fillchar * right_pad

Expand Down
Loading