summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-27 19:32:50 +0100
committerhut <hut@lavabit.com>2009-12-27 19:32:50 +0100
commitf257e4761b725bdfed9c8d9589048fc5e3969b53 (patch)
tree4e9c2efdd50f979aa7a335a622cee2016bde7d0c
parentef0ee84300be230ab5ef417f4a303d07e5b5f0b9 (diff)
downloadranger-f257e4761b725bdfed9c8d9589048fc5e3969b53.tar.gz
use a pager widget for file previews
-rw-r--r--ranger/gui/widgets/filelist.py24
-rw-r--r--ranger/gui/widgets/pager.py71
2 files changed, 88 insertions, 7 deletions
diff --git a/ranger/gui/widgets/filelist.py b/ranger/gui/widgets/filelist.py
index 95fcf8fe..d8fc1073 100644
--- a/ranger/gui/widgets/filelist.py
+++ b/ranger/gui/widgets/filelist.py
@@ -1,7 +1,9 @@
 """The FileList widget displays the contents of a directory or file."""
 from . import Widget
+from ..displayable import DisplayableContainer
+from .pager import Pager
 
-class FileList(Widget):
+class FileList(Widget, DisplayableContainer):
 	main_display = False
 	display_infostring = False
 	scroll_begin = 0
@@ -10,8 +12,14 @@ class FileList(Widget):
 	tagged_marker = '*'
 
 	def __init__(self, win, level):
-		Widget.__init__(self, win)
+		DisplayableContainer.__init__(self, win)
+		self.pager = Pager(win)
+		self.add_obj(self.pager)
 		self.level = level
+	
+	def resize(self, *args):
+		DisplayableContainer.resize(self, *args)
+		self.pager.resize(*args)
 
 	def click(self, event):
 		"""Handle a MouseEvent"""
@@ -66,6 +74,7 @@ class FileList(Widget):
 		from ranger.fsobject.file import File
 		from ranger.fsobject.directory import Directory
 
+		self.pager.visible = False
 		if self.target is None:
 			pass
 		elif type(self.target) == File:
@@ -73,6 +82,8 @@ class FileList(Widget):
 		elif type(self.target) == Directory:
 			self.draw_directory()
 
+		DisplayableContainer.draw(self)
+
 	def finalize(self):
 		if self.postpone_drawing:
 			self.target.load_content_if_outdated()
@@ -92,13 +103,12 @@ class FileList(Widget):
 			return
 		
 		try:
-			if self.target.size < 1024 * 20:
-				f = open(self.target.path, 'r')
-				for line in range(self.hei):
-					read = f.readline().expandtabs()
-					self.win.addnstr(self.y + line, self.x, read, self.wid)
+			f = open(self.target.path, 'r')
 		except:
 			pass
+		else:
+			self.pager.visible = True
+			self.pager.set_source(f)
 
 	def draw_directory(self):
 		"""Draw the contents of a directory"""
diff --git a/ranger/gui/widgets/pager.py b/ranger/gui/widgets/pager.py
new file mode 100644
index 00000000..7a93a2d6
--- /dev/null
+++ b/ranger/gui/widgets/pager.py
@@ -0,0 +1,71 @@
+"""
+The pager displays text and allows you to scroll inside it.
+"""
+from ranger import log
+from . import Widget
+
+class Pager(Widget):
+	source = None
+	source_is_stream = False
+	def __init__(self, win):
+		Widget.__init__(self, win)
+		self.scroll_begin = 0
+
+#		self.commandlist = CommandList()
+#		self.settings.keys.initialize_pager_commands( \
+#				self.commandlist)
+	
+	def draw(self):
+		line_gen = self._generate_lines(
+				starty=self.scroll_begin, startx=0)
+
+		for line, i in zip(line_gen, range(self.hei)):
+			y, x = self.y + i, self.x
+
+			try:
+				self.win.addstr(y, x, line)
+			except:
+				pass
+	
+	def set_source(self, source):
+		if self.source and self.source_is_stream:
+			self.source.close()
+
+		if hasattr(source, '__getitem__'):
+			self.source_is_stream = True
+			self.lines = source
+		elif hasattr(source, 'readline'):
+			self.source_is_stream = True
+			self.lines = []
+		else:
+			self.source = None
+			self.source_is_stream = False
+			return False
+
+		self.source = source
+		return True
+	
+	def _get_line(self, n, attempt_to_read=True):
+		try:
+			return self.lines[n]
+		except (KeyError, IndexError):
+			if attempt_to_read and self.source_is_stream:
+				for l in self.source:
+					self.lines.append(l)
+					if len(self.lines) > n:
+						break
+				return self._get_line(n, attempt_to_read=False)
+			return ""
+	
+	def _generate_lines(self, starty, startx):
+		i = starty
+		if not self.source:
+			raise StopIteration
+		while True:
+			try:
+				line = self._get_line(i).expandtabs(4)
+				line = line[startx:self.wid - 1 + startx].rstrip()
+				yield line
+			except IndexError:
+				raise StopIteration
+			i += 1
lame/lynx_help/keystrokes/option_help.html?id=a2e9461739dd215db90a5cee2c22a74e5f57d151'>^
d3f9d547 ^













d97a6513 ^






d3f9d547 ^










































































d97a6513 ^




























































a2e94617 ^





















d97a6513 ^



a2e94617 ^



d3f9d547 ^

2a256611 ^
d3f9d547 ^









d97a6513 ^













d3f9d547 ^




































d3f9d547 ^




























08fc6e5c ^
d3f9d547 ^






08fc6e5c ^
d3f9d547 ^




















e087f6d4

d3f9d547 ^
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