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

Setting maximum column width #21

Closed
darrencl opened this issue Oct 15, 2019 · 15 comments
Closed

Setting maximum column width #21

darrencl opened this issue Oct 15, 2019 · 15 comments

Comments

@darrencl
Copy link

darrencl commented Oct 15, 2019

Hi @ronisbr ,

I am just wondering if it is possible to setting maximum column width (max_colwidth) with the current version? If not, I think it would be great to have one. While screen_size option takes the maximum character of the printed table itself, this option can set the maximum character in a column. This is similar on what pandas.options has by setting in display.max_colwidth.

Another relevant feature, which maybe useful, is user can specify the behavior when the column exceeds max_colwidth. With this, the user will be able to choose between crop (... on remaining characters), or split with new line, that is split the remaining character into new line (maybe split in closest whitespace). The latter is now possible to do with manual work on adding \n in string with linebreaks=true.

These options are useful for particularly printing long text data in tabular form.

Thanks

@ronisbr
Copy link
Owner

ronisbr commented Oct 21, 2019

Hi @darrencl ,

Sorry for the late response, it was a very busy week.

I really like your suggestion. I will split this into two parts: it is easy to create the cropping based on the maximum column width. However, the automatic line break needs more thinking. We can split in the closest whitespace (adding \n), but this will lead to problems if a word is bigger than the column size. It should be "easy" to create an algorithm that deals with all of it, but I will need sometime to think about it.

@darrencl
Copy link
Author

Hi @ronisbr

No worries at all! Thanks for getting back to this.

I agree on splitting this into 2 parts given the latter feature, I think, is more of 'nice to have'.

Yeah, I was thinking the same on splitting the string into closest whitespace and maybe adding spaces if the number of character is less than the max (with this, every line will have same number of characters). If I understand the default behavior correctly, this will ensure every columns have the same width. In regards to the issues on length(word) > max_colwidth, I think we have 2 options: throwing an Exception (maybe StringIndexError?), or warn user and auto-update max_colwidth = length(word_exceeding). I kind of leaning towards the latter as it sounds more robust and more flexible.

ronisbr added a commit that referenced this issue Oct 23, 2019
The keyword `columns_width` can be used to specify the desired
width for each column.

This addresses the first part of issue #21.
@ronisbr
Copy link
Owner

ronisbr commented Oct 23, 2019

Hi @darrencl ,

If you use master, then you can use the keyword column_width to select the column size of each column. Can you test please?

julia> model = [1.123456789 for i = 1:8, j = 1:10];

julia> pretty_table(model; columns_width = [1, 0, 2, 3, 4, 5, 6, 7, 8, 9])
┌───┬─────────────┬────┬─────┬──────┬───────┬────────┬─────────┬──────────┬───────────┐
│  │      Col. 2 │ C │ Co │ Col │ Col. │ Col. 7 │  Col. 8 │   Col. 9 │   Col. 10 │
├───┼─────────────┼────┼─────┼──────┼───────┼────────┼─────────┼──────────┼───────────┤
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
│ 1.12345678911.1.11.121.1231.12341.123451.123456 │
└───┴─────────────┴────┴─────┴──────┴───────┴────────┴─────────┴──────────┴───────────┘

@darrencl
Copy link
Author

Hi @ronisbr ,

Sorry for the late reply, was busy yesterday.
Just tested it and working as expected! Very nice!

I think an option to input column_width as int can be added as opposed to only restricting it to be a list. This way, the users can conveniently use int to set the same column_width across the table.

@ronisbr
Copy link
Owner

ronisbr commented Oct 24, 2019

Nice! The idea of an Int is perfect! I will do that :)

@ronisbr
Copy link
Owner

ronisbr commented Oct 25, 2019

What do you think about removing the possibility to be nothing and by default I make this parameter equal 0. This will lead to all columns to have their size automatically computed and we can also specify a fixed size for all of them.

@darrencl
Copy link
Author

darrencl commented Oct 25, 2019

I think it is not a big deal since the automatic size is the default behaviour. :)
I assume the users will only specify the column_width when they need it anyway, i.e. > 0, so yeah, that idea sounds good to me!

ronisbr added a commit that referenced this issue Oct 28, 2019
If `columns_width` is an integer, then it will be the size of all
columns.

This addresses part of issue #21.
@ronisbr
Copy link
Owner

ronisbr commented Oct 28, 2019

Done!

julia> model = [1.123456789 for i = 1:8, j = 1:10];

julia> pretty_table(model; columns_width = 5)
┌───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┐
│ Col. │ Col. │ Col. │ Col. │ Col. │ Col. │ Col. │ Col. │ Col. │ Col. │
├───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1.121.121.121.121.121.121.121.121.121.12 │
│ 1.121.121.121.121.121.121.121.121.121.12 │
│ 1.121.121.121.121.121.121.121.121.121.12 │
│ 1.121.121.121.121.121.121.121.121.121.12 │
│ 1.121.121.121.121.121.121.121.121.121.12 │
│ 1.121.121.121.121.121.121.121.121.121.12 │
│ 1.121.121.121.121.121.121.121.121.121.12 │
│ 1.121.121.121.121.121.121.121.121.121.12 │
└───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┘

