Skip to content

Commit d622de1

Browse files
committed
bugfix our center() padding algorithm
1 parent a1b9a83 commit d622de1

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

docs/intro.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ languages.
442442
History
443443
=======
444444

445+
*next version*
446+
* **Bugfix** our `center()`_ to better match padding of `str.center()`_.
447+
445448
0.3.3 *2026-01-24*
446449
* **Performance** improvement in `width()`_. `PR #185`_.
447450
* **Bugfix** missing ``py.typed``, ``Typing :: Typed``. `PR #184`_.

tests/test_justify.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111

1212
def test_ljust():
13-
assert ljust('hi', 5) == 'hi '
14-
assert ljust('', 5) == ' '
15-
assert ljust('hello', 3) == 'hello'
16-
assert ljust('hello', 5) == 'hello'
13+
# our ljust() matches standard python ljust() for ascii
14+
assert ljust('hi', 5) == 'hi ' == str.ljust('hi', 5)
15+
assert ljust('', 5) == ' ' == str.ljust('', 5)
16+
assert ljust('hello', 3) == 'hello' == str.ljust('hello', 3)
17+
assert ljust('hello', 5) == 'hello' == str.ljust('hello', 5)
18+
assert ljust('hi', 5, fillchar='-') == 'hi---' == str.ljust('hi', 5, '-')
19+
# advanced capabilities
1720
assert ljust('\x1b[31mhi\x1b[0m', 5) == '\x1b[31mhi\x1b[0m '
1821
assert ljust('\u4e2d', 4) == '\u4e2d '
19-
assert ljust('hi', 5, fillchar='-') == 'hi---'
2022
assert ljust('hi', 5, fillchar='\u00b7') == 'hi\u00b7\u00b7\u00b7'
2123
assert ljust(CJK_WORD, 8) == CJK_WORD + ' '
2224
assert width(ljust(CJK_WORD, 8)) == 8
@@ -27,30 +29,35 @@ def test_ljust():
2729

2830

2931
def test_rjust():
30-
assert rjust('hi', 5) == ' hi'
31-
assert rjust('', 5) == ' '
32-
assert rjust('hello', 3) == 'hello'
33-
assert rjust('hello', 5) == 'hello'
32+
# our rjust() matches standard python rjust() for ascii
33+
assert rjust('hi', 5) == ' hi' == str.rjust('hi', 5)
34+
assert rjust('', 5) == ' ' == str.rjust('', 5)
35+
assert rjust('hello', 3) == 'hello' == str.rjust('hello', 3)
36+
assert rjust('hello', 5) == 'hello' == str.rjust('hello', 5)
37+
assert rjust('hi', 5, fillchar='-') == '---hi' == str.rjust('hi', 5, '-')
38+
# advanced capabilities
3439
assert rjust('\x1b[31mhi\x1b[0m', 5) == ' \x1b[31mhi\x1b[0m'
3540
assert rjust('\u4e2d', 4) == ' \u4e2d'
36-
assert rjust('hi', 5, fillchar='-') == '---hi'
3741
assert rjust('hi', 5, fillchar='\u00b7') == '\u00b7\u00b7\u00b7hi'
3842
assert rjust(CJK_WORD, 8) == ' ' + CJK_WORD
3943
assert width(rjust(CAFE_COMBINING, 8)) == 8
4044
assert width(rjust(EMOJI_FAMILY, 6)) == 6
4145

4246

4347
def test_center():
44-
assert center('hi', 6) == ' hi '
45-
assert center('hi', 5) == ' hi '
46-
assert center('', 4) == ' '
47-
assert center('hello', 3) == 'hello'
48-
assert center('hello', 5) == 'hello'
48+
# our center() matches standard python center() for ascii
49+
assert center('hi', 6) == ' hi ' == str.center('hi', 6)
50+
assert center('hi', 5) == ' hi ' == str.center('hi', 5)
51+
assert center('ab', 7) == ' ab ' == str.center('ab', 7)
52+
assert center('', 4) == ' ' == str.center('', 4)
53+
assert center('hello', 3) == 'hello' == str.center('hello', 3)
54+
assert center('hello', 5) == 'hello' == str.center('hello', 5)
55+
assert center('hi', 6, fillchar='-') == '--hi--' == str.center('hi', 6, '-')
56+
assert center('x', 4) == ' x ' == str.center('x', 4)
57+
# advanced capabilities
4958
assert center('\x1b[31mhi\x1b[0m', 6) == ' \x1b[31mhi\x1b[0m '
5059
assert center('\u4e2d', 6) == ' \u4e2d '
51-
assert center('hi', 6, fillchar='-') == '--hi--'
5260
assert center('hi', 6, fillchar='\u00b7') == '\u00b7\u00b7hi\u00b7\u00b7'
53-
assert center('x', 4) == ' x '
5461
assert width(center(CJK_WORD, 8)) == 8
5562
assert width(center(CAFE_COMBINING, 8)) == 8
5663
assert width(center(EMOJI_FAMILY, 6)) == 6

wcwidth/wcwidth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,8 @@ def center(
742742
else:
743743
text_width = width(text, control_codes=control_codes, ambiguous_width=ambiguous_width)
744744
total_padding = max(0, dest_width - text_width)
745-
left_pad = total_padding // 2
745+
# matching https://jazcap53.github.io/pythons-eccentric-strcenter.html
746+
left_pad = total_padding // 2 + (total_padding & dest_width & 1)
746747
right_pad = total_padding - left_pad
747748
return fillchar * left_pad + text + fillchar * right_pad
748749

0 commit comments

Comments
 (0)