summary refs log tree commit diff stats
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
parent58e6da694cdf6a1f6f78ae40cfaecdd630df65a7 (diff)
downloadranger-9b73aeb590066121ab4c80681c2b54be1c65d4bc.tar.gz
Implement the urxvt-based image previews
-rw-r--r--doc/ranger.142
-rw-r--r--doc/ranger.pod16
-rw-r--r--examples/rc_emacs.conf8
-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
7 files changed, 132 insertions, 12 deletions
diff --git a/doc/ranger.1 b/doc/ranger.1
index 184fa7e5..bf30ac6f 100644
--- a/doc/ranger.1
+++ b/doc/ranger.1
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32)
+.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
 .\"
 .\" Standard preamble:
 .\" ========================================================================
@@ -46,7 +46,7 @@
 .ie \n(.g .ds Aq \(aq
 .el       .ds Aq '
 .\"
-.\" If the F register is >0, we'll generate index entries on stderr for
+.\" If the F register is turned on, we'll generate index entries on stderr for
 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
 .\" entries marked with X<> in POD.  Of course, you'll have to process the
 .\" output yourself in some meaningful fashion.
@@ -54,16 +54,20 @@
 .\" Avoid warning from groff about undefined register 'F'.
 .de IX
 ..
-.if !\nF .nr F 0
-.if \nF>0 \{\
-.    de IX
-.    tm Index:\\$1\t\\n%\t"\\$2"
+.nr rF 0
+.if \n(.g .if rF .nr rF 1
+.if (\n(rF:(\n(.g==0)) \{
+.    if \nF \{
+.        de IX
+.        tm Index:\\$1\t\\n%\t"\\$2"
 ..
-.    if !\nF==2 \{\
-.        nr % 0
-.        nr F 2
+.        if !\nF==2 \{
+.            nr % 0
+.            nr F 2
+.        \}
 .    \}
 .\}
+.rr rF
 .\"
 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
@@ -129,7 +133,7 @@
 .\" ========================================================================
 .\"
 .IX Title "RANGER 1"
-.TH RANGER 1 "ranger-1.7.2" "09/03/2016" "ranger manual"
+.TH RANGER 1 "ranger-1.7.2" "10/05/2016" "ranger manual"
 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
 .\" way too many mistakes in technical documents.
 .if n .ad l
@@ -283,6 +287,24 @@ This only works in iTerm2 compiled with image preview support, but works over
 ssh.
 .PP
 To enable this feature, set the option \f(CW\*(C`preview_images_method\*(C'\fR to iterm2.
+.PP
+\fIurxvt\fR
+.IX Subsection "urxvt"
+.PP
+This only works in urxvt compiled with pixbuf support. Does not work over ssh.
+.PP
+Essentially this mode sets an image as a terminal background temporarily, so it
+will break any previously set image background.
+.PP
+To enable this feature, set the option \f(CW\*(C`preview_images_method\*(C'\fR to urxvt.
+.PP
+\fIurxvt-fs\fR
+.IX Subsection "urxvt-fs"
+.PP
+The same as urxvt but utilizing not only the preview pane but the whole terminal
+window.
+.PP
+To enable this feature, set the option \f(CW\*(C`preview_images_method\*(C'\fR to urxvt-fs.
 .SS "\s-1SELECTION\s0"
 .IX Subsection "SELECTION"
 The \fIselection\fR is defined as \*(L"All marked files \s-1IF THERE ARE ANY,\s0 otherwise
diff --git a/doc/ranger.pod b/doc/ranger.pod
index e8252dcb..03ff46e7 100644
--- a/doc/ranger.pod
+++ b/doc/ranger.pod
@@ -186,6 +186,22 @@ ssh.
 
 To enable this feature, set the option C<preview_images_method> to iterm2.
 
+=head3 urxvt
+
+This only works in urxvt compiled with pixbuf support. Does not work over ssh.
+
+Essentially this mode sets an image as a terminal background temporarily, so it
+will break any previously set image background.
+
+To enable this feature, set the option C<preview_images_method> to urxvt.
+
+=head3 urxvt-fs
+
+The same as urxvt but utilizing not only the preview pane but the whole terminal
+window.
+
+To enable this feature, set the option C<preview_images_method> to urxvt-fs.
+
 =head2 SELECTION
 
 The I<selection> is defined as "All marked files IF THERE ARE ANY, otherwise
diff --git a/examples/rc_emacs.conf b/examples/rc_emacs.conf
index 39a6d654..7a183c75 100644
--- a/examples/rc_emacs.conf
+++ b/examples/rc_emacs.conf
@@ -73,6 +73,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/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