summary refs log tree commit diff stats
path: root/ranger/core
diff options
context:
space:
mode:
authorGermainZ <germanosz@gmail.com>2014-03-09 12:46:36 +0200
committerGermainZ <germanosz@gmail.com>2014-03-09 12:46:36 +0200
commit085ea1e99122a032424133104fca2e76610d8b1d (patch)
tree29176b2ca3fdd7a02d7653ed155a399714831121 /ranger/core
parentf61fbfae9257363979ded4688240f6a83c3f3233 (diff)
downloadranger-085ea1e99122a032424133104fca2e76610d8b1d.tar.gz
Allow scope.sh to be used for image previews
Diffstat (limited to 'ranger/core')
-rw-r--r--ranger/core/actions.py28
-rw-r--r--ranger/core/main.py6
2 files changed, 30 insertions, 4 deletions
diff --git a/ranger/core/actions.py b/ranger/core/actions.py
index 80b35970..9f20adbb 100644
--- a/ranger/core/actions.py
+++ b/ranger/core/actions.py
@@ -11,6 +11,7 @@ from os.path import join, isdir, realpath, exists
 from os import link, symlink, getcwd, listdir, stat
 from inspect import cleandoc
 from stat import S_IEXEC
+from hashlib import sha1
 
 import ranger
 from ranger.ext.direction import Direction
@@ -782,7 +783,11 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware):
         if self.settings.preview_images and self.thisfile.image:
             pager.set_image(self.thisfile.realpath)
         else:
-            pager.set_source(self.thisfile.get_preview_source(pager.wid, pager.hei))
+            f = self.thisfile.get_preview_source(pager.wid, pager.hei)
+            if self.thisfile.is_image_preview:
+                pager.set_image(f)
+            else:
+                pager.set_source(f)
 
     # --------------------------
     # -- Previews
@@ -836,8 +841,17 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware):
                     return None
 
                 data['loading'] = True
+
+                cacheimg = os.path.join(ranger.CACHEDIR, sha1(path.encode()).hexdigest() + '.jpg')
+                if (os.path.isfile(cacheimg) and os.path.getmtime(cacheimg) > os.path.getmtime(path)):
+                    data['foundpreview'] = True
+                    data['imagepreview'] = True
+                    pager.set_image(cacheimg)
+                    data['loading'] = False
+                    return cacheimg
+
                 loadable = CommandLoader(args=[self.settings.preview_script,
-                    path, str(width), str(height)], read=True,
+                    path, str(width), str(height), cacheimg], read=True,
                     silent=True, descr="Getting preview of %s" % path)
                 def on_after(signal):
                     exit = signal.process.poll()
@@ -851,6 +865,8 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware):
                         data[(width, -1)] = content
                     elif exit == 5:
                         data[(-1, -1)] = content
+                    elif exit == 6:
+                        data['imagepreview'] = True
                     elif exit == 1:
                         data[(-1, -1)] = None
                         data['foundpreview'] = False
@@ -871,8 +887,12 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware):
                     data['loading'] = False
                     pager = self.ui.get_pager()
                     if self.thisfile and self.thisfile.is_file:
-                        pager.set_source(self.thisfile.get_preview_source(
-                            pager.wid, pager.hei))
+                        if 'imagepreview' in data:
+                            pager.set_image(cacheimg)
+                            return cacheimg
+                        else:
+                            pager.set_source(self.thisfile.get_preview_source(
+                                pager.wid, pager.hei))
                 def on_destroy(signal):
                     try:
                         del self.previews[path]
diff --git a/ranger/core/main.py b/ranger/core/main.py
index ff4e02d6..631209cd 100644
--- a/ranger/core/main.py
+++ b/ranger/core/main.py
@@ -110,6 +110,12 @@ def main():
             from ranger.ext import curses_interrupt_handler
             curses_interrupt_handler.install_interrupt_handler()
 
+        # Create cache directory
+        if fm.settings.preview_images and fm.settings.use_preview_script:
+            from ranger import CACHEDIR
+            if not os.path.exists(CACHEDIR):
+                os.makedirs(CACHEDIR)
+
         # Run the file manager
         fm.initialize()
         ranger.api.hook_init(fm)