summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-04 18:46:42 +0100
committerhut <hut@lavabit.com>2009-12-04 18:46:42 +0100
commit6874e0ea3059b0f394ca9cf5929cb82c3fe4a09d (patch)
treed7165e3f357fb0d208310f965db8df87a5403e6e /ranger
parentcc952d63d81e410d1c7dd3bfbe91ee6cfdd3d7a8 (diff)
downloadranger-6874e0ea3059b0f394ca9cf5929cb82c3fe4a09d.tar.gz
implemented Bookmarks
Diffstat (limited to 'ranger')
-rw-r--r--ranger/bookmark.py65
-rw-r--r--ranger/conf/keys.py11
-rw-r--r--ranger/fm.py22
-rw-r--r--ranger/main.py5
4 files changed, 99 insertions, 4 deletions
diff --git a/ranger/bookmark.py b/ranger/bookmark.py
new file mode 100644
index 00000000..e9bfcee1
--- /dev/null
+++ b/ranger/bookmark.py
@@ -0,0 +1,65 @@
+from string import ascii_letters, digits
+ALLOWED_KEYS = ascii_letters + digits + "`'"
+
+class NonexistantBookmark(Exception):
+	pass
+
+class Bookmarks(object):
+	def __init__(self, path = None):
+		import string, re, os
+		self.dct = {}
+		if path is None:
+			self.path = os.path.expanduser("~/.ranger/bookmarks")
+		self.load_pattern = re.compile(r"^[\d\w`']:.")
+		self.enter_dir_function = None
+
+	def load(self):
+		import os
+		self.dct.clear()
+
+		if os.access(self.path, os.R_OK):
+			f = open(self.path, 'r')
+			for line in f:
+				if self.load_pattern.match(line):
+					key, value = line[0], line[2:-1]
+					if key in ALLOWED_KEYS: 
+						self.dct[key] = value
+
+			f.close()
+
+	def enter(self, key):
+		if self.enter_dir_function is not None:
+			self.enter_dir_function(self[key])
+		else:
+			raise RuntimeError('Not specified how to enter a directory')
+
+	def remember(self, value):
+		self["`"] = value
+		self["'"] = value
+
+	def __getitem__(self, key):
+		if key in self.dct:
+			return self.dct[key]
+		else:
+			raise NonexistantBookmark()
+
+	def __setitem__(self, key, value):
+		if key in ALLOWED_KEYS:
+			self.dct[key] = value
+			self.save()
+
+	def __contains__(self, key):
+		return key in self.dct
+
+	def save(self):
+		import os
+		if os.access(self.path, os.W_OK):
+			f = open(self.path, 'w')
+
+			for key, value in self.dct.items():
+				if type(key) == str\
+						and type(value) == str \
+						and key in ALLOWED_KEYS:
+					f.write("{0}:{1}\n".format(str(key), str(value)))
+
+			f.close()
diff --git a/ranger/conf/keys.py b/ranger/conf/keys.py
index b9816919..9a9bcff3 100644
--- a/ranger/conf/keys.py
+++ b/ranger/conf/keys.py
@@ -1,6 +1,7 @@
 def initialize_commands(cl):
 	from ranger.fm import FM
 	from curses.ascii import ctrl
+	from ranger.bookmark import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS
 	import curses
 
 	# syntax for binding keys: cl.bind(fnc, *keys)
@@ -54,6 +55,11 @@ def initialize_commands(cl):
 	cl.bind(cd("~/.trash"),   'gt')
 	cl.bind(cd("/srv"),       'gs')
 
+	# bookmarks
+	for key in ALLOWED_BOOKMARK_KEYS:
+		cl.bind(c(FM.enter_bookmark, key),   "`" + key, "'" + key)
+		cl.bind(c(FM.set_bookmark, key),     "m" + key)
+
 	# system functions
 	cl.bind(FM.exit,         ctrl('D'), 'q', 'ZZ')
 	cl.bind(FM.reset,        ctrl('R'))
@@ -66,6 +72,11 @@ def initialize_commands(cl):
 	cl.bind(curry(FM.open_console, '!'), '!')
 	cl.bind(curry(FM.open_console, '@'), 'r')
 
+	def test(fm):
+		from ranger.helper import log
+		log(fm.bookmarks.dct)
+	cl.bind(test, 'x')
+
 	cl.rebuild_paths()
 
 
diff --git a/ranger/fm.py b/ranger/fm.py
index 95110455..3f36045d 100644
--- a/ranger/fm.py
+++ b/ranger/fm.py
@@ -3,15 +3,17 @@ from ranger.conf.apps import CustomApplications as Applications
 null = open(devnull, 'a')
 
 class FM():
-	def __init__(self, environment, ui):
+	def __init__(self, environment, ui, bookmarks):
 		self.env = environment
 		self.ui = ui
 		self.apps = Applications()
