summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-04-07 00:01:28 +0200
committerhut <hut@lavabit.com>2010-04-07 00:01:28 +0200
commitdcb079cb17350157754ab947141b183bc9fc4ca1 (patch)
tree88a507f865c8299fe596d8da2aa1d91b83bf599d
parent55d836d3fa05d27a93ed7da50c429c154d576b6e (diff)
downloadranger-dcb079cb17350157754ab947141b183bc9fc4ca1.tar.gz
core.actions: unified the movement functions
-rw-r--r--ranger/core/actions.py112
-rw-r--r--ranger/defaults/keys.py25
-rw-r--r--ranger/ext/direction.py22
3 files changed, 106 insertions, 53 deletions
diff --git a/ranger/core/actions.py b/ranger/core/actions.py
index b64ef316..d01397a7 100644
--- a/ranger/core/actions.py
+++ b/ranger/core/actions.py
@@ -18,6 +18,7 @@ import shutil
 from inspect import cleandoc
 
 import ranger
+from ranger.ext.direction import Direction
 from ranger.shared import EnvironmentAware, SettingsAware
 from ranger import fsobject
 from ranger.gui.widgets import console_mode as cmode
@@ -36,6 +37,29 @@ class Actions(EnvironmentAware, SettingsAware):
 
 	handle_mouse = resize = dummy
 
+	def move_left(self, narg=1):
+		"""Enter the parent directory"""
+		self.move(left=1, narg=narg)
+
+	def move_right(self, narg=None):
+		"""Enter the current directory or execute the current file"""
+		self.move(right=1, narg=narg)
+
+	def move_pointer(self, relative = 0, absolute = None, narg=None):
+		"""Move the pointer down by <relative> or to <absolute>"""
+		dct = dict(down=relative, narg=narg)
+		if absolute is not None:
+			dct['to'] = absolute
+		self.move(**dct)
+
+	def move_pointer_by_pages(self, relative):
+		"""Move the pointer down by <relative> pages"""
+		self.move(down=relative, pages=True)
+
+	def move_pointer_by_percentage(self, relative=0, absolute=None, narg=None):
+		"""Move the pointer down to <absolute>%"""
+		self.move(to=absolute, percentage=True, narg=narg)
+
 	# --------------------------
 	# -- Basic Commands
 	# --------------------------
@@ -95,48 +119,60 @@ class Actions(EnvironmentAware, SettingsAware):
 	# -- Moving Around
 	# --------------------------
 
-	def move_left(self, narg=1):
-		"""Enter the parent directory"""
-		try:
-			directory = os.path.join(*(['..'] * narg))
-		except:
-			return
-		self.env.enter_dir(directory)
-
-	def move_right(self, mode=0, narg=None):
-		"""Enter the current directory or execute the current file"""
-		cf = self.env.cf
-		sel = self.env.get_selection()
-
-		if isinstance(narg, int):
-			mode = narg
-		if not self.env.enter_dir(cf):
-			if sel:
-				if self.execute_file(sel, mode=mode) is False:
-					self.open_console(cmode.OPEN_QUICK)
-
-	def move_pointer(self, relative = 0, absolute = None, narg=None):
-		"""Move the pointer down by <relative> or to <absolute>"""
-		self.env.cwd.move(relative=relative,
-				absolute=absolute, narg=narg)
+	def move(self, narg=None, **kw):
+		"""
+		A universal movement method.
 
-	def move_pointer_by_pages(self, relative):
-		"""Move the pointer down by <relative> pages"""
-		self.env.cwd.move(relative=int(relative * self.env.termsize[0]))
+		Accepts these parameters:
+		(int) down, (int) up, (int) left, (int) right, (int) to,
+		(bool) absolute, (bool) relative, (bool) pages,
+		(bool) percentage
 
-	def move_pointer_by_percentage(self, relative=0, absolute=None, narg=None):
-		"""Move the pointer down by <relative>% or to <absolute>%"""
-		try:
-			factor = len(self.env.cwd) / 100.0
-		except:
-			return
+		to=X is translated to down=X, absolute=True
 
-		if narg is not None:
-			absolute = narg
+		Example:
+		self.move(down=4, pages=True)  # moves down by 4 pages.
+		self.move(to=2, pages=True)  # moves to page 2.
+		self.move(right=1, percentage=True)  # moves to 80%
+		"""
+		direction = Direction(kw)
+		if 'left' in direction:
+			steps = direction.left()
+			if narg is not None:
+				steps *= narg
+			try:
+				directory = os.path.join(*(['..'] * steps))
+			except:
+				return
+			self.env.enter_dir(directory)
+
+		elif 'right' in direction:
+			mode = direction.right()
+			if narg is not None:
+				mode = narg
+			cf = self.env.cf
+			selection = self.env.get_selection()
+			if not self.env.enter_dir(cf) and selection:
+				if self.execute_file(selection, mode=mode) is False:
+					self.open_console(cmode.OPEN_QUICK)
 
