Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

center-formatting broken with certain escape sequences #55

Open
anarcat opened this issue Dec 4, 2017 · 5 comments · May be fixed by #60
Open

center-formatting broken with certain escape sequences #55

anarcat opened this issue Dec 4, 2017 · 5 comments · May be fixed by #60

Comments

@anarcat
Copy link

anarcat commented Dec 4, 2017

it seems that the "center" alignment can get confused if there is some escape sequences in a column.

for example, this program will generate this misaligned table:

[997]anarcat@angela:undertime$ ./undertime.py --start 7 --end 20 --date "in two weeks, at 15:00" America/Montreal US/Pacific Europe/London Australia/Melbourne
┌───────┬────────────┬───────────────┬─────────────────────┐
│  EST  │ US/Pacific │ Europe/London │ Australia/Melbourne │
├───────┼────────────┼───────────────┼─────────────────────┤
│ 00:00 │   21:00    │     05:00     │        16:00        │
│ 01:00 │   22:00    │     06:00     │        17:00        │
│ 02:00 │   23:00    │     07:00     │        18:00        │
│ 03:00 │   00:00    │     08:00     │        19:00        │
│ 04:00 │   01:00    │     09:00     │        20:00        │
│ 05:00 │   02:00    │     10:00     │        21:00        │
│ 06:00 │   03:00    │     11:00     │        22:00        │
│ 07:00 │   04:00    │     12:00     │        23:00        │
│ 08:00 │   05:00    │     13:00     │        00:00        │
│ 09:00 │   06:00    │     14:00     │        01:00        │
│ 10:00 │    07:00   │     15:00     │        02:00        │
│ 11:00 │    08:00   │     16:00     │        03:00        │
│ 12:00 │    09:00   │     17:00     │        04:00        │
│ 13:00 │    10:00   │     18:00     │        05:00        │
│ 14:00 │    11:00   │     19:00     │        06:00        │
│ 15:00 │    12:00   │     20:00     │        07:00        │
│ 16:00 │    13:00   │     21:00     │        08:00        │
│ 17:00 │    14:00   │     22:00     │        09:00        │
│ 18:00 │    15:00   │     23:00     │        10:00        │
│ 19:00 │    16:00   │     00:00     │        11:00        │
│ 20:00 │    17:00   │     01:00     │        12:00        │
│ 21:00 │    18:00   │     02:00     │        13:00        │
│ 22:00 │    19:00   │     03:00     │        14:00        │
│ 23:00 │    20:00   │     04:00     │        15:00        │
└───────┴────────────┴───────────────┴─────────────────────┘

if the colors are disabled, the column is aligned fine:

$ ./undertime.py --start 7 --end 20 --date "in two weeks, at 15:00" America/Montreal US/Pacific Europe/London Australia/Melbourne --no-colors
┌───────┬────────────┬───────────────┬─────────────────────┐
│  EST  │ US/Pacific │ Europe/London │ Australia/Melbourne │
├───────┼────────────┼───────────────┼─────────────────────┤
│ 00:00 │   21:00    │     05:00     │        16:00        │
│ 01:00 │   22:00    │     06:00     │        17:00        │
│ 02:00 │   23:00    │     07:00     │        18:00        │
│ 03:00 │   00:00    │     08:00     │        19:00        │
│ 04:00 │   01:00    │     09:00     │        20:00        │
│ 05:00 │   02:00    │     10:00     │        21:00        │
│ 06:00 │   03:00    │     11:00     │        22:00        │
│ 07:00 │   04:00    │     12:00     │        23:00        │
│ 08:00 │   05:00    │     13:00     │        00:00        │
│ 09:00 │   06:00    │     14:00     │        01:00        │
│ 10:00 │   07:00    │     15:00     │        02:00        │
│ 11:00 │   08:00    │     16:00     │        03:00        │
│ 12:00 │   09:00    │     17:00     │        04:00        │
│ 13:00 │   10:00    │     18:00     │        05:00        │
│ 14:00 │   11:00    │     19:00     │        06:00        │
│ 15:00 │   12:00    │     20:00     │        07:00        │
│ 16:00 │   13:00    │     21:00     │        08:00        │
│ 17:00 │   14:00    │     22:00     │        09:00        │
│ 18:00 │   15:00    │     23:00     │        10:00        │
│ 19:00 │   16:00    │     00:00     │        11:00        │
│ 20:00 │   17:00    │     01:00     │        12:00        │
│ 21:00 │   18:00    │     02:00     │        13:00        │
│ 22:00 │   19:00    │     03:00     │        14:00        │
│ 23:00 │   20:00    │     04:00     │        15:00        │
└───────┴────────────┴───────────────┴─────────────────────┘

not sure what's going on here but it sure seems like the escape sequence calculations go wrong at some point.

PS: I understand this is not exactly a minimal test case but I'm a little tired right now and would rather document this issue at this point. Let me know if you need help minimizing the code or understanding how the table is created.

@mgedmin
Copy link

mgedmin commented Mar 29, 2018

The correlation between colors and alignment becomes more apparent when you can see the colors:

ekrano nuotrauka is 2018-03-29 11-12-47

@mgedmin
Copy link

mgedmin commented Mar 29, 2018

Here's a failing unit test:

diff --git a/tests/test_width_and_alignment/test_align_and_pad_cell.py b/tests/test_width_and_alignment/test_align_and_pad_cell.py
index e0a928e..aa8bd27 100644
--- a/tests/test_width_and_alignment/test_align_and_pad_cell.py
+++ b/tests/test_width_and_alignment/test_align_and_pad_cell.py
@@ -74,6 +74,9 @@ from terminaltables.width_and_alignment import align_and_pad_cell
     ('蓝色', 'center', 6, [' 蓝色 ']),
     (u'שלום', 'center', 6, [u' \u05e9\u05dc\u05d5\u05dd ']),
     (u'معرب', 'center', 6, [u' \u0645\u0639\u0631\u0628 ']),
+
+    ('00:00', 'center', 8, ['  00:00 ']),
+    (colored('00:00', 'blue'), 'center', 8, ['  \x1b[34m00:00\x1b[0m ']),
 ])
 def test_width(string, align, width, expected):
     """Test width and horizontal alignment.

I don't know which padding you prefer when centering requires left and right padding to be different, but it should be the same for colored and non-colored text.

mgedmin added a commit to mgedmin/terminaltables that referenced this issue Mar 29, 2018
@mgedmin mgedmin linked a pull request Mar 29, 2018 that will close this issue
@KwadroNaut
Copy link

Nice to see there's a Pull Request to fix this. Anything you need, @Robpol86 to integrate this?

@anarcat
Copy link
Author

anarcat commented Jul 24, 2019

@KwadroNaut i suspect the maintainer here might need a little help, see #70

@anarcat
Copy link
Author

anarcat commented Nov 21, 2019

FWIW, i ended up switching to tabulate which doesn't have that problem and supports more output formats.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants