Update configurable symbols

- Support additional properties.
- Cached values.
This commit is contained in:
awsr
2025-12-28 16:55:55 -08:00
parent 0a20b58170
commit e1a4331a21
2 changed files with 39 additions and 18 deletions
+10 -10
View File
@@ -391,27 +391,27 @@ def create_html(search_text, sort_column):
if ext.get('status', None) is None or type(ext['status']) == str: # old format
ext['status'] = 0
if ext['url'] is None or ext['url'] == '':
status = f"<div style='cursor:help;width:1em;' title='Local'>{ui_symbols.svg_bullet.color('#00C0FD')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Local'>{ui_symbols.svg_bullet.style('#00C0FD')}</div>"
elif ext['status'] > 0:
if ext['status'] == 1:
status = f"<div style='cursor:help;width:1em;' title='Verified'>{ui_symbols.svg_bullet.color('#00FD9C')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Verified'>{ui_symbols.svg_bullet.style('#00FD9C')}</div>"
elif ext['status'] == 2:
status = f"<div style='cursor:help;width:1em;' title='Supported only with backend: Original'>{ui_symbols.svg_bullet.color('#FFC300')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Supported only with backend: Original'>{ui_symbols.svg_bullet.style('#FFC300')}</div>"
elif ext['status'] == 3:
status = f"<div style='cursor:help;width:1em;' title='Supported only with backend: Diffusers'>{ui_symbols.svg_bullet.color('#FFC300')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Supported only with backend: Diffusers'>{ui_symbols.svg_bullet.style('#FFC300')}</div>"
elif ext['status'] == 4:
status = f"<div style='cursor:help;width:1em;' title=\"{html.escape(ext.get('note', 'custom value'))}\">{ui_symbols.svg_bullet.color('#4E22FF')}</div>"
status = f"<div style='cursor:help;width:1em;' title=\"{html.escape(ext.get('note', 'custom value'))}\">{ui_symbols.svg_bullet.style('#4E22FF')}</div>"
elif ext['status'] == 5:
status = f"<div style='cursor:help;width:1em;' title='Not supported'>{ui_symbols.svg_bullet.color('#CE0000')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Not supported'>{ui_symbols.svg_bullet.style('#CE0000')}</div>"
elif ext['status'] == 6:
status = f"<div style='cursor:help;width:1em;' title='Just discovered'>{ui_symbols.svg_bullet.color('#AEAEAE')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Just discovered'>{ui_symbols.svg_bullet.style('#AEAEAE')}</div>"
else:
status = f"<div style='cursor:help;width:1em;' title='Unknown status'>{ui_symbols.svg_bullet.color('#008EBC')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Unknown status'>{ui_symbols.svg_bullet.style('#008EBC')}</div>"
else:
if updated < datetime.now(timezone.utc) - timedelta(6*30): # TZ-aware
status = f"<div style='cursor:help;width:1em;' title='Unmaintained'>{ui_symbols.svg_bullet.color('#C000CF')}</div>"
status = f"<div style='cursor:help;width:1em;' title='Unmaintained'>{ui_symbols.svg_bullet.style('#C000CF')}</div>"
else:
status = f"<div style='cursor:help;width:1em;' title='No info'>{ui_symbols.svg_bullet.color('#7C7C7C')}</div>"
status = f"<div style='cursor:help;width:1em;' title='No info'>{ui_symbols.svg_bullet.style('#7C7C7C')}</div>"
code += f"""
<tr style="display: {visible}">
+29 -8
View File
@@ -1,3 +1,9 @@
import re
from functools import lru_cache
# Basic symbols
refresh = ''
close = ''
load = ''
@@ -41,23 +47,38 @@ sort_time_dsc = '\uf0dd'
style_apply = ''
style_save = ''
# Configurable symbols
class SVGSymbol:
__re_display = re.compile(r"(?<=display:\s*?)\w+(?=;)")
@lru_cache # Class method due to RUF001, but also mostly so the `style` method shows params in IDE
@classmethod
def __stylize(cls, svg: str, color: str | None = None, display: str | None = None):
if color:
svg = re.sub("currentColor", color, svg, count=1)
if display:
svg = cls.__re_display.sub(display, svg, count=1)
return svg
def __init__(self, svg: str):
self.svg = svg
self.before = ""
self.after = ""
self.supports_color = False
self.supports_display = False
if "currentColor" in self.svg:
self.supports_color = True
self.before, self.after = self.svg.split("currentColor", maxsplit=1)
if self.__re_display.search(self.svg):
self.supports_display = True
def color(self, color: str):
if self.supports_color:
return self.before + color + self.after
else:
return self.svg
def style(self, color: str | None = None, display: str | None = None) -> str:
style_args = {
"color": color if color and self.supports_color else None,
"display": display if display and self.supports_display else None
}
return self.__stylize(self.svg, **style_args)
def __str__(self):
return self.svg
svg_bullet = SVGSymbol("<svg style='stroke:currentColor;fill:none;stroke-width:2;' viewBox='0 0 16 16'><circle cx='8' cy='8' r='7'/></svg>")