summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2013-02-16 17:32:59 +0100
committerhut <hut@lavabit.com>2013-02-16 17:32:59 +0100
commit4fc4f85607b27df380184395f65161363db748f8 (patch)
treebe1e39200a8fdac8f26ef9d07f7189c4cdfc6c14
parent225aa78ffb38b3dfea9d2b1c0ab3b6eb6f4977b7 (diff)
parent9cc2278339448e1f1b8dff2664175b49e28fa035 (diff)
downloadranger-4fc4f85607b27df380184395f65161363db748f8.tar.gz
Merge branch 'master' into vcs
-rw-r--r--ranger/core/fm.py10
-rw-r--r--ranger/core/loader.py27
-rw-r--r--ranger/ext/img_display.py17
-rw-r--r--ranger/fsobject/file.py4
-rw-r--r--ranger/gui/widgets/pager.py9
5 files changed, 56 insertions, 11 deletions
diff --git a/ranger/core/fm.py b/ranger/core/fm.py
index d29e446b..e9ffb888 100644
--- a/ranger/core/fm.py
+++ b/ranger/core/fm.py
@@ -167,12 +167,22 @@ class FM(Actions, SignalDispatcher):
             sys.stderr.write("refusing to copy config files in clean mode\n")
             return
         import shutil
+        from errno import EEXIST
         def copy(_from, to):
             if os.path.exists(self.confpath(to)):
                 sys.stderr.write("already exists: %s\n" % self.confpath(to))
             else:
                 sys.stderr.write("creating: %s\n" % self.confpath(to))
                 try:
+                    os.makedirs(ranger.arg.confdir)
+                except OSError as err:
+                    if err.errno != EEXIST:  # EEXIST means it already exists
+                        print("This configuration directory could not be created:")
+                        print(ranger.arg.confdir)
+                        print("To run ranger without the need for configuration")
+                        print("files, use the --clean option.")
+                        raise SystemExit()
+                try:
                     shutil.copy(self.relpath(_from), self.confpath(to))
                 except Exception as e:
                     sys.stderr.write("  ERROR: %s\n" % str(e))
diff --git a/ranger/core/loader.py b/ranger/core/loader.py
index d5a30caf..bae19e9e 100644
--- a/ranger/core/loader.py
+++ b/ranger/core/loader.py
@@ -121,22 +121,37 @@ class CommandLoader(Loadable, SignalDispatcher, FileManagerAware):
     """
     finished = False
     process = None
-    def __init__(self, args, descr, silent=False, read=False):
+    def __init__(self, args, descr, silent=False, read=False, input=None,
+            kill_on_pause=False):
         SignalDispatcher.__init__(self)
         Loadable.__init__(self, self.generate(), descr)
         self.args = args
         self.silent = silent
         self.read = read
         self.stdout_buffer = ""
+        self.input = input
+        self.kill_on_pause = kill_on_pause
 
     def generate(self):
-        null = open(os.devnull, 'r')
+        if self.input:
+            stdin = PIPE
+        else:
+            stdin = open(os.devnull, 'r')
         self.process = process = Popen(self.args,
-                stdout=PIPE, stderr=PIPE, stdin=null)
+                stdout=PIPE, stderr=PIPE, stdin=stdin)
         self.signal_emit('before', process=process, loader=self)
+        if self.input:
+            try:
+                process.stdin.write(self.input)
+            except IOError as e:
+                if e.errno != errno.EPIPE and e.errno != errno.EINVAL:
+                    raise
+            process.stdin.close()
         if self.silent and not self.read:
             while process.poll() is None:
                 yield
+                if self.finished:
+                    break
                 sleep(0.03)
         else:
             py3 = sys.version >= '3'
@@ -147,6 +162,8 @@ class CommandLoader(Loadable, SignalDispatcher, FileManagerAware):
                 selectlist.append(process.stderr)
             while process.poll() is None:
                 yield
+                if self.finished:
+                    break
                 try:
                     rd, _, __ = select.select(selectlist, [], [], 0.03)
                     if rd:
@@ -175,12 +192,14 @@ class CommandLoader(Loadable, SignalDispatcher, FileManagerAware):
                 if py3:
                     read = safeDecode(read)
                 self.stdout_buffer += read
-        null.close()
         self.finished = True
         self.signal_emit('after', process=process, loader=self)
 
     def pause(self):
         if not self.finished and not self.paused:
+            if self.kill_on_pause:
+                self.finished = True
+                return
             try:
                 self.process.send_signal(20)
             except:
diff --git a/ranger/ext/img_display.py b/ranger/ext/img_display.py
index 307c7b75..9dcd276b 100644
--- a/ranger/ext/img_display.py
+++ b/ranger/ext/img_display.py
@@ -43,9 +43,9 @@ def _w3mimgdisplay(commands):
 
     return output
 
-def draw(path, start_x, start_y, max_width, max_height):
+def generate_w3m_input(path, start_x, start_y, max_width, max_height):
     """
