bczsalba / pytermgui

Python TUI framework with mouse support, modular widget system, customizable and rapid terminal markup language and more!
https://ptg.bczsalba.com
MIT License
2.23k stars 54 forks source link

Render plot correctly #2

Closed Tbruno25 closed 2 years ago

Tbruno25 commented 2 years ago

I'm looking to create multiple plot trend lines inside of a tui.

Another library I'm utilizing produces a string such as '⠀⠀⠀⠀⢠⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⡆⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀\n⠀⠀⠀⠀⡸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢠⢳⠀⠀⠀⠀⠀⣧⠀⠀⡤⡄⠀⠀⠀⠀\n⠀⠀⠀⢀⠇⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠘⡄⠀⠀⡀⢠⢻⠀⢀⠇⡇⠀⢀⠀⠀\n⡆⠀⢀⠎⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⡜⠀⡇⠀⠀⡇⢸⢸⠀⢸⠀⢇⠀⢸⠀⠀\n⢣⠀⡸⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⢇⠀⢰⢇⢸⠈⡆⢸⠀⢸⠀⡿⡀⠀\n⢸⠀⡇⠀⠀⢸⠀⢀⠤⡆⠀⠀⠀⢸⠀⠀⢸⠀⢸⢸⢸⠀⡇⡎⠀⢸⠀⡇⡇⠀\n⠘⣼⠀⠀⠀⢸⠀⢸⠀⢱⠀⠀⠀⡎⠀⠀⢸⠀⢸⢸⢸⠀⡇⡇⠀⢸⢠⠃⡇⢀\n⠀⠇⠀⠀⠀⢸⠀⡎⠀⠘⡄⠀⢸⠀⠀⠀⠘⡄⡜⢸⢸⠀⢣⠇⠀⢸⢸⠀⡇⢸\n⠀⠀⠀⠀⠀⢸⠀⡇⠀⠀⡇⠀⡇⠀⠀⠀⠀⡇⡇⠸⡇⠀⢸⠀⠀⠀⣿⠀⢣⢸\n⠀⠀⠀⠀⠀⢸⠀⡇⠀⠀⢸⢰⠁⠀⠀⠀⠀⡇⡇⠀⡇⠀⠀⠀⠀⠀⡇⠀⢸⢸\n⠀⠀⠀⠀⠀⠸⣰⠁⠀⠀⠀⠇⠀⠀⠀⠀⠀⢳⠁⠀⠇⠀⠀⠀⠀⠀⠁⠀⢸⢸\n⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡸\n⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⡇\n⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇\n⠀⠀⠀⠀⠀⠀⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠃'

When printed from the python interpreter this formats correctly

⠀⠀⠀⠀⢠⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⡆⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⡸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢠⢳⠀⠀⠀⠀⠀⣧⠀⠀⡤⡄⠀⠀⠀⠀ ⠀⠀⠀⢀⠇⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠘⡄⠀⠀⡀⢠⢻⠀⢀⠇⡇⠀⢀⠀⠀ ⡆⠀⢀⠎⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⡜⠀⡇⠀⠀⡇⢸⢸⠀⢸⠀⢇⠀⢸⠀⠀ ⢣⠀⡸⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⢇⠀⢰⢇⢸⠈⡆⢸⠀⢸⠀⡿⡀⠀ ⢸⠀⡇⠀⠀⢸⠀⢀⠤⡆⠀⠀⠀⢸⠀⠀⢸⠀⢸⢸⢸⠀⡇⡎⠀⢸⠀⡇⡇⠀ ⠘⣼⠀⠀⠀⢸⠀⢸⠀⢱⠀⠀⠀⡎⠀⠀⢸⠀⢸⢸⢸⠀⡇⡇⠀⢸⢠⠃⡇⢀ ⠀⠇⠀⠀⠀⢸⠀⡎⠀⠘⡄⠀⢸⠀⠀⠀⠘⡄⡜⢸⢸⠀⢣⠇⠀⢸⢸⠀⡇⢸ ⠀⠀⠀⠀⠀⢸⠀⡇⠀⠀⡇⠀⡇⠀⠀⠀⠀⡇⡇⠸⡇⠀⢸⠀⠀⠀⣿⠀⢣⢸ ⠀⠀⠀⠀⠀⢸⠀⡇⠀⠀⢸⢰⠁⠀⠀⠀⠀⡇⡇⠀⡇⠀⠀⠀⠀⠀⡇⠀⢸⢸ ⠀⠀⠀⠀⠀⠸⣰⠁⠀⠀⠀⠇⠀⠀⠀⠀⠀⢳⠁⠀⠇⠀⠀⠀⠀⠀⠁⠀⢸⢸ ⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡸ ⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⡇ ⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇ ⠀⠀⠀⠀⠀⠀⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠃

However, when added to a pytermgui widget it does not render correctly.

Is there any easy way to overcome this?

Also -- thank you for sharing your library, it really is awesome.

bczsalba commented 2 years ago

Good day!

This is a known issue with the line-breaker algorithm, AFAIK it currently completely ignores \n characters. This will be fixed sometime post-1.0.0, until then you can add content line-by-line to a widget for a band-aid fix.

Screenshot 2021-09-30 at 21 04 10

All I'm doing here is unpacking a generator that goes through the plot split by linebreaks. Any unnamed argument to Container is considered a potential Widget, so the auto() method is run on it to convert it as such. A more verbose way would be this:

# `plot` is already defined above
import pytermgui as ptg

root = ptg.Container()
for line in plot.splitlines():
    root += ptg.Label(line)

root.print()

Thank you for your interest, I hope this module will be useful for you. If you create any public projects with it, you can always contact me for a shoutout in the README.

Tbruno25 commented 2 years ago

Thank you for the temporary solution!

I spent an hour experimenting and think I'm going to hold off until the api / library is a bit more mature. I look forward to revisiting this in the near future!

bczsalba commented 2 years ago

Hey @Tbruno25! I know it's been like 8 months, but there is now a break_line implementation that works with newlines, and should work for your usecase!