summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-03 20:25:01 +0100
committerhut <hut@lavabit.com>2009-12-03 20:25:01 +0100
commitb810fe28876d750495ac05d9993ce02afb561c01 (patch)
tree8efbd9f7b4a0f795b7926ccaaa2ce870df4b4bed /ranger
parent4cb54d3819d8a58d0bd07b8da0c4a978807c44ec (diff)
downloadranger-b810fe28876d750495ac05d9993ce02afb561c01.tar.gz
mime type support, integrated into colorschemes
Diffstat (limited to 'ranger')
-rw-r--r--ranger/conf/colorschemes/snow.py16
-rw-r--r--ranger/fsobject.py35
-rw-r--r--ranger/gui/colorscheme.py1
-rw-r--r--ranger/gui/wdisplay.py2
-rw-r--r--ranger/mimetype.py14
5 files changed, 62 insertions, 6 deletions
diff --git a/ranger/conf/colorschemes/snow.py b/ranger/conf/colorschemes/snow.py
index 4bb00ca9..d5bd8c3a 100644
--- a/ranger/conf/colorschemes/snow.py
+++ b/ranger/conf/colorschemes/snow.py
@@ -17,14 +17,24 @@ class MyColorScheme(ColorScheme):
 			if context.empty or context.error:
 				bg = red
 
+			if context.media:
+				if context.image:
+					fg = yellow
+				else:
+					fg = magenta
+
+			if context.container:
+				fg = red
+
+			if context.document:
+				fg = default
+
 			if context.directory:
 				fg = blue
 			elif context.executable:
+				attr |= bold
 				fg = green
 
-			if context.media:
-				fg = magenta
-
 			if context.link:
 				fg = cyan
 
diff --git a/ranger/fsobject.py b/ranger/fsobject.py
index c4a1e882..35a6465a 100644
--- a/ranger/fsobject.py
+++ b/ranger/fsobject.py
@@ -8,6 +8,10 @@ T_NONEXISTANT = 'nonexistant'
 
 BAD_INFO = None
 
+CONTAINER_EXTENSIONS = 'rar zip tar gz bz bz2 tgz 7z iso cab'.split()
+DOCUMENT_EXTENSIONS = 'pdf doc ppt odt'.split()
+DOCUMENT_BASENAMES = 'README TODO LICENSE'.split()
+
 class FileSystemObject(object):
 
 	def __init__(self, path):
@@ -19,6 +23,10 @@ class FileSystemObject(object):
 		self.path = path
 		self.basename = basename(path)
 		self.dirname = dirname(path)
+		try:
+			self.extension = self.basename[self.basename.rindex('.') + 1:]
+		except ValueError:
+			self.extension = None
 		self.exists = False
 		self.accessible = False
 		self.marked = False
@@ -32,17 +40,40 @@ class FileSystemObject(object):
 		self.infostring = None
 		self.permissions = None
 		self.type = T_UNKNOWN
+
+		self.set_mimetype()
 	
 	def __str__(self):
 		return str(self.path)
 
+	def set_mimetype(self):
+		import ranger.mimetype as mimetype
+		try:
+			self.mimetype = mimetype.get() [self.extension]
+		except KeyError:
+			self.mimetype = ''
+
+		self.video = self.mimetype.startswith('video')
+		self.image = self.mimetype.startswith('image')
+		self.audio = self.mimetype.startswith('audio')
+		self.media = self.video or self.image or self.audio
+		self.document = self.mimetype.startswith('text') or (self.extension in DOCUMENT_EXTENSIONS) or (self.basename in DOCUMENT_BASENAMES)
+		self.container = self.extension in CONTAINER_EXTENSIONS
+
+		keys = ('video', 'audio', 'image', 'media', 'document', 'container')
+		self.mimetype_tuple = tuple(key for key in keys if getattr(self, key))
+
+		if self.mimetype == '':
+			self.mimetype = None
+
 	# load() reads useful information about the file from the file system
 	# and caches it in instance attributes.
 	def load(self):
-		self.loaded = True
-
 		import os
 		from ranger.helper import human_readable
+
+		self.loaded = True
+
 		if os.access(self.path, os.F_OK):
 			self.stat = os.stat(self.path)
 			self.islink = os.path.islink(self.path)
diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py
index 282a08ba..d7833016 100644
--- a/ranger/gui/colorscheme.py
+++ b/ranger/gui/colorscheme.py
@@ -2,6 +2,7 @@ CONTEXT_KEYS = [ 'reset', 'error',
 		'in_display', 'in_statusbar', 'in_titlebar', 'in_console',
 		'directory', 'file', 'hostname',
 		'executable', 'media', 'link',
+		'video', 'audio', 'image', 'media', 'document', 'container',
 		'broken', 'selected', 'empty', 'maindisplay']
 
 class ColorSchemeContext():
diff --git a/ranger/gui/wdisplay.py b/ranger/gui/wdisplay.py
index a1953001..07111437 100644
--- a/ranger/gui/wdisplay.py
+++ b/ranger/gui/wdisplay.py
@@ -107,7 +107,7 @@ class WDisplay(SuperClass):
 			except IndexError:
 				break
 
-			this_color = base_color[:]
+			this_color = base_color + list(drawed.mimetype_tuple)
 
 			if i == selected_i:
 				this_color.append('selected')
diff --git a/ranger/mimetype.py b/ranger/mimetype.py
new file mode 100644
index 00000000..4346d9c8
--- /dev/null
+++ b/ranger/mimetype.py
@@ -0,0 +1,14 @@
+types = {'not loaded': True}
+
+def load():
+	import sys, os, pickle
+	types.clear()
+
+	f = open(os.path.join(sys.path[0], 'data/mime.dat'), 'rb')
+	types.update(pickle.load(f))
+	f.close()
+
+def get():
+	if 'not loaded' in types:
+		load()
+	return types
403' href='#n403'>403 404 405 406 407