Robpol86 / terminaltables

Project no longer maintained.
https://github.com/matthewdeanmartin/terminaltables
MIT License
689 stars 70 forks source link

Pad centered colored text consistently #60

Open mgedmin opened 6 years ago

mgedmin commented 6 years ago

Fixes #55.

anarcat commented 6 years ago

hmmm... this makes other tests fail in the suite... so yeah, maybe a policy decision to make here, but at least the tests should be fixed before this is merged. :)

travis says this:

    def test_odd_width_height_pad_space(string, align, expected):
        """Test odd number width, height, padding, and dots for whitespaces.

        :param str string: String to test.
        :param str align: Alignment in any dimension but one at a time.
        :param list expected: Expected output string.
        """
        actual = align_and_pad_cell(string, (align,), (5, 3), (1, 1, 1, 1), '.')
>       assert actual == expected
E       AssertionError: assert ['.......', '....', '.......'] == ['.......', '.....', '.......']
E         At index 1 diff: '.test..' != '..test.'
E         Use -v to get the full diff
tests/test_width_and_alignment/test_align_and_pad_cell.py:205: AssertionError
mgedmin commented 6 years ago

Oops! I thought I ran tox before pushing, but apparently I forgot.

mgedmin commented 6 years ago

How does Python's str.center() decide where to put excessive padding? It seems inconsistent:

>>> 'test'.center(7, '.')  # extra padding on the left
'..test.'
>>> '00:00'.center(8, '.')  # extra padding on the right!
'.00:00..'

I wanted to be consistent with str.center() and thought it would always put the extra padding on the right. I was wrong.

mgedmin commented 6 years ago

CPython does this:

    marg = width - PyUnicode_GET_LENGTH(self);
    left = marg / 2 + (marg & width & 1);

    return pad(self, left, marg - left, fillchar);

The extra padding goes to the left when both margin and width are odd.

codecov-io commented 6 years ago

Codecov Report

Merging #60 into master will decrease coverage by <.01%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #60      +/-   ##
==========================================
- Coverage   99.57%   99.56%   -0.01%     
==========================================
  Files           8        7       -1     
  Lines         466      462       -4     
  Branches       76       76              
==========================================
- Hits          464      460       -4     
  Misses          1        1              
  Partials        1        1
Impacted Files Coverage Δ
terminaltables/width_and_alignment.py 100% <100%> (ø) :arrow_up:
tests/test_all_tables_e2e/__init__.py

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update ad8f46e...c00687f. Read the comment docs.

faudebert commented 5 years ago

I can confirm I have better results regarding centering with this PR merged. Would appreciate a release including it.

anarcat commented 5 years ago

ping - any reason why this isn't merged yet? see also #70...