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.21k stars 54 forks source link

[BUG] size has no effect when creating a terminal and recording? #68

Closed pawamoy closed 2 years ago

pawamoy commented 2 years ago

Describe the bug See #67 (same code):

from io import StringIO
import pytermgui as ptg

code = """
while True:
    if condition:
        print("Hello")
    else:
        print("Goodbye")

    input()
"""

with ptg.Terminal(stream=StringIO(), size=(20, 20)).record() as recorder:
    recorder.write(ptg.tim.parse(ptg.highlight_python(code)))
    print(recorder.export_svg(inline_styles=True))

I can't seem to get the desired size for the generated SVG. It seems it uses my actual terminal size (vertical screen) rather than what I'm providing using the size parameter (zoomed out to see the full size):

pytermgui2

Expected behavior Respect the size value of the terminal when exporting a record to SVG.

System information

PyTermGUI version 6.2.1

System details:
    Python version: 3.8.13
    $TERM:          xterm-256color
    $COLORTERM:     truecolor
    Color support:  ColorSystem.TRUE
    OS Platform:    Linux-5.17.9-arch1-1-x86_64-with-glibc2.35
bczsalba commented 2 years ago

There is a bit of weirdness with how Terminals are handled in the library, and it will be focused on in the new documentations. Basically, there exists a global terminal that basically everything uses to write/read information to, and it can be set using the ptg.set_global_terminal function.

This shouldn't affect your use case, but it seems it does, since changing it to this:

terminal = ptg.Terminal(stream=StringIO(), size=(20, 20))
ptg.set_global_terminal(terminal)

with terminal.record() as recorder:
    recorder.write(ptg.tim.parse(ptg.highlight_python(code)))
    print(recorder.export_svg(inline_styles=True))

Seems to work perfectly well. Will look into it further, but using it this way should help for now.

Sidenote: Dimensions of 20x20 are very odd looking, so you probably wanna adjust them. I haven't been able to figure out a way to clip overflow in SVGs yet, so it might look a bit broken :(