From efab6cf556faa7d32825596f34e01219272579c0 Mon Sep 17 00:00:00 2001 From: AnonymouX47 Date: Tue, 4 Jun 2024 08:36:48 +0100 Subject: [PATCH 1/3] refac: tui: Optimize image pixel support detection --- toot/tui/images.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/toot/tui/images.py b/toot/tui/images.py index 77e5bbc..89710c7 100644 --- a/toot/tui/images.py +++ b/toot/tui/images.py @@ -10,6 +10,8 @@ try: from term_image import disable_queries # prevent phantom keystrokes from PIL import Image, ImageDraw + _IMAGE_PIXEL_FORMATS = frozenset({'kitty', 'iterm'}) + TuiScreen = UrwidImageScreen disable_queries() @@ -17,7 +19,7 @@ try: return True def can_render_pixels(image_format): - return image_format in ['kitty', 'iterm'] + return image_format in _IMAGE_PIXEL_FORMATS def get_base_image(image, image_format) -> BaseImage: # we don't autodetect kitty, iterm; we choose based on option switches From b264927da991c6a3285fab99b486cda2076817a3 Mon Sep 17 00:00:00 2001 From: AnonymouX47 Date: Tue, 4 Jun 2024 08:46:35 +0100 Subject: [PATCH 2/3] refac: tui.images: Optimize image class selection - Change: Execute image class selection logic once and cache the result. --- toot/tui/images.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/toot/tui/images.py b/toot/tui/images.py index 89710c7..00ebfe7 100644 --- a/toot/tui/images.py +++ b/toot/tui/images.py @@ -11,6 +11,7 @@ try: from PIL import Image, ImageDraw _IMAGE_PIXEL_FORMATS = frozenset({'kitty', 'iterm'}) + _ImageCls = None TuiScreen = UrwidImageScreen disable_queries() @@ -23,13 +24,20 @@ try: def get_base_image(image, image_format) -> BaseImage: # we don't autodetect kitty, iterm; we choose based on option switches - BaseImage.forced_support = True - if image_format == 'kitty': - return KittyImage(image) - elif image_format == 'iterm': - return ITerm2Image(image) - else: - return BlockImage(image) + + global _ImageCls + + if not _ImageCls: + BaseImage.forced_support = True + _ImageCls = ( + 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: if baseheight and not basewidth: From 1ea2e29e2563f34cedfbe1f15ad7260684f3b84e Mon Sep 17 00:00:00 2001 From: AnonymouX47 Date: Tue, 4 Jun 2024 08:59:09 +0100 Subject: [PATCH 3/3] fix: Fix iTerm2's title with image support enabled - Fix: Force support support only for the selected image format. If support is forced for `KittyImage`, then `UrwidImageScreen` will emit kitty graphics control sequences to delete images upon starting and stoping the screen. iTerm2 (on Mac OS) doesn't eat up APCs (which kitty graphics control sequences are) as it should, instead it writes them to the screen and/or to its title bar. --- toot/tui/images.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toot/tui/images.py b/toot/tui/images.py index 00ebfe7..c74489c 100644 --- a/toot/tui/images.py +++ b/toot/tui/images.py @@ -28,7 +28,6 @@ try: global _ImageCls if not _ImageCls: - BaseImage.forced_support = True _ImageCls = ( KittyImage if image_format == 'kitty' @@ -36,6 +35,7 @@ try: if image_format == 'iterm' else BlockImage ) + _ImageCls.forced_support = True return _ImageCls(image)