+		self.bookmarks = bookmarks
+		self.bookmarks.enter_dir_function = self.enter_dir
 
 	def run(self):
 		self.env.enter_dir(self.env.path)
 
-		while 1:
+		while True:
 			try:
 				self.ui.draw()
 				key = self.ui.get_next_key()
@@ -38,9 +40,23 @@ class FM():
 	def enter_dir(self, path):
 		self.env.enter_dir(path)
 
+	def enter_bookmark(self, key):
+		from ranger.bookmark import NonexistantBookmark
+		try:
+			destination = self.bookmarks[key]
+			current_path = self.env.pwd.path
+			if destination != current_path:
+				self.bookmarks.enter(key)
+				self.bookmarks.remember(current_path)
+		except NonexistantBookmark:
+			pass
+
+	def set_bookmark(self, key):
+		self.bookmarks[key] = self.env.pwd.path
+
 	def move_left(self):
 		self.env.enter_dir('..')
-
+	
 	def move_right(self, mode = 0):
 		cf = self.env.cf
 		if not self.env.enter_dir(cf):
diff --git a/ranger/main.py b/ranger/main.py
index 14953682..e6718122 100644
--- a/ranger/main.py
+++ b/ranger/main.py
@@ -6,6 +6,7 @@ from optparse import OptionParser, SUPPRESS_HELP
 from ranger.fm import FM
 from ranger.environment import Environment
 from ranger.command import CommandList
+from ranger.bookmark import Bookmarks
 from ranger.conf import keys, options
 from ranger.gui.defaultui import DefaultUI as UI
 from ranger.conf.colorschemes.snow import MyColorScheme
@@ -66,9 +67,11 @@ def main():
 	commandlist = CommandList()
 	colorscheme = MyColorScheme()
 	keys.initialize_commands(commandlist)
+	bookmarks = Bookmarks()
+	bookmarks.load()
 
 	my_ui = UI(env, commandlist, colorscheme)
-	my_fm = FM(env, my_ui)
+	my_fm = FM(env, my_ui, bookmarks)
 
 	try:
 		# Run the file manager
artik.com> 2015-05-06 00:19:03 -0700 committer Kartik K. Agaram <vc@akkartik.com> 2015-05-06 00:19:03 -0700 1279 - colorized rendering of the source files' href='/akkartik/mu/commit/html/024compare.cc.html?h=hlt&id=672e3e50c6ed6de161e40aa256c3fc0f2b1f7cf9'>672e3e50 ^
672e3e50 ^
672e3e50 ^

65361948 ^

9570363a ^
65361948 ^




9570363a ^
65361948 ^


672e3e50 ^


d5d908dd ^
672e3e50 ^
9570363a ^


9542bb11 ^

65361948 ^
9570363a ^

9542bb11 ^



9570363a ^

65361948 ^


9570363a ^
65361948 ^

672e3e50 ^




9570363a ^

65361948 ^
672e3e50 ^
672e3e50 ^



9570363a ^

65361948 ^
672e3e50 ^
672e3e50 ^

65361948 ^

9570363a ^
65361948 ^




9570363a ^
65361948 ^


672e3e50 ^


d5d908dd ^
672e3e50 ^
9570363a ^


9542bb11 ^

65361948 ^
9570363a ^

9542bb11 ^



9570363a ^

65361948 ^


9570363a ^
65361948 ^

672e3e50 ^




9570363a ^

65361948 ^
672e3e50 ^
672e3e50 ^



9570363a ^

65361948 ^
672e3e50 ^
672e3e50 ^

65361948 ^

9570363a ^
65361948 ^




9570363a ^
65361948 ^


672e3e50 ^


d5d908dd ^
672e3e50 ^
9570363a ^


9542bb11 ^


9570363a ^

9542bb11 ^


65361948 ^
9570363a ^

65361948 ^


9570363a ^
65361948 ^

672e3e50 ^




9570363a ^


672e3e50 ^
672e3e50 ^



9570363a ^


672e3e50 ^
672e3e50 ^



9570363a ^


672e3e50 ^
672e3e50 ^

65361948 ^

9570363a ^
65361948 ^




9570363a ^
65361948 ^


672e3e50 ^


d5d908dd ^
672e3e50 ^
9570363a ^


9542bb11 ^


9570363a ^

9542bb11 ^


65361948 ^
9570363a ^

65361948 ^


9570363a ^
65361948 ^

672e3e50 ^




9570363a ^


672e3e50 ^
672e3e50 ^



9570363a ^


672e3e50 ^
672e3e50 ^



9570363a ^


672e3e50 ^
672e3e50 ^
65361948 ^


9570363a ^
65361948 ^




9570363a ^
65361948 ^

672e3e50 ^



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