summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-06-03 23:01:49 +0200
committerhut <hut@lavabit.com>2010-06-03 23:14:47 +0200
commit43e0f44a4788f3251d5f2ad7c1bfeff014353aba (patch)
tree7fa3014ab4fe4408c2a6a42111f399a27c9fe46a
parentcfa5ab76e62d73af76f131cb82938faa069e332e (diff)
downloadranger-43e0f44a4788f3251d5f2ad7c1bfeff014353aba.tar.gz
added more code from David Barnett, previews work
-rw-r--r--ranger/core/actions.py9
-rwxr-xr-xranger/ext/preview.sh21
-rw-r--r--ranger/fsobject/file.py21
-rw-r--r--ranger/gui/ansi.py17
-rw-r--r--ranger/gui/curses_shortcuts.py9
-rw-r--r--ranger/gui/widgets/pager.py20
6 files changed, 76 insertions, 21 deletions
diff --git a/ranger/core/actions.py b/ranger/core/actions.py
index 14f862c7..2db749cd 100644
--- a/ranger/core/actions.py
+++ b/ranger/core/actions.py
@@ -468,13 +468,8 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware):
 		if not hasattr(self.ui, 'open_embedded_pager'):
 			return
 
-		try:
-			f = open(self.env.cf.path, 'r')
-		except:
-			pass
-		else:
-			pager = self.ui.open_embedded_pager()
-			pager.set_source(f)
+		pager = self.ui.open_embedded_pager()
+		pager.set_source(self.env.cf.get_preview_source())
 
 	# --------------------------
 	# -- Tabs
diff --git a/ranger/ext/preview.sh b/ranger/ext/preview.sh
new file mode 100755
index 00000000..80186e4d
--- /dev/null
+++ b/ranger/ext/preview.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+mimetype=$(file --mime-type -Lb "$1")
+basetype=$(echo "$mimetype" | grep -o '^[^/]\+')
+extension=$(echo "$1" | grep '\.' | grep -o '[^.]\+$')
+
+case "$basetype" in
+	text)
+		highlight --ansi "$1" || cat "$1"
+		exit 0;;
+	image)
+		img2txt "$1" || exit 1
+		exit 0;;
+esac
+
+case "$extension" in
+	zip|gz)
+		atool -l "$1"
+		exit 0;;
+esac
+
+exit 1
diff --git a/ranger/fsobject/file.py b/ranger/fsobject/file.py
index 2619fa35..25902f57 100644
--- a/ranger/fsobject/file.py
+++ b/ranger/fsobject/file.py
@@ -16,6 +16,9 @@
 import re
 import zipfile
 from ranger.fsobject import FileSystemObject
