refac: tui.images: Optimize image class selection

- Change: Execute image class selection logic once and cache the result.
This commit is contained in:
AnonymouX47 2024-06-04 08:46:35 +01:00
parent efab6cf556
commit b264927da9
1 changed files with 15 additions and 7 deletions

View File

@ -11,6 +11,7 @@ try:
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
_IMAGE_PIXEL_FORMATS = frozenset({'kitty', 'iterm'}) _IMAGE_PIXEL_FORMATS = frozenset({'kitty', 'iterm'})
_ImageCls = None
TuiScreen = UrwidImageScreen TuiScreen = UrwidImageScreen
disable_queries() disable_queries()
@ -23,13 +24,20 @@ try:
def get_base_image(image, image_format) -> BaseImage: def get_base_image(image, image_format) -> BaseImage:
# we don't autodetect kitty, iterm; we choose based on option switches # we don't autodetect kitty, iterm; we choose based on option switches
BaseImage.forced_support = True
if image_format == 'kitty': global _ImageCls
return KittyImage(image)
elif image_format == 'iterm': if not _ImageCls:
return ITerm2Image(image) BaseImage.forced_support = True
else: _ImageCls = (
return BlockImage(image) KittyImage
if image_format == 'kitty'
else ITerm2Image
if image_format == 'iterm'
else BlockImage
)
return _ImageCls(image)
def resize_image(basewidth: int, baseheight: int, img: Image.Image) -> Image.Image: def resize_image(basewidth: int, baseheight: int, img: Image.Image) -> Image.Image:
if baseheight and not basewidth: if baseheight and not basewidth: