facelessuser / coloraide

A library to aid in using colors
https://facelessuser.github.io/coloraide
MIT License
195 stars 12 forks source link

Docs: Extract color previews from unassigned Color objects in match/case #274

Closed facelessuser closed 1 year ago

facelessuser commented 1 year ago

We just added color extraction from unassigned objects for previews:

if True:
    # We will now get a color preview for this
   # where you had to explicitly print prior to this.
    Color('red')

We added this for if/elif/else, for/else, white/else, with, and try/catch/else/finally. Unfortunately, we have not yet added it for match/case.

Match case is a bit more complicated and has multiple different types of patterns, some can generate * variables, and it is just generally more difficult to extract the color results by executing line by line in AST. This is because normally this is run via exec because you can't run the start of the block by itself, you need the whole context. We work around this for other blocks by handling the block (evaluating the conditions and applying the block logic ourselves) and then running the block content line by line. To do this in match/case, we need to figure out a way to execute and match the conditions from the AST so we can iterate the case blocks line by line. This may be easier than I think, or we'll have to implement all the pattern match cases individually, covering all their quirks to then know which case block we need to evaluate.

Now, this doesn't mean match/case is currently broken, we just don't have fine enough resolution to extract color previews when a color is under the scope of the match/case block. You can still print:

Screenshot 2023-02-24 at 9 57 01 AM

You can store the objects and display them later:

Screenshot 2023-02-24 at 10 08 12 AM

We just can't get at those objects under that scope yet:

Screenshot 2023-02-24 at 9 57 16 AM
facelessuser commented 1 year ago

I actually was able to get it to work. It took a bit, but all the pieces kind of build off each other. Now we can get color previews from inside match/case. That handles all the major blocks. We are skipping Async blocks.

This will make it more natural to play with the lib live.

Screenshot 2023-02-24 at 10 41 52 PM