AnonymouX47 / term-image

Display images in the terminal with python
https://term-image.readthedocs.io
MIT License
206 stars 9 forks source link

("pack", UrwidImage(...)) causes the UrwidImage to not render #77

Closed danschwarz closed 1 year ago

danschwarz commented 1 year ago

I have an urwid.Columns widget in which I am placing text in some columns, an UrwidImage in another. ("pack", urwid.Text("SomeTextHere")) works fine. ("pack",BoxAdapter(UrwidImage(...)),1) causes the UrwidImage to disappear on render. I think it may be calculating zero-width. Thoughts?

AnonymouX47 commented 1 year ago

Yes, UrwidImage isn't a fixed widget. As a matter of fact, almost all built-in urwid widgets aren't.

The reason being, I couldn't think of any way to compute a valid image size without maxcols or maxrows i.e an empty size tuple, and to be fair, it doesn't make much sense anyways.

Use Columns([..., UrwidImage(...), ...]) instead i.e as a box/flow widget with weighted width within the Columns widget. Or with given with i.e Columns([... , (width, UrwidImage(...)), ...]).

EDIT: Note that ("pack", widget) is quite different for a Pile widget. In this case, widget is treated as a flow widget, which UrwidImage is.

AnonymouX47 commented 1 year ago

With Columns(<list of UrwidImage widgets>, 1):

Screenshot_2023-02-27_00-37-58

With

Columns([("pack", image_w) for image_w in <list of UrwidImage widgets>], 1)

Only the first widget is rendered and fills the entire Columns widget. So, I guess that's what you're experiencing.

AnonymouX47 commented 1 year ago

This is totally intended behavior. See:

https://github.com/AnonymouX47/term-image/blob/10ba5add52d33f4f21d9462804b4a2cb9333cbd1/src/term_image/widget/urwid.py#L160

Though, I guess i need to change "packed" to "fixed". That was a mistake.

AnonymouX47 commented 1 year ago

I'm really sorry, I should've asked first. Any ideas on how to compute the size for a fixed image widget?

danschwarz commented 1 year ago

Only the first widget is rendered and fills the entire Columns widget. So, I guess that's what you're experiencing.

That is what I'm experiencing. After I wrote up the issue, I realized that it wasn't a zero width situation, but a too-wide situation preventing the display of the image.

I'm able to work around this issue by simply not attempting to pack the UrwidImage. As long as I follow it with another column of sufficient weight, the UrwidImage gets displayed at minimum width, which is what I am after.

Any ideas on how to compute the size for a fixed image widget? Given that you do centering and (optionally) upscaling, I don't think it is possible to know the size outside of render()

danschwarz commented 1 year ago

So, fine to close this out as wontfix. Thanks.

AnonymouX47 commented 1 year ago

You're welcome.

Thanks for all your reports, suggestions and help.