From 085ea1e99122a032424133104fca2e76610d8b1d Mon Sep 17 00:00:00 2001 From: GermainZ Date: Sun, 9 Mar 2014 12:46:36 +0200 Subject: Allow scope.sh to be used for image previews --- ranger/__init__.py | 1 + ranger/container/file.py | 3 +++ ranger/core/actions.py | 28 ++++++++++++++++++++++++---- ranger/core/main.py | 6 ++++++ ranger/data/scope.sh | 4 ++++ ranger/gui/widgets/browsercolumn.py | 5 ++++- 6 files changed, 42 insertions(+), 5 deletions(-) diff --git a/ranger/__init__.py b/ranger/__init__.py index c3f05d3d..05d2f623 100644 --- a/ranger/__init__.py +++ b/ranger/__init__.py @@ -25,6 +25,7 @@ MAX_RESTORABLE_TABS = 3 MACRO_DELIMITER = '%' DEFAULT_PAGER = 'less' LOGFILE = '/tmp/ranger_errorlog' +CACHEDIR = os.path.expanduser("~/.cache/ranger") USAGE = '%prog [options] [path]' VERSION = 'ranger-master %s\n\nPython %s' % (__version__, sys.version) diff --git a/ranger/container/file.py b/ranger/container/file.py index 7c83cace..a404ef91 100644 --- a/ranger/container/file.py +++ b/ranger/container/file.py @@ -87,3 +87,6 @@ class File(FileSystemObject): def get_preview_source(self, width, height): return self.fm.get_preview(self, width, height) + + def is_image_preview(self): + return 'imagepreview' in self.fm.previews[self.realpath] 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) diff --git a/ranger/data/scope.sh b/ranger/data/scope.sh index 76481f94..eb905d99 100755 --- a/ranger/data/scope.sh +++ b/ranger/data/scope.sh @@ -16,11 +16,13 @@ # 3 | fix width | success. Don't reload when width changes # 4 | fix height | success. Don't reload when height changes # 5 | fix both | success. Don't ever reload +# 6 | image | success. display the image $cached points to as an image preview # Meaningful aliases for arguments: path="$1" # Full path of the selected file width="$2" # Width of the preview pane (number of fitting characters) height="$3" # Height of the preview pane (number of fitting characters) +cached="$4" # Path that should be used to cache image previews maxln=200 # Stop after $maxln lines. Can be used like ls | head -n $maxln @@ -75,6 +77,8 @@ case "$mimetype" in image/*) img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;; # Display information about media files: + video/*) + ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;; video/* | audio/*) exiftool "$path" && exit 5 # Use sed to remove spaces so the output fits into the narrow window diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py index d705e6ba..14900e8b 100644 --- a/ranger/gui/widgets/browsercolumn.py +++ b/ranger/gui/widgets/browsercolumn.py @@ -186,7 +186,10 @@ class BrowserColumn(Pager): if f is None: Pager.close(self) else: - self.set_source(f) + if self.target.is_image_preview(): + self.set_image(f) + else: + self.set_source(f) Pager.draw(self) def _draw_directory(self): -- cgit 1.4.1-2-gfad0 From 53ccbcf35566d5873f4f42b2d0ea8c080626bd31 Mon Sep 17 00:00:00 2001 From: GermainZ Date: Sun, 9 Mar 2014 12:54:04 +0200 Subject: Disable video image previews by default --- ranger/data/scope.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ranger/data/scope.sh b/ranger/data/scope.sh index eb905d99..6659b76e 100755 --- a/ranger/data/scope.sh +++ b/ranger/data/scope.sh @@ -76,9 +76,10 @@ case "$mimetype" in # Ascii-previews of images: image/*) img2txt --gamma=0.6 --width="$width" "$path" && exit 4 || exit 1;; + # Image preview for videos, disabled by default: + # video/*) + # ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;; # Display information about media files: - video/*) - ffmpegthumbnailer -i "$path" -o "$cached" -s 0 && exit 6 || exit 1;; video/* | audio/*) exiftool "$path" && exit 5 # Use sed to remove spaces so the output fits into the narrow window -- cgit 1.4.1-2-gfad0 From c2ec5e1b34fa702879ff73ef8941cf416edf5402 Mon Sep 17 00:00:00 2001 From: GermainZ Date: Sun, 9 Mar 2014 12:55:39 +0200 Subject: Update rc.conf --- ranger/config/rc.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf index 8955b4a4..e7b19cf6 100644 --- a/ranger/config/rc.conf +++ b/ranger/config/rc.conf @@ -38,7 +38,7 @@ set confirm_on_delete multiple # README for dependencies) to preview images, archives, etc. set preview_script ~/.config/ranger/scope.sh -# Use the external preview script or display simple plain text previews? +# Use the external preview script or display simple plain text or image previews? set use_preview_script true # Open all images in this directory when running certain image viewers -- cgit 1.4.1-2-gfad0 From 91daf58b56c7b84361f6be52097b753e737f3002 Mon Sep 17 00:00:00 2001 From: GermainZ Date: Sun, 9 Mar 2014 22:14:46 +0200 Subject: Python2 fix for utf-8 characters in file paths --- ranger/core/actions.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 9f20adbb..a3497902 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -12,6 +12,7 @@ from os import link, symlink, getcwd, listdir, stat from inspect import cleandoc from stat import S_IEXEC from hashlib import sha1 +from sys import version_info import ranger from ranger.ext.direction import Direction @@ -799,6 +800,13 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware): except: return False + if version_info[0] == 3: + def sha1_encode(self, path): + return os.path.join(ranger.CACHEDIR, sha1(path.encode('utf-8')).hexdigest()) + else: + def sha1_encode(self, path): + return os.path.join(ranger.CACHEDIR, sha1(path).hexdigest()) + def get_preview(self, file, width, height): pager = self.ui.get_pager() path = file.realpath @@ -842,7 +850,7 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware): data['loading'] = True - cacheimg = os.path.join(ranger.CACHEDIR, sha1(path.encode()).hexdigest() + '.jpg') + cacheimg = os.path.join(ranger.CACHEDIR, self.sha1_encode(path)) if (os.path.isfile(cacheimg) and os.path.getmtime(cacheimg) > os.path.getmtime(path)): data['foundpreview'] = True data['imagepreview'] = True -- cgit 1.4.1-2-gfad0 From b4b3bc6e8a0ba9a5855120bbec8712bb34bbd25a Mon Sep 17 00:00:00 2001 From: GermainZ Date: Sun, 9 Mar 2014 22:31:27 +0200 Subject: Add extension --- ranger/core/actions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ranger/core/actions.py b/ranger/core/actions.py index a3497902..2dfb116e 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -802,10 +802,12 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware): if version_info[0] == 3: def sha1_encode(self, path): - return os.path.join(ranger.CACHEDIR, sha1(path.encode('utf-8')).hexdigest()) + return os.path.join(ranger.CACHEDIR, + sha1(path.encode('utf-8')).hexdigest()) + '.jpg' else: def sha1_encode(self, path): - return os.path.join(ranger.CACHEDIR, sha1(path).hexdigest()) + return os.path.join(ranger.CACHEDIR, + sha1(path).hexdigest()) + '.jpg' def get_preview(self, file, width, height): pager = self.ui.get_pager() -- cgit 1.4.1-2-gfad0