+from subprocess import Popen, PIPE
+from ranger.core.runner import devnull
+from ranger import relpath
 
 N_FIRST_BYTES = 20
 control_characters = set(chr(n) for n in
@@ -28,8 +31,8 @@ PREVIEW_BLACKLIST = re.compile(r"""
 			# 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
+				| avi | mpe?g | mp\d | og[gmv] | wm[av] | mkv | flv
+				| vob | wav | mpc | flac | divx? | xcf | pdf
 			# binary files:
 				| torrent | class | so | img | py[co] | dmg
 			# containers:
@@ -78,15 +81,21 @@ class File(FileSystemObject):
 			return False
 		if not self.accessible or self.is_fifo or self.is_device:
 			return False
+		if self.image or self.container:
+			return True
 		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():
+		if self.is_binary():
 			return False
 		return True
 
 	def get_preview_source(self):
-		if self.extension == 'zip':
-			return '\n'.join(zipfile.ZipFile(self.path).namelist())
-		return open(self.path, 'r')
+		try:
+			p = Popen([relpath('ext/preview.sh'), self.path],
+					stdout=PIPE, stderr=devnull)
+			if not p.poll():
+				return p.stdout
+		except:
+			return open(self.path, 'r')
diff --git a/ranger/gui/ansi.py b/ranger/gui/ansi.py
index fe0753a1..a9b37665 100644
--- a/ranger/gui/ansi.py
+++ b/ranger/gui/ansi.py
@@ -17,14 +17,15 @@
 from ranger.gui import color
 import re
 
-ansi_re = re.compile('(\033' + r'\[\d+(?:;\d+)*?[a-zA-Z])')
+ansi_re = re.compile('(\033' + r'\[\d*(?:;\d+)*?[a-zA-Z])')
+reset = '\033[0m'
 
 def split_ansi_from_text(ansi_text):
 	return ansi_re.split(ansi_text)
 
-def text_with_fg_bg(ansi_text):
+def text_with_fg_bg_attr(ansi_text):
 	for chunk in split_ansi_from_text(ansi_text):
-		if chunk[0] == '\033':
+		if chunk and chunk[0] == '\033':
 			if chunk[-1] != 'm':
 				continue
 			match = re.match(r'^.\[(.*).$', chunk)
@@ -33,9 +34,15 @@ def text_with_fg_bg(ansi_text):
 
 			# Convert arguments to attributes/colors
 			for arg in attr_args.split(';'):
-				n = int(arg)
+				try:
+					n = int(arg)
+				except:
+					if arg == '':
+						n = 0
+					else:
+						continue
 				if n == 0:
-					fg, bg, attr = 0, 0, 0
+					fg, bg, attr = -1, -1, 0
 				elif n == 1:
 					attr |= color.bold
 				elif n == 4:
diff --git a/ranger/gui/curses_shortcuts.py b/ranger/gui/curses_shortcuts.py
index 3df45700..42f9dada 100644
--- a/ranger/gui/curses_shortcuts.py
+++ b/ranger/gui/curses_shortcuts.py
@@ -1,4 +1,5 @@
 # Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
+# Copyright (C) 2010 David Barnett <davidbarnett2@gmail.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -13,9 +14,11 @@
 # 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 curses
 import _curses
 
 from ranger.ext.iter_tools import flatten
+from ranger.gui.color import get_color
 from ranger.shared import SettingsAware
 
 def ascii_only(string):
@@ -95,6 +98,12 @@ class CursesShortcuts(SettingsAware):
 		except _curses.error:
 			pass
 
+	def set_fg_bg_attr(self, fg, bg, attr):
+		try:
+			self.win.attrset(curses.color_pair(get_color(fg, bg)) | attr)
+		except _curses.error:
+			pass
+
 	def color_reset(self):
 		"""Change the colors to the default colors"""
 		CursesShortcuts.color(self, 'reset')
diff --git a/ranger/gui/widgets/pager.py b/ranger/gui/widgets/pager.py
index c0bc98b4..58fcdfd1 100644
--- a/ranger/gui/widgets/pager.py
+++ b/ranger/gui/widgets/pager.py
@@ -1,4 +1,5 @@
 # Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
+# Copyright (C) 2010 David Barnett <davidbarnett2@gmail.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -18,6 +19,7 @@ The pager displays text and allows you to scroll inside it.
 """
 import re
 from . import Widget
+from ranger.gui import ansi
 from ranger.ext.direction import Direction
 from ranger.container.keymap import CommandArgs
 
@@ -106,6 +108,13 @@ class Pager(Widget):
 
 			if TITLE_REGEXP.match(line):
 				self.color_at(i, 0, -1, 'title', *baseclr)
+		elif self.markup == 'ansi':
+			self.win.move(i, 0)
+			for chunk in ansi.text_with_fg_bg_attr(line):
+				if isinstance(chunk, tuple):
+					self.set_fg_bg_attr(*chunk)
+				else:
+					self.addstr(chunk)
 
 	def move(self, narg=None, **kw):
 		direction = Direction(kw)
@@ -158,12 +167,13 @@ class Pager(Widget):
 
 		if isinstance(source, str):
 			self.source_is_stream = False
-			self.lines = source.split('\n')
+			self.lines = source.splitlines()
 		elif hasattr(source, '__getitem__'):
 			self.source_is_stream = False
 			self.lines = source
 		elif hasattr(source, 'readline'):
 			self.source_is_stream = True
+			self.markup = 'ansi'
 			self.lines = []
 		else:
 			self.source = None
@@ -206,8 +216,12 @@ class Pager(Widget):
 		while True:
 			try:
 				line = self._get_line(i).expandtabs(4)
-				line = line[startx:self.wid + startx].rstrip()
-				yield line
+				if self.markup is 'ansi':
+					line = ansi.char_slice(line, startx, self.wid + startx) \
+							+ ansi.reset
+				else:
+					line = line[startx:self.wid + startx]
+				yield line.rstrip()
 			except IndexError:
 				raise StopIteration
 			i += 1
h=3.8&id=99785382ae6d4b20e8baf33542059e466e0b3c7c'>9978538 ^
b661426 ^
c53980c ^
c09bf8d ^
c09bf8d ^




ca65478 ^
c09bf8d ^
95766d6 ^
c53980c ^
dc5d967 ^
c09bf8d ^
1173723 ^

77f8c07 ^
6651dd7 ^
c09bf8d ^
da909dd ^
1173723 ^
c09bf8d ^
bcb07de ^
c09bf8d ^
67bc08d ^
b4d53bf ^
67bc08d ^

c09bf8d ^



e6cbe9c ^
95766d6 ^
b2cb925 ^
95766d6 ^
b2cb925 ^
1173723 ^

8af1d97 ^



c53980c ^
c09bf8d ^
c09bf8d ^


29355bd ^

ca65478 ^
29355bd ^

b9da4b0 ^
dc5d967 ^
b9da4b0 ^
29355bd ^
e995c1b ^



6d22782 ^
1836b67 ^



6d22782 ^

1836b67 ^



05fbbbd ^
e995c1b ^
29355bd ^

10885d3 ^

29355bd ^
6458d72 ^
7b5638f ^
d800ec0 ^
2272df9 ^
26157e6 ^
6b25d06 ^
6d22782 ^


9fce821 ^
91e569c ^

6b25d06 ^
6d22782 ^
b9da4b0 ^




ca65478 ^
4ad20ff ^
dc5d967 ^
439e15d ^

439e15d ^
b9da4b0 ^
7d7cde0 ^
2e836ec ^
6a39a49 ^







2e836ec ^
95e8d12 ^
2e836ec ^
95e8d12 ^






b4d53bf ^

6a39a49 ^
14d05e7 ^
fee8df6 ^


14d05e7 ^
b098c94 ^
5983c00 ^
95e8d12 ^









6a39a49 ^
95e8d12 ^
439e15d ^


ca65478 ^
439e15d ^


26e134b ^

439e15d ^


ca65478 ^
439e15d ^
dc5d967 ^
439e15d ^
b79b5fa ^
439e15d ^
2511b5c ^
3399650 ^
fde45eb ^
da2bbd3 ^
fde45eb ^


439e15d ^


ca65478 ^
83d2390 ^
dc5d967 ^
439e15d ^

59b4a5e ^
c0705ee ^
dba2306 ^
c0705ee ^
439e15d ^



ca65478 ^
0464e42 ^
adaa28a ^

dc5d967 ^
adaa28a ^

2ffdc19 ^
ca65478 ^

2ffdc19 ^
adaa28a ^



2ffdc19 ^
adaa28a ^


ca65478 ^
adaa28a ^

fde45eb ^



adaa28a ^


ca65478 ^
0f3acce ^







ca65478 ^
439e15d ^
dc5d967 ^
439e15d ^


439e15d ^




0053620 ^

439e15d ^


ca65478 ^
439e15d ^
dc5d967 ^

439e15d ^


3399650 ^
c09bf8d ^
dba2306 ^
b1701ad ^

3399650 ^


66da153 ^
901b3ed ^
5983c00 ^
3399650 ^

d2d394e ^
3399650 ^

c09bf8d ^
d2d394e ^
6092aa9 ^
c0705ee ^
3399650 ^

439e15d ^


ca65478 ^
439e15d ^


0053620 ^

439e15d ^
adaa28a ^
bf35794 ^
adaa28a ^








0f3acce ^
adaa28a ^





6b25d06 ^
0464e42 ^
adaa28a ^


0f3acce ^
adaa28a ^

adaa28a ^

3af6434 ^

ee31e38 ^
a73a882 ^
ee31e38 ^
a73a882 ^
adaa28a ^

b6ad663 ^

6b25d06 ^
b6ad663 ^







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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385