summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-10-08 21:14:42 +0200
committerhut <hut@lavabit.com>2010-10-08 21:14:42 +0200
commitd538bd40182a00d4b6e6327d091a9e0943ee2976 (patch)
tree0534a7678978980fad3b66a384d1b1af00163c45
parent085934209cdfe195b7621a6e722c5688da3d7c1c (diff)
downloadranger-d538bd40182a00d4b6e6327d091a9e0943ee2976.tar.gz
fsobject.file: load previews in fm.loader
-rw-r--r--ranger/core/loader.py3
-rw-r--r--ranger/fsobject/file.py28
-rw-r--r--ranger/gui/widgets/pager.py1
3 files changed, 21 insertions, 11 deletions
diff --git a/ranger/core/loader.py b/ranger/core/loader.py
index 54d5a279..66f7017c 100644
--- a/ranger/core/loader.py
+++ b/ranger/core/loader.py
@@ -62,8 +62,7 @@ class CommandLoader(Loadable, SignalDispatcher, FileManagerAware):
 
 	def generate(self):
 		self.process = process = Popen(self.args,
-				stdout=open(os.devnull, 'w'),
-				stderr=PIPE)
+				stdout=PIPE, stderr=PIPE)
 		self.signal_emit('before', process=process)
 		while process.poll() is None:
 			try:
diff --git a/ranger/fsobject/file.py b/ranger/fsobject/file.py
index 57d82b31..5458b8b1 100644
--- a/ranger/fsobject/file.py
+++ b/ranger/fsobject/file.py
@@ -17,6 +17,7 @@ import re
 from ranger.fsobject import FileSystemObject
 from subprocess import Popen, PIPE
 from ranger.core.runner import devnull
+from ranger.core.loader import CommandLoader
 
 N_FIRST_BYTES = 20
 control_characters = set(chr(n) for n in
@@ -53,6 +54,8 @@ PREVIEW_WHITELIST = re.compile(r"""
 
 class File(FileSystemObject):
 	is_file = True
+	preview_data = None
+	preview_known = False
 
 	@property
 	def firstbytes(self):
@@ -91,15 +94,22 @@ class File(FileSystemObject):
 			return False
 		return True
 
+	def update_preview(self, signal):
+		self.preview_known = True
+		self.preview_data = None
+		if not signal.process.poll():
+			self.preview_data = signal.process.stdout.read()
+			self.fm.ui.pager.need_redraw = True
+			self.fm.ui.redraw()
+
 	def get_preview_source(self, widget):
 		if self.fm.settings.preview_script:
-			try:
-				p = Popen([self.fm.settings.preview_script, self.path,
-						str(widget.wid), str(widget.hei)],
-						stdout=PIPE, stderr=devnull)
-				if p.poll():  # nonzero exit code
-					return None
-				return p.stdout
-			except:
-				pass
+			if self.preview_known:
+				return self.preview_data
+			loadable = CommandLoader(args=[self.fm.settings.preview_script,
+				self.path, str(widget.wid), str(widget.hei)],
+				descr="Getting preview of %s" % self.path)
+			loadable.signal_bind('after', self.update_preview, weak=True)
+			self.fm.loader.add(loadable)
+			return "loading..."
 		return open(self.path, 'r')
diff --git a/ranger/gui/widgets/pager.py b/ranger/gui/widgets/pager.py
index 5e761a0f..798c4697 100644
--- a/ranger/gui/widgets/pager.py
+++ b/ranger/gui/widgets/pager.py
@@ -168,6 +168,7 @@ class Pager(Widget):
 
 		if isinstance(source, str):
 			self.source_is_stream = False
+			self.markup = 'ansi'
 			self.lines = source.splitlines()
 		elif hasattr(source, '__getitem__'):
 			self.source_is_stream = False