summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorWojciech Siewierski <wojciech.siewierski@onet.pl>2016-10-05 23:58:31 +0200
committerWojciech Siewierski <wojciech.siewierski@onet.pl>2016-10-05 23:58:31 +0200
commit9b73aeb590066121ab4c80681c2b54be1c65d4bc (patch)
tree142890799995ba8ba107a3caaafa3b15afabfc4b /ranger
parent58e6da694cdf6a1f6f78ae40cfaecdd630df65a7 (diff)
downloadranger-9b73aeb590066121ab4c80681c2b54be1c65d4bc.tar.gz
Implement the urxvt-based image previews
Diffstat (limited to 'ranger')
-rw-r--r--ranger/config/rc.conf8
-rw-r--r--ranger/container/settings.py2
-rw-r--r--ranger/core/fm.py4
-rw-r--r--ranger/ext/img_display.py64
4 files changed, 76 insertions, 2 deletions
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index cc7f5007..02d7e411 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -79,6 +79,14 @@ set preview_images false
 #   Preview images in full color using iTerm2 image previews
 #   (http://iterm2.com/images.html). This requires using iTerm2 compiled
 #   with image preview support.
+#
+# * urxvt:
+#   Preview images in full color using urxvt image backgrounds. This
+#   requires using urxvt compiled with pixbuf support.
+#
+# * urxvt-fs:
+#   The same as urxvt but utilizing not only the preview pane but the
+#   whole terminal window.
 set preview_images_method w3m
 
 # Use a unicode "..." character to mark cut-off filenames?
diff --git a/ranger/container/settings.py b/ranger/container/settings.py
index a5d71874..1faf5860 100644
--- a/ranger/container/settings.py
+++ b/ranger/container/settings.py
@@ -79,7 +79,7 @@ ALLOWED_SETTINGS = {
 ALLOWED_VALUES = {
     'confirm_on_delete': ['always', 'multiple', 'never'],
     'line_numbers': ['false', 'absolute', 'relative'],
-    'preview_images_method': ['w3m', 'iterm2'],
+    'preview_images_method': ['w3m', 'iterm2', 'urxvt', 'urxvt-full'],
     'vcs_backend_bzr': ['enabled', 'local', 'disabled'],
     'vcs_backend_git': ['enabled', 'local', 'disabled'],
     'vcs_backend_hg': ['enabled', 'local', 'disabled'],
diff --git a/ranger/core/fm.py b/ranger/core/fm.py
index 523239c1..8168b7d6 100644
--- a/ranger/core/fm.py
+++ b/ranger/core/fm.py
@@ -203,6 +203,10 @@ class FM(Actions, SignalDispatcher):
             return W3MImageDisplayer()
         elif self.settings.preview_images_method == "iterm2":
             return ITerm2ImageDisplayer()
+        elif self.settings.preview_images_method == "urxvt":
+            return URXVTImageDisplayer()
+        elif self.settings.preview_images_method == "urxvt-full":
+            return URXVTImageFSDisplayer()
         else:
             return ImageDisplayer()
 
diff --git a/ranger/ext/img_display.py b/ranger/ext/img_display.py
index ada7b31c..a7e2f511 100644
--- a/ranger/ext/img_display.py
+++ b/ranger/ext/img_display.py
@@ -6,7 +6,7 @@
 """Interface for drawing images into the console
 
 This module provides functions to draw images in the terminal using supported
-implementations, which are currently w3m and iTerm2.
+implementations, which are currently w3m, iTerm2 and urxvt.
 """
 
 import base64
@@ -295,3 +295,65 @@ class ITerm2ImageDisplayer(ImageDisplayer, FileManagerAware):
             return 0, 0
         file_handle.close()
         return width, height
+
+
+class URXVTImageDisplayer(ImageDisplayer, FileManagerAware):
+    """Implementation of ImageDisplayer working by setting the urxvt
+    background image "under" the preview pane.
+
+    Ranger must be running in urxvt for this to work.
+
+    """
+
+    def _get_sizes(self):
+        """Return the width and height of the preview pane in relation to the
+        whole terminal window.
+
+        """
+        total_columns_ratio = sum(self.fm.settings.column_ratios)
+        preview_column_ratio = self.fm.settings.column_ratios[-1]
+        w = int((100 * preview_column_ratio) / total_columns_ratio)
+        h = 100  # As much as possible while preserving the aspect ratio.
+        return w, h
+
+    def _get_offsets(self):
+        """Return the offsets of the image center."""
+        x = 100  # Right-aligned.
+        y = 2    # TODO: Use the font size to calculate this offset.
+        return x, y
+
+    def draw(self, path, start_x, start_y, width, height):
+        # The coordinates in the arguments are ignored as urxvt takes
+        # the coordinates in a non-standard way: the position of the
+        # image center as a percentage of the terminal size. As a
+        # result all values below are in percents.
+
+        x, y = self._get_offsets()
+        w, h = self._get_sizes()
+
+        sys.stdout.write("\033]20;{path};{w}x{h}+{x}+{y}:op=keep-aspect\a".format(**vars()))
+        sys.stdout.flush()
+
+    def clear(self, start_x, start_y, width, height):
+        sys.stdout.write("\033]20;;100x100+1000+1000\a")
+        sys.stdout.flush()
+
+    def quit(self):
+        sys.stdout.write("\033]20;;100x100+1000+1000\a")
+        sys.stdout.flush()
+
+
+class URXVTImageFSDisplayer(URXVTImageDisplayer):
+    """URXVTImageDisplayer that utilizes the whole terminal."""
+
+    def _get_sizes(self):
+        """Use the whole terminal."""
+        w = 100
+        h = 100
+        return w, h
+
+    def _get_offsets(self):
+        """Center the image."""
+        x = 50
+        y = 50
+        return x, y