jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.31k stars 148 forks source link

Is max_width word-wrapping configurable? #258

Open ajgringo619 opened 10 months ago

ajgringo619 commented 10 months ago

In my use-case, the column in question contains package names, which sometimes have hyphens or multiple hyphens. When I set a max_width on the column, some of the package names get split up:

| bluedevil breeze breeze-gtk eos-bash-shared glib2 kactivitymanagerd kde-cli-     |
| tools kde-gtk-config kdecoration kdeplasma-addons khotkeys kinfocenter kmenuedit | 
| kpipewire kscreen kscreenlocker ksystemstats kwallet-pam kwin layer-shell-qt     | 
| lib32-glib2 libarchive libkscreen libksysguard libwebp milou oxygen-sounds       | 
| plasma-desktop plasma-disks plasma-integration plasma-nm plasma-pa plasma-       | 
| workspace polkit-kde-agent powerdevil python-gobject qt5-webengine sddm-kcm      |
| sqlite systemsettings vivaldi                                                    |

Is there a way to exclude hyphenated words from being wrapped?

hugovk commented 10 months ago

No, but it could be done by setting break_on_hyphens=False here:

https://github.com/jazzband/prettytable/blob/1c0aeee4d3109f38dff8926111b4ca168d059d90/src/prettytable/prettytable.py#L1957

>>> import textwrap
>>> line = "bluedevil breeze breeze-gtk eos-bash-shared glib2 kactivitymanagerd kde-cli-tools"
>>> textwrap.fill(line, 40)
'bluedevil breeze breeze-gtk eos-bash-\nshared glib2 kactivitymanagerd kde-cli-\ntools'
>>> textwrap.fill(line, 40, break_on_hyphens=False)
'bluedevil breeze breeze-gtk\neos-bash-shared glib2 kactivitymanagerd\nkde-cli-tools'
>>>

Docs: https://docs.python.org/3/library/textwrap.html

Would you like to put a PR together to make it configurable?

Maybe the thing to do would have a dictionary that can be used to set all the other textwrap.fill parameters?

ajgringo619 commented 10 months ago

I've got the time, just not sure where to begin. Can you point me in the right direction? Never contributed directly to a Github project before.

hugovk commented 10 months ago

Sure!

The high level is:

You start off by forking this repo, there's a Fork button on the main page: https://github.com/jazzband/prettytable

Then clone it to your computer, for example: git clone https://github.com/ajgringo619/PrettyTable

Docs: https://docs.github.com/en/get-started/quickstart/fork-a-repo

The comes the coding, add what's needed, with tests. You can test locally via tox, pip install -U tox, then tox -e py311 to test with Python 3.11 for example.

Also it's a good idea to enable GitHub Actions on your fork - https://github.com/ajgringo619/PrettyTable/actions - then it'll run the tests when you push.

When you're ready, you create a "pull request" to submit it back here. Then we can review it, update it, then merge.

Docs: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request

Feel free to ask more details about any of these steps!

ajgringo619 commented 10 months ago

Thanks for the detailed info, although I'm sure I'mm be asking a lot more questions. First one: what's the oldest version of Python I should be testing on?

hugovk commented 10 months ago

We follow the same as the upstream supported CPython versions, so Python 3.8.

You don't necessarily need to install all the versions though, because the CI will test 3.8-3.12 on Ubuntu, Windows and macOS.

ajgringo619 commented 10 months ago

I've already got versions 3.9 through 3.11 installed, so one more won't be a problem. Thanks again for all the info!

anibal2j commented 3 months ago

Is this fix going to fix the fact that column headers should also wrap? I have column headers that are dynamic, and I need them to wrap - otherwise the table looks terrible. SHould I open a new issue for that?

ajgringo619 commented 2 months ago

Can't believe it's been 7 months since I had "time" to work on this; I deleted the fork as it was just sitting there collecting dust. If I can truly commit to this in the future, I will post again with actual progress.

av-guy commented 2 months ago

@ajgringo619 I can take care of this one if you would like.

ajgringo619 commented 2 months ago

@ajgringo619 I can take care of this one if you would like.

Thanks a bunch. I'll be happy to test whatever you come up with.