-		self.env.cwd.move(
-				relative=int(relative * factor),
-				absolute=int(absolute * factor))
+		elif direction.vertical():
+			if narg is not None:
+				if direction.absolute():
+					direction.set(narg)
+				else:
+					direction.multiply(narg)
+			if 'pages' in direction:
+				direction.multiply(self.env.termsize[0])
+			elif 'percentage' in direction:
+				factor = len(self.env.cwd) / 100.0
+				direction.multiply(factor)
+			dct = {}
+			if direction.absolute():
+				dct['absolute'] = int(direction.down())
+			else:
+				dct['relative'] = int(direction.down())
+			self.env.cwd.move(**dct)
 
 	def history_go(self, relative):
 		"""Move back and forth in the history"""
diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py
index 2e0e1eb7..12b943c6 100644
--- a/ranger/defaults/keys.py
+++ b/ranger/defaults/keys.py
@@ -60,19 +60,18 @@ def initialize_commands(map):
 	_vimlike_aliases(map)
 	map.alias(KEY_LEFT, KEY_BACKSPACE, DEL)
 
-	map(KEY_DOWN, fm.move_pointer(relative=1))
-	map(KEY_UP, fm.move_pointer(relative=-1))
-	map(KEY_RIGHT, KEY_ENTER, ctrl('j'), fm.move_right())
-	map(KEY_LEFT, KEY_BACKSPACE, DEL, fm.move_left(1))
-
-	map(KEY_HOME, fm.move_pointer(absolute=0))
-	map(KEY_END, fm.move_pointer(absolute=-1))
-
-	map('%', fm.move_pointer_by_percentage(absolute=50))
-	map(KEY_NPAGE, ctrl('f'), fm.move_pointer_by_pages(1))
-	map(KEY_PPAGE, ctrl('b'), fm.move_pointer_by_pages(-1))
-	map(ctrl('d'), 'J', fm.move_pointer_by_pages(0.5))
-	map(ctrl('u'), 'K', fm.move_pointer_by_pages(-0.5))
+	map(KEY_DOWN, fm.move(down=1))
+	map(KEY_UP, fm.move(up=1))
+	map(KEY_RIGHT, KEY_ENTER, ctrl('j'), fm.move(right=1))
+	map(KEY_LEFT, KEY_BACKSPACE, DEL, fm.move(left=1))
+	map(KEY_HOME, fm.move(to=0))
+	map(KEY_END, fm.move(to=-1))
+
+	map('%', fm.move(to=50, percentage=True))
+	map(KEY_NPAGE, ctrl('f'), fm.move(down=1, pages=True))
+	map(KEY_PPAGE, ctrl('b'), fm.move(up=1, pages=True))
+	map(ctrl('d'), 'J', fm.move(down=0.5, pages=True))
+	map(ctrl('u'), 'K', fm.move(up=0.5, pages=True))
 
 	map(']', fm.traverse())
 	map('[', fm.history_go(-1))
diff --git a/ranger/ext/direction.py b/ranger/ext/direction.py
index 1cdaa97b..8be4fcaa 100644
--- a/ranger/ext/direction.py
+++ b/ranger/ext/direction.py
@@ -30,8 +30,14 @@ print(bool(d.horizontal())) # False, since no horizontal direction is defined
 class Direction(dict):
 	__doc__ = __doc__  # for nicer pydoc
 
-	def __init__(self, **keywords):
-		dict.__init__(self, keywords)
+	def __init__(self, dictionary=None, **keywords):
+		if dictionary is not None:
+			dict.__init__(self, dictionary)
+		else:
+			dict.__init__(self, keywords)
+		if 'to' in self:
+			self['down'] = self['to']
+			self['absolute'] = True
 
 	def copy(self):
 		return Direction(**self)
@@ -71,3 +77,15 @@ class Direction(dict):
 
 	def horizontal(self):
 		return set(self) & set(['left', 'right'])
+
+	def multiply(self, n):
+		for key in ('up', 'right', 'down', 'left'):
+			try:
+				self[key] *= n
+			except:
+				pass
+
+	def set(self, n):
+		for key in ('up', 'right', 'down', 'left'):
+			if key in self:
+				self[key] = n
aa531b3d75a7d48d36e6ab4cb1'>^
4a4a392d ^

b1635a5c ^
4a4a392d ^




b1635a5c ^
4a4a392d ^









2104d1a7 ^
4a4a392d ^




















86351aaf ^
4a4a392d ^

b1635a5c ^
4a4a392d ^






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