@ronisbr ronisbr closed this as completed in 362673f Nov 8, 2019
@ronisbr
Copy link
Owner

ronisbr commented Nov 8, 2019

Hi @darrencl !

Finally, I could finish the code to auto wrap the text on spaces. Can you please test to me? Is it available in master. The option is autowrap, but it only works if linebreaks is true and the column width is fixed. This was somewhat complicated and I am expecting regressions and bugs.

julia> pretty_table( ["this is a very very long line\nand another very very very long line" 10 20;
                      "Here is another line\nbut not so long" 30 40]; autowrap = true, linebreaks = true, columns_width = [10,-1,-1], hlines = 1:2 )
┌────────────┬────────┬────────┐
│     Col. 1 │ Col. 2 │ Col. 3 │
├────────────┼────────┼────────┤
│  this is a │     10 │     20 │
│  very very │        │        │
│       long │        │        │
│       line │        │        │
│        and │        │        │
│    another │        │        │
│  very very │        │        │
│  very long │        │        │
│       line │        │        │
├────────────┼────────┼────────┤
│    Here is │     30 │     40 │
│    another │        │        │
│       line │        │        │
│    but not │        │        │
│         so │        │        │
│       long │        │        │
└────────────┴────────┴────────┘

julia> pretty_table( ["this is a very very long line\nand another very very very long line" 10 20;
                      "Here is another line\nbut not so long" 30 40]; autowrap = true, linebreaks = true, columns_width = [10,-1,-1], hlines = 1:2, alignment = :c )
┌────────────┬────────┬────────┐
│   Col. 1   │ Col. 2 │ Col. 3 │
├────────────┼────────┼────────┤
│ this is a  │   10   │   20   │
│ very very  │        │        │
│    long    │        │        │
│    line    │        │        │
│    and     │        │        │
│  another   │        │        │
│ very very  │        │        │
│ very long  │        │        │
│    line    │        │        │
├────────────┼────────┼────────┤
│  Here is   │   30   │   40   │
│  another   │        │        │
│    line    │        │        │
│  but not   │        │        │
│     so     │        │        │
│    long    │        │        │
└────────────┴────────┴────────┘

@ronisbr
Copy link
Owner

ronisbr commented Nov 8, 2019

In fact I just found a bug :D

@ronisbr
Copy link
Owner

ronisbr commented Nov 8, 2019

Now its fine :)

julia> pretty_table( ["this is a very very long line\nand another very very very long line" 10 20;
                      "Here is another line\nbut not so long" 30 40]; autowrap = true, linebreaks = true, columns_width = [10,-1,-1], hlines = 1:2 )
┌────────────┬────────┬────────┐
│     Col. 1 │ Col. 2 │ Col. 3 │
├────────────┼────────┼────────┤
│  this is a │     10 │     20 │
│  very very │        │        │
│  long line │        │        │
│        and │        │        │
│    another │        │        │
│  very very │        │        │
│  very long │        │        │
│       line │        │        │
├────────────┼────────┼────────┤
│    Here is │     30 │     40 │
│    another │        │        │
│       line │        │        │
│    but not │        │        │
│    so long │        │        │
└────────────┴────────┴────────┘

julia> pretty_table( ["this is a very very long line\nand another very very very long line" 10 20;
                      "Here is another line\nbut not so long" 30 40]; autowrap = true, linebreaks = true, columns_width = [10,-1,-1], hlines = 1:2, alignment = :c )
┌────────────┬────────┬────────┐
│   Col. 1   │ Col. 2 │ Col. 3 │
├────────────┼────────┼────────┤
│ this is a  │   10   │   20   │
│ very very  │        │        │
│ long line  │        │        │
│    and     │        │        │
│  another   │        │        │
│ very very  │        │        │
│ very long  │        │        │
│    line    │        │        │
├────────────┼────────┼────────┤
│  Here is   │   30   │   40   │
│  another   │        │        │
│    line    │        │        │
│  but not   │        │        │
│  so long   │        │        │
└────────────┴────────┴────────┘

@darrencl
Copy link
Author

@ronisbr Very nice! Thanks for the update!

@jo-fleck
Copy link

@ronisbr Thanks for this great package!

Is column width control also supported for the tex backend? (I have in mind fixed width column-types such as shown here.)

@ronisbr
Copy link
Owner

ronisbr commented May 21, 2020

Hi @jo-fleck !

Thanks! I am glad it is being useful :)

Unfortunately the LaTeX backend does not support yet fixed columns. It is a very simple thing to implement, I just need to think about a good API.

@jo-fleck
Copy link

Thanks! I'll be happy to test and give feedback.

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

No branches or pull requests

3 participants