summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-02-10 02:30:11 +0100
committerhut <hut@lavabit.com>2010-03-09 14:40:19 +0100
commit4ba2112b6db4cd556708324473f34f12f16063ec (patch)
treeb85f92fca49e575f46bcec91896fc1136f741f97
parent95e3dfb1d6cdbb4b3501236d44be6ba1afe47462 (diff)
downloadranger-4ba2112b6db4cd556708324473f34f12f16063ec.tar.gz
keyparser: allow arbitrary number/order of directions
-rw-r--r--test/tc_newkeys.py100
1 files changed, 66 insertions, 34 deletions
diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py
index 2db9b160..8f6be3f5 100644
--- a/test/tc_newkeys.py
+++ b/test/tc_newkeys.py
@@ -71,45 +71,30 @@ class KeyBuffer(object):
 			return
 
 		# evaluate the command
-		if self.eval_command:
-			try:
-				self.tree_pointer = self.tree_pointer[key]
-			except TypeError:
-				print(self.tree_pointer)
-				self.failure = True
-				return None
-			except KeyError:
-				if DIRKEY in self.tree_pointer:
-					self.eval_command = False
-					self.eval_quantifier = True
-					self.tree_pointer = self.tree_pointer[DIRKEY]
-					self.dir_tree_pointer = self.direction_keys._tree
-				elif ANYKEY in self.tree_pointer:
-					self.moo.append(key)
-					self.tree_pointer = self.tree_pointer[ANYKEY]
-					self._try_to_finish()
-				else:
-					self.failure = True
-					return None
-			else:
-				self._try_to_finish()
+		if self.eval_command and self._do_eval_command(key):
+			return
 
+		# evaluate (the first number of) the direction-quantifier
 		if self.eval_quantifier and self._do_eval_quantifier(key):
 			return
 
 		# evaluate direction keys {j,k,gg,pagedown,...}
 		if not self.eval_command:
-			try:
-				self.dir_tree_pointer = self.dir_tree_pointer[key]
-			except KeyError:
-				self.failure = True
-			else:
-				if not isinstance(self.dir_tree_pointer, dict):
-					match = self.dir_tree_pointer
-					direction = match.actions['dir'] * self.direction_quant
-					self.directions.append(direction)
-					self.direction_quant = None
-					self._try_to_finish()
+			self._do_eval_direction(key)
+
+	def _do_eval_direction(self, key):
+		try:
+			self.dir_tree_pointer = self.dir_tree_pointer[key]
+		except KeyError:
+			self.failure = True
+		else:
+			if not isinstance(self.dir_tree_pointer, dict):
+				match = self.dir_tree_pointer
+				direction = match.actions['dir'] * self.direction_quant
+				self.directions.append(direction)
+				self.direction_quant = None
+				self.eval_command = True
+				self._try_to_finish()
 
 	def _do_eval_quantifier(self, key):
 		if self.eval_command:
@@ -123,9 +108,32 @@ class KeyBuffer(object):
 			setattr(self, attr, getattr(self, attr) * 10 + key - 48)
 		else:
 			self.eval_quantifier = False
-			return False
+			return None
 		return True
 
+	def _do_eval_command(self, key):
+		try:
+			self.tree_pointer = self.tree_pointer[key]
+		except TypeError:
+			print(self.tree_pointer)
+			self.failure = True
+			return None
+		except KeyError:
+			if DIRKEY in self.tree_pointer:
+				self.eval_command = False
+				self.eval_quantifier = True
+				self.tree_pointer = self.tree_pointer[DIRKEY]
+				self.dir_tree_pointer = self.direction_keys._tree
+			elif ANYKEY in self.tree_pointer:
+				self.moo.append(key)
+				self.tree_pointer = self.tree_pointer[ANYKEY]
+				self._try_to_finish()
+			else:
+				self.failure = True
+				return None
+		else:
+			self._try_to_finish()
+
 	def _try_to_finish(self):
 		if not isinstance(self.tree_pointer, dict):
 			self.command = self.tree_pointer
@@ -387,4 +395,28 @@ class Test(TestCase):
 		self.assertEqual(Ellipsis, press('ア'))
 		self.assertEqual(Ellipsis, press('9'))
 
+	def test_multiple_directions(self):
+		km = Keymap()
+		directions = Keymap()
+		kb = KeyBuffer(km, directions)
+		directions.add('j', dir=Direction(down=1))
+		directions.add('k', dir=Direction(down=-1))
+
+		def add_dirs(arg):
+			n = 0
+			for dir in arg.directions:
+				n += dir.down
+			return n
+
+		km.add(add_dirs, 'x}y}')
+		km.add(add_dirs, 'four}}}}')
+
+		press = self._mkpress(kb, km)
+
+		self.assertEqual(2, press('xjyj'))
+		self.assertEqual(0, press('fourjkkj'))
+		self.assertEqual(2, press('four2j4k2j2j'))
+		self.assertEqual(10, press('four1j2j3j4j'))
+		self.assertEqual(10, press('four1j2j3j4jafslkdfjkldj'))
+
 if __name__ == '__main__': main()
on' href='/akspecs/ranger/blame/doc/pydoc/ranger.gui.displayable.html?h=v1.2.0&id=2ca2e862e616eba862ac80f5ee0003d1aa984a32'>^
f07bb12f ^






4c13e1f2 ^
f07bb12f ^

4c13e1f2 ^
f07bb12f ^











4c13e1f2 ^
f07bb12f ^
4c13e1f2 ^

































f07bb12f ^




4c13e1f2 ^
f07bb12f ^
34a60763 ^
f07bb12f ^


62cd83ba ^

f07bb12f ^






4c13e1f2 ^

f07bb12f ^


4c13e1f2 ^

f07bb12f ^



4c13e1f2 ^


f07bb12f ^











f07bb12f ^















4c13e1f2 ^




34a60763 ^
4c13e1f2 ^
34a60763 ^
4c13e1f2 ^



f07bb12f ^
62cd83ba ^
f07bb12f ^






4c13e1f2 ^















f07bb12f ^





4c13e1f2 ^
f07bb12f ^
34a60763 ^
f07bb12f ^




4c13e1f2 ^
f07bb12f ^








f07bb12f ^



4c13e1f2 ^
f07bb12f ^


62cd83ba ^

f07bb12f ^




4c13e1f2 ^
f07bb12f ^
4c13e1f2 ^

f07bb12f ^



f07bb12f ^















4c13e1f2 ^

f07bb12f ^
4c13e1f2 ^
f07bb12f ^
34a60763 ^
f07bb12f ^
34a60763 ^
f07bb12f ^
4c13e1f2 ^
f07bb12f ^

4c13e1f2 ^
62cd83ba ^
4c13e1f2 ^

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