-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Make term-img easier to unit test #11
Comments
Thank you very much, I really appreciate the suggestion! 😃 The main reason why I designed the constructor as such was to simplify and separate functionality. I wanted the class constructor to be as simple as reasonably possible. To explain what I mean by simplification and separation... I know adding just two more parameters to I feel the constructor should only provide an interface to the basic/necessary functionalities and then extra things should be handled by other specialized interfaces via methods or properties... I guess that's why I mentioned "advanced" in the description of The case of unit testing seems to me as more of a special case, which would call for more than basic functionalities. Instead of... ... what do you think about this? block_image = term_image.TermImage(pil_image)
block_image.set_size(
width=scaled_width,
height=scaled_height,
check_height=False,
check_width=False,
)
string_image = str(block_image)
pil_image.close() |
Also, thanks for the Additional context! 😊 |
About not validating the size at First of all, it introduces an inconsistency in that other means of setting the render size validate it immediately. Also, for instance, if it isn't known ahead if a given size will be valid and various "candidate" sizes are being tried to see which of them are valid... delaying the validation of the given size could cause a problem when the size is actually valid because the image would already be drawn which might not be the user's intent. |
Going through your code again, I noticed
Also, briefly going through If my inferences are correct, then it seems to me as though what you need exactly is the maxsize parameter of Here's what I propose: block_image = term_image.TermImage(pil_image)
block_image.set_size(maxsize=(max_width, max_height))
string_image = str(block_image)
pil_image.close() The best fitting size is automatically calculated by Please, try this and let me know what the result is. Thanks once again. |
Firstly, thank you so much for all the reponses here. Basically: I agree with your latest advice, More detailsI see what you say about keeping
I think the unit tests are where this "problem" showed up, but in reality this is a problem of being able to carefully control your image. Maybe—like me—you don't want to print it directly to the terminal, and want to output to text to do something with it. You later showed some workarounds to deal with this (
I think this part I'm still not 100% sold on. I think it's still really suprising to have the size be validated if it's not clear that the user is even interested in printing to the terminal. Maybe they wish to save it to a file or—like me—want to export it to a string to process it further, and don't really care about the terminal size. Intuitively to me it would make more sense to validate when the user actually tries to render to a terminal, because they definitley care at that point. To be clear this isn't a big deal for me, and you showed me some workarounds that work perfectly for my case. But as a user—who read the docs poorly—it was something that threw me off.
This is perfect. You're totally right, I can get rid of both |
Ended up implimenting what we discussed. As you can see, a lot of code removed—so (again) thanks a lot for the guidance! |
You're very welcome! 😃
True 🤔... For some reason, I never saw it from this perspective until this statement 🥲. Though, the terminal size will still be used when setting automatic size i.e via
I know that pain, when you realize there was actually one function/method or function parameter that could do something you've been racking your brains over 🥲... happens to everyone once in a while. Thanks for everything! |
You might also wanna watch out for some upcoming features...
I guess they might be be useful for your project. The first three should be coming with 0.3.x and the last one, probably later (though, definitely coming soon). Also, I'm planning to take the rendering performance up some notches 😉. |
I think I'll leave this open till I implement the changes to |
I noticed you're using f"{block_image:{max_width}.{max_height}}" to always have the images centered within the available space i.e See here for more. |
You'd be right.
Really looking forward to seeing this developing. Will be following closely. Of course, no rush! Really happy already with how the renders turned out.
Yeah I saw that! I don't see this leveraged a lot so I thought this was an interesting API design! |
Thanks 😃 I'll try to make sure development moves quickly. |
@paw-lu Thank you very much. I believe this issue has been fixed and the changes will be rolling out with version 0.2.0 by the beginning of April. |
Is your feature request related to a problem? Please describe.
I'm having trouble actually using term-img when running unit tests, because term_img.exceptions.InvalidSize is raised when
TermImage
is initialized.Describe the solution you'd like
I'm not sure what's best here, but one quick solution would be add
check_width
andcheck_height
args toTermImage
, so that they can be passed toset_size
herehttps://github.com/AnonymouX47/term-img/blob/44182ee63d0f0da281c9c57313e81dca1f3abc14/term_img/image.py#L91
Overall, I'm also wondering if the size shouldn't be validated at
__init__
? Might be more appropriate to evaluate atdraw()
time only.Describe alternatives you've considered
I could just mock
get_terminal_size
to give me the size I'm trying to emulate in my tests as a fallback, but wondering if the library itself could use some way to control this.Additional context
Just wanted to say that I've really enjoyed using this library, and want to make it the default image renderer in a cli I'm working on. Appreciate the work here!
The text was updated successfully, but these errors were encountered: