visidata icon indicating copy to clipboard operation
visidata copied to clipboard

[colorizers] custom colorizers not shown in sheet context

Open geekscrapy opened this issue 2 years ago • 5 comments

Small description if a user (mainly a dev!) does =sheet.colorizers a list of colorizers is shown in the next column, however this is not a complete list that is being currently applied to the sheet.

Expected result To show all colorizers relevant to the sheet

Actual result with screenshot Only the following default colorizers are shown image

Repro Add a new colorizer with: Ctrl+x vd.sheet.addColorizer(RowColorizer(1000, 'green',lambda s,c,r,v: True))

Additional context 2.7

geekscrapy avatar Nov 30 '21 14:11 geekscrapy

I noticed this as I was creating a plugin that adds/activates colorizers via a long command, and wanted to add a long command to remove the colorizers, but I can't seem to find them!

geekscrapy avatar Nov 30 '21 21:11 geekscrapy

This is tricky, because colorizers is the list of colorizers at the class-level, and _colorizers is the list at the object-level. And note that colorizers from parent classes also apply. You can get the computed full list of colorizers from the (undocumented) allColorizers property on the sheet.

saulpw avatar Nov 30 '21 22:11 saulpw

For future ref: Did it this way as sheet apparently doesn't have a property of allColorizers [vd.sheet._colorizers.remove(c) for c in vd.sheet.allColorizers if c.coloropt=='green']

geekscrapy avatar Dec 01 '21 17:12 geekscrapy

Hmm, just looking at this a little more:

So when adding colorizers at the class-level, and the object is initiated they are exposed via sheet.colorizers (as expected) and also, sheet.allColorizers. However when added at runtime with sheet.addColorizer, they get added to sheet._colorizers.

So to account for this, the colorizer toggle off command to allow a user to remove the colorizers needs to look like this:

    for c in sheet.allColorizers:
        if 'custom_colorizer' in str(c.func):
            # on sheet load colorizers are placed into `sheet.colorizers`
            try: sheet.colorizers.remove(c)
            except ValueError: pass
            # when `sheet.addColorizer(c)` used, it is placed into `sheet._colorizers`
            try: sheet.removeColorizer(c)
            except ValueError: pass

From your comment above, this seems to be expected, but shouldn't all colorizers reside in sheet._colorizers to allow sheet.add/removeColorizer to work consistently?

geekscrapy avatar Jan 10 '22 12:01 geekscrapy

You might be right, @geekscrapy, we'll look at this.

saulpw avatar Mar 02 '22 06:03 saulpw

Okay, so I made a fix so that all class colorizers are set in sheet._colorizers when the Sheet is created, so addColorizer will just add to this list and then resort it. So now if you look at sheet._colorizers in vd it will be an accurate list, and removeColorizer should work consistently.

saulpw avatar Oct 09 '23 22:10 saulpw