diff options
-rw-r--r-- | ranger/core/fm.py | 26 | ||||
-rw-r--r-- | ranger/ext/img_display.py | 19 |
2 files changed, 24 insertions, 21 deletions
diff --git a/ranger/core/fm.py b/ranger/core/fm.py index 43001e8b..e0a7b5df 100644 --- a/ranger/core/fm.py +++ b/ranger/core/fm.py @@ -22,11 +22,7 @@ from ranger.container.tags import Tags, TagsDummy from ranger.gui.ui import UI from ranger.container.bookmarks import Bookmarks from ranger.core.runner import Runner -from ranger.ext.img_display import (W3MImageDisplayer, ITerm2ImageDisplayer, - TerminologyImageDisplayer, - URXVTImageDisplayer, URXVTImageFSDisplayer, - KittyImageDisplayer, UeberzugImageDisplayer, - ImageDisplayer) +from ranger.ext import img_display from ranger.core.metadata import MetadataManager from ranger.ext.rifle import Rifle from ranger.container.directory import Directory @@ -227,22 +223,10 @@ class FM(Actions, # pylint: disable=too-many-instance-attributes for line in entry.splitlines(): yield line - def _get_image_displayer(self): # pylint: disable=too-many-return-statements - if self.settings.preview_images_method == "w3m": - return W3MImageDisplayer() - elif self.settings.preview_images_method == "iterm2": - return ITerm2ImageDisplayer() - elif self.settings.preview_images_method == "terminology": - return TerminologyImageDisplayer() - elif self.settings.preview_images_method == "urxvt": - return URXVTImageDisplayer() - elif self.settings.preview_images_method == "urxvt-full": - return URXVTImageFSDisplayer() - elif self.settings.preview_images_method == "kitty": - return KittyImageDisplayer() - elif self.settings.preview_images_method == "ueberzug": - return UeberzugImageDisplayer() - return ImageDisplayer() + def _get_image_displayer(self): + registry = img_display.IMAGE_DISPLAYER_REGISTRY + cls = registry[self.settings.preview_images_method] + return cls() def _get_thisfile(self): return self.thistab.thisfile diff --git a/ranger/ext/img_display.py b/ranger/ext/img_display.py index 2cce5c7a..aa4822ec 100644 --- a/ranger/ext/img_display.py +++ b/ranger/ext/img_display.py @@ -23,6 +23,7 @@ import warnings import json import threading from subprocess import Popen, PIPE +from collections import defaultdict import termios from contextlib import contextmanager @@ -71,6 +72,19 @@ class ImageDisplayError(Exception): class ImgDisplayUnsupportedException(Exception): pass +def fallback_image_displayer(): + """Simply makes some noise when chosen. Temporary fallback behavior.""" + + raise ImgDisplayUnsupportedException + +IMAGE_DISPLAYER_REGISTRY = defaultdict(fallback_image_displayer) +def register_image_displayer(nickname=None): + """Register an ImageDisplayer by nickname if available.""" + + def decorator(cls): + IMAGE_DISPLAYER_REGISTRY[nickname or cls.__name__] = cls + return cls + return decorator class ImageDisplayer(object): """Image display provider functions for drawing images in the terminal""" @@ -90,6 +104,7 @@ class ImageDisplayer(object): pass +@register_image_displayer("w3m") class W3MImageDisplayer(ImageDisplayer, FileManagerAware): """Implementation of ImageDisplayer using w3mimgdisplay, an utilitary program from w3m (a text-based web browser). w3mimgdisplay can display @@ -243,6 +258,7 @@ class W3MImageDisplayer(ImageDisplayer, FileManagerAware): # ranger-independent libraries. +@register_image_displayer("iterm2") class ITerm2ImageDisplayer(ImageDisplayer, FileManagerAware): """Implementation of ImageDisplayer using iTerm2 image display support (http://iterm2.com/images.html). @@ -351,6 +367,7 @@ class ITerm2ImageDisplayer(ImageDisplayer, FileManagerAware): return width, height +@register_image_displayer("terminology") class TerminologyImageDisplayer(ImageDisplayer, FileManagerAware): """Implementation of ImageDisplayer using terminology image display support (https://github.com/billiob/terminology). @@ -390,6 +407,7 @@ class TerminologyImageDisplayer(ImageDisplayer, FileManagerAware): self.clear(0, 0, 0, 0) +@register_image_displayer("urxvt") class URXVTImageDisplayer(ImageDisplayer, FileManagerAware): """Implementation of ImageDisplayer working by setting the urxvt background image "under" the preview pane. @@ -474,6 +492,7 @@ class URXVTImageDisplayer(ImageDisplayer, FileManagerAware): self.clear(0, 0, 0, 0) # dummy assignments +@register_image_displayer("urxvt-full") class URXVTImageFSDisplayer(URXVTImageDisplayer): """URXVTImageDisplayer that utilizes the whole terminal.""" |