Skip to content

Commit

Permalink
feat: Add .widget.UrwidImageScreen
Browse files Browse the repository at this point in the history
- Add: An urwid screen that clears *kitty* images at start and stop.
  • Loading branch information
AnonymouX47 committed Feb 22, 2023
1 parent c1af6af commit f0cb34b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/term_image/widget/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
except ImportError:
pass
else:
from .urwid import UrwidImage, UrwidImageCanvas, UrwidImageJanitor
from .urwid import UrwidImage, UrwidImageCanvas, UrwidImageJanitor, UrwidImageScreen

del urwid
__all__ += ["UrwidImage", "UrwidImageCanvas", "UrwidImageJanitor"]
__all__ += [
"UrwidImage",
"UrwidImageCanvas",
"UrwidImageJanitor",
"UrwidImageScreen",
]
26 changes: 25 additions & 1 deletion src/term_image/widget/urwid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import urwid

from ..exceptions import UrwidImageError
from ..image import BaseImage, ITerm2Image, KittyImage, Size, TextImage
from ..image import BaseImage, ITerm2Image, KittyImage, Size, TextImage, kitty
from ..utils import COLOR_RESET_b, ESC_b

# NOTE: Any new "private" attribute of any subclass of an urwid class should be
Expand Down Expand Up @@ -601,3 +601,27 @@ def process_shard_tails():
self._ti_image_cviews = frozenset(images)

return main_canv


class UrwidImageScreen(urwid.raw_display.Screen):
"""A screen that clears all visible images of the
:py:class:`kitty <term_image.image.KittyImage>` render style immediately after
it starts and immediately before it stops.
See the `baseclass
<http://urwid.org/reference/display_modules.html#urwid.raw_display.Screen>`_
for futher description.
"""

def _start(self, *args, **kwargs):
ret = super()._start(*args, **kwargs)
if KittyImage._forced_support or KittyImage.is_supported():
self.write(kitty.DELETE_ALL_IMAGES)

return ret

def _stop(self):
if KittyImage._forced_support or KittyImage.is_supported():
self.write(kitty.DELETE_ALL_IMAGES)

return super()._stop()

0 comments on commit f0cb34b

Please sign in to comment.