summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-03-28 01:33:35 +0100
committerhut <hut@lavabit.com>2010-03-28 01:33:35 +0100
commitd3c262a924eebdab926b9569ecdb225b3be1e4a9 (patch)
treedb25013d7155e3b8bb3ac4f4b2a11d04170dd06a
parent103a23b89123e93c0cc651b91b1740797f397bc3 (diff)
downloadranger-d3c262a924eebdab926b9569ecdb225b3be1e4a9.tar.gz
closed issue #71
-rw-r--r--TODO2
-rw-r--r--ranger/fsobject/file.py25
-rw-r--r--ranger/gui/widgets/browsercolumn.py25
3 files changed, 48 insertions, 4 deletions
diff --git a/TODO b/TODO
index b1089225..7da64db5 100644
--- a/TODO
+++ b/TODO
@@ -48,7 +48,7 @@ General
    ( ) #64  10/02/25  scroll in previews
    (X) #66  10/02/28  explain how colorschemes work
    ( ) #70  10/03/14  mouse handler for titlebar
-   ( ) #71  10/03/21  previews: black/whitelist + read file
+   (X) #71  10/03/21  previews: black/whitelist + read file
 
 
 Bugs
diff --git a/ranger/fsobject/file.py b/ranger/fsobject/file.py
index 86621095..15bfc18b 100644
--- a/ranger/fsobject/file.py
+++ b/ranger/fsobject/file.py
@@ -13,6 +13,31 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+control_characters = set(chr(n) for n in set(range(0, 9)) | set(range(14,32)))
+
 from .fsobject import FileSystemObject as SuperClass
 class File(SuperClass):
 	is_file = True
+
+	@property
+	def first4bytes(self):
+		try:
+			return self._first4bytes
+		except:
+			try:
+				f = open(self.path, 'r')
+				self._first4bytes = f.read(4)
+				f.close()
+				return self._first4bytes
+			except:
+				pass
+
+	def is_binary(self):
+		if not self.first4bytes:
+			return
+		if self.first4bytes == "\x7F\x45\x4C\x46":
+			return True
+		if control_characters & set(self.first4bytes):
+			return True
+		return False
+
diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py
index 238a4803..06a89627 100644
--- a/ranger/gui/widgets/browsercolumn.py
+++ b/ranger/gui/widgets/browsercolumn.py
@@ -41,6 +41,15 @@ PREVIEW_BLACKLIST = re.compile(r"""
 		$
 """, re.VERBOSE | re.IGNORECASE)
 
+PREVIEW_WHITELIST = re.compile(r"""
+		\.(
+			txt | py | c
+		)
+		# ignore filetype-independent suffixes:
+			(\.part|\.bak|~)?
+		$
+""", re.VERBOSE | re.IGNORECASE)
+
 class BrowserColumn(Pager):
 	main_column = False
 	display_infostring = False
@@ -149,10 +158,20 @@ class BrowserColumn(Pager):
 			self.last_redraw_time = time()
 
 	def _preview_this_file(self, target):
+		if not self.settings.preview_files:
+			return False
+		if not self.target or not self.target.is_file:
+			return False
 		maxsize = self.settings.max_filesize_for_preview
-		return self.settings.preview_files \
-				and not PREVIEW_BLACKLIST.search(target.basename) \
-				and (maxsize is None or maxsize >= target.size)
+		if maxsize is not None and target.size > maxsize:
+			return False
+		if PREVIEW_WHITELIST.search(target.basename):
+			return True
+		if PREVIEW_BLACKLIST.search(target.basename):
+			return False
+		if target.is_binary():
+			return False
+		return True
 
 	def _draw_file(self):
 		"""Draw a preview of the file, if the settings allow it"""
<hut@lavabit.com> 2010-02-27 04:39:54 +0100 Makefile: added make info + some improvements' href='/akspecs/ranger/commit/Makefile?h=v1.9.0b1&id=2c5ea01dd2003f172bdc5afdb8bb8bf34eccf0fa'>2c5ea01d ^
b0a216f5 ^








ec598fc7 ^








b0a216f5 ^










94c5d83e ^


dee6cfa6 ^
e9e4b4ff ^
b0a216f5 ^
3a1e1f28 ^
b0a216f5 ^

e9e4b4ff ^
3a1e1f28 ^
e9e4b4ff ^





e9e4b4ff ^
3a1e1f28 ^
50a0cb1c ^
e9e4b4ff ^
fde932f2 ^
85fd5288 ^
4ade06a6 ^

85fd5288 ^
0c2c782d ^

e278a003 ^
b0a216f5 ^
4ade06a6 ^
b0a216f5 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116