summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-05-17 21:16:57 +0200
committerhut <hut@lavabit.com>2010-05-17 21:16:57 +0200
commitf4c646d8a61710747e157ebac2e27ebfacf7b54c (patch)
tree1b593856e885b3324b9c7c4b7bda362d0b6409cc
parenta23a3649948fd001f1d0fd6a932408c7d982fc14 (diff)
parent437c12d9093b09d0ebb90bc6bfe35a8769c31c17 (diff)
downloadranger-f4c646d8a61710747e157ebac2e27ebfacf7b54c.tar.gz
Merge git://github.com/dbarnett/ranger
-rw-r--r--ranger/fsobject/file.py59
-rw-r--r--ranger/fsobject/fsobject.py9
-rw-r--r--ranger/gui/widgets/browsercolumn.py64
3 files changed, 76 insertions, 56 deletions
diff --git a/ranger/fsobject/file.py b/ranger/fsobject/file.py
index 4618df33..1d25e7d8 100644
--- a/ranger/fsobject/file.py
+++ b/ranger/fsobject/file.py
@@ -13,11 +13,47 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import re
+import stat
+import zipfile
+from StringIO import StringIO
+
 N_FIRST_BYTES = 20
 control_characters = set(chr(n) for n in
 		set(range(0, 9)) | set(range(14, 32)))
 
 from ranger.fsobject import FileSystemObject
+
+# Don't even try to preview files which mach this regular expression:
+PREVIEW_BLACKLIST = re.compile(r"""
+		# look at the extension:
+		\.(
+			# one character extensions:
+				[oa]
+			# media formats:
+				| avi | [mj]pe?g | mp\d | og[gmv] | wm[av] | mkv | flv
+				| png | bmp | vob | wav | mpc | flac | divx? | xcf | pdf
+			# binary files:
+				| torrent | class | so | img | py[co] | dmg
+			# containers:
+				| iso | rar | 7z | tar | gz | bz2 | tgz
+		)
+		# ignore filetype-independent suffixes:
+			(\.part|\.bak|~)?
+		# ignore fully numerical file extensions:
+			(\.\d+)*?
+		$
+""", re.VERBOSE | re.IGNORECASE)
+
+PREVIEW_WHITELIST = re.compile(r"""
+		\.(
+			txt | py | c
+		)
+		# ignore filetype-independent suffixes:
+			(\.part|\.bak|~)?
+		$
+""", re.VERBOSE | re.IGNORECASE)
+
 class File(FileSystemObject):
 	is_file = True
 
@@ -38,3 +74,26 @@ class File(FileSystemObject):
 		if self.firstbytes and control_characters & set(self.firstbytes):
 			return True
 		return False
+
+	def has_preview(self):
+		if not (self.is_file \
+				and self.accessible \
+				and self.stat \
+				and not self.is_device \
+				and not self.stat.st_mode & stat.S_IFIFO):
+			return False
+
+		if PREVIEW_WHITELIST.search(self.basename):
+			return True
+		if PREVIEW_BLACKLIST.search(self.basename):
+			return False
+		if self.extension not in ('zip',) and self.is_binary():
+			return False
+		return True
+
+	def get_preview_source(self):
+		if self.extension == 'zip':
+			contents = '\n'.join(zipfile.ZipFile(self.path).namelist())
+			return StringIO(contents)
+		return open(self.path, 'r')
+
diff --git a/ranger/fsobject/fsobject.py b/ranger/fsobject/fsobject.py
index afef48e4..d570d009 100644
--- a/ranger/fsobject/fsobject.py
+++ b/ranger/fsobject/fsobject.py
@@ -19,7 +19,7 @@ CONTAINER_EXTENSIONS = ('7z', 'ace', 'ar', 'arc', 'bz', 'bz2', 'cab', 'cpio',
 
 from os import access, listdir, lstat, readlink, stat
 from time import time
Drawable)
}

func NewBordered(content Drawable, borders uint) *Bordered {
	b := &Bordered{
		borders: borders,
		content: content,
	}
	content.OnInvalidate(b.contentInvalidated)
	return b
}

func (bordered *Bordered) contentInvalidated(d Drawable) {
	bordered.Invalidate()
}

func (bordered *Bordered) Children() []Drawable {
	return []Drawable{bordered.content}
}

func (bordered *Bordered) Invalidate() {
	bordered.DoInvalidate(bordered)
}

func (bordered *Bordered) Draw(ctx *Context) {
	x := 0
	y := 0
	width := ctx.Width()
	height := ctx.Height()
	style := tcell.StyleDefault.Reverse(true)
	if bordered.borders&BORDER_LEFT != 0 {
		ctx.Fill(0, 0, 1, ctx.Height(), ' ', style)
		x += 1
		width -= 1
	}
	if bordered.borders&BORDER_TOP != 0 {
		ctx.Fill(0, 0, ctx.Width(), 1, ' ', style)
		y += 1
		height -= 1
	}
	if bordered.borders&BORDER_RIGHT != 0 {
		ctx.Fill(ctx.Width()-1, 0, 1, ctx.Height(), ' ', style)
		width -= 1
	}
	if bordered.borders&BORDER_BOTTOM != 0 {
		ctx.Fill(0, ctx.Height()-1, ctx.Width(), 1, ' ', style)
		height -= 1
	}
	subctx := ctx.Subcontext(x, y, width, height)
	bordered.content.Draw(subctx)
}