Robpol86 / terminaltables

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

center-formatting broken with certain escape sequences #55

Open anarcat opened 6 years ago

anarcat commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.

KwadroNaut commented 6 years ago

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

anarcat commented 5 years ago

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

anarcat commented 4 years ago

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