-    Draw an image file in the terminal.
+    Prepare the input string for w3mimgpreview
 
     start_x, start_y, max_height and max_width specify the drawing area.
     They are expressed in number of characters.
@@ -75,14 +75,21 @@ def draw(path, start_x, start_y, max_width, max_height):
         width = (width * max_height_pixels) // height
         height = max_height_pixels
 
-    # draw
-    cmd = "0;1;{x};{y};{w};{h};;;;;{filename}\n4;\n3;".format(
+    return "0;1;{x};{y};{w};{h};;;;;{filename}\n4;\n3;".format(
             x = start_x * fontw,
             y = start_y * fonth,
             w = width,
             h = height,
             filename = path)
-    _w3mimgdisplay(cmd)
+
+def draw(path, start_x, start_y, max_width, max_height):
+    """
+    Draw an image file in the terminal.
+
+    start_x, start_y, max_height and max_width specify the drawing area.
+    They are expressed in number of characters.
+    """
+    _w3mimgdisplay(generate_w3m_input(path, start_x, start_y, max_width, max_height))
 
 def clear(start_x, start_y, width, height):
     """
diff --git a/ranger/fsobject/file.py b/ranger/fsobject/file.py
index 8d75e740..2a287ff5 100644
--- a/ranger/fsobject/file.py
+++ b/ranger/fsobject/file.py
@@ -72,7 +72,9 @@ class File(FileSystemObject, SettingsAware):
         if self.fm.settings.preview_script and \
                 self.fm.settings.use_preview_script:
             return True
-        if self.image or self.container:
+        if self.image and self.fm.settings.preview_images:
+            return True
+        if self.container:
             return False
         if PREVIEW_WHITELIST.search(self.basename):
             return True
diff --git a/ranger/gui/widgets/pager.py b/ranger/gui/widgets/pager.py
index 4e8161a5..8bfdf42d 100644
--- a/ranger/gui/widgets/pager.py
+++ b/ranger/gui/widgets/pager.py
@@ -6,6 +6,7 @@
 The pager displays text and allows you to scroll inside it.
 """
 from . import Widget
+from ranger.core.loader import CommandLoader
 from ranger.gui import ansi
 from ranger.ext.direction import Direction
 import ranger.ext.img_display as img_display
@@ -83,7 +84,13 @@ class Pager(Widget):
             self.source = None
             self.need_redraw_image = False
             try:
-                img_display.draw(self.image, self.x, self.y, self.wid, self.hei)
+                cmd = CommandLoader([img_display.W3MIMGDISPLAY_PATH] +
+                            img_display.W3MIMGDISPLAY_OPTIONS,
+                        input=img_display.generate_w3m_input(self.image,
+                            self.x, self.y, self.wid, self.hei),
+                        descr="loading preview image",
+                        silent=True, kill_on_pause=True)
+                self.fm.loader.add(cmd)
             except img_display.ImgDisplayUnsupportedException:
                 self.fm.settings.preview_images = False
             except Exception as e:
ot;""Looks for an application, returns app_default if it doesn't exist""" try: return getattr(self, 'app_' + app) except AttributeError: return self.app_default def apply(self, app, context): if not app: app = 'default' try: handler = getattr(self, 'app_' + app) except AttributeError: if app in get_executables(): return tup(app, *context) # generic app handler = self.app_default return handler(context) def has(self, app): """Returns whether an application is defined""" return hasattr(self, 'app_' + app) def all(self): """Returns a list with all application functions""" result = set() # go through all the classes in the mro (method resolution order) # so subclasses will return the apps of their superclasses. for cls in self.__class__.__mro__: result |= set(m[4:] for m in cls.__dict__ if m.startswith('app_')) return sorted(result) def tup(*args): """ This helper function creates a tuple out of the arguments. ('a', ) + tuple(some_iterator) is equivalent to: tup('a', *some_iterator) """ return args def depends_on(*args): args = tuple(flatten(args)) def decorator(fnc): fnc.dependencies = args return fnc return decorator