Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make custom formatting in Table work properly with ANSI colors #7041

Open
astrofrog opened this issue Dec 29, 2017 · 1 comment
Open

Make custom formatting in Table work properly with ANSI colors #7041

astrofrog opened this issue Dec 29, 2017 · 1 comment

Comments

@astrofrog
Copy link
Member

astrofrog commented Dec 29, 2017

If using ANSI colors in a table formatting function, the columns are not padded correctly due to the fact that the ANSI color commands are included in the length of the string:

In [1]: from termcolor import colored

In [2]: def fmt(x):
   ...:     if x > 0:
   ...:         return colored(str(x), 'green')
   ...:     else:
   ...:         return colored(str(x), 'red')
   ...:     

In [4]: from astropy.table import Table
t =
In [5]: t = Table()

In [6]: t['a'] = [-1, 1, 0, 3, -3]

In [7]: t['b'] = [-1, 1, 0, 3, -3]

In [8]: t
Out[8]: 
<Table length=5>
  a     b  
int64 int64
----- -----
   -1    -1
    1     1
    0     0
    3     3
   -3    -3

In [9]: t['a'].format = fmt

In [10]: t['b'].format = fmt

In [11]: t
Out[11]: 
<Table length=5>
     a           b     
   int64       int64   
----------- -----------
-1 -1
 1  1
 0  0
 3  3
-3 -3

The color works correctly though:

screen shot 2017-12-29 at 16 48 25

It would be nice if we could do the right thing™️ here though and auto-strip the ANSI color commands from strings when figuring out the width of the columns. Feel free to close this if it would be much too difficult though.

@taldcroft
Copy link
Member

Is there any kind of fast library that can strip out ANSI color commands or render ANSI text as plain ASCII in order to compute length? I think that without a pre-existing library to do the job (fast) this would be a bit of work and would impact performance.

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

No branches or pull requests

2 participants