summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/ext/direction.py150
1 files changed, 51 insertions, 99 deletions
diff --git a/ranger/ext/direction.py b/ranger/ext/direction.py
index 30eb87ce..1cdaa97b 100644
--- a/ranger/ext/direction.py
+++ b/ranger/ext/direction.py
@@ -13,109 +13,61 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-class NoDefault(object):
-	pass
-
-class Direction(object):
-	"""An object with a down and right method"""
-	def __init__(self, right=None, down=None, absolute=False,
-			percent=False, pages=False, **keywords):
-		self.has_explicit_direction = False
-
-		if 'up' in keywords:
-			self.down = -keywords['up']
-		else:
-			self.down = down
-
-		if 'left' in keywords:
-			self.right = -keywords['left']
-		else:
-			self.right = right
-
-		if 'relative' in keywords:
-			self.absolute = not relative
-		else:
-			self.absolute = absolute
-
-		if 'default' in keywords:
-			self.default = keywords['default']
-		else:
-			self.default = NoDefault
-
-		self.original_down = self.down
-		self.original_right = self.right
-
-		self.percent = percent
-		self.pages = pages
-	
-	@property
+"""
+Directions provide convenience methods for movement operations.
+
+Direction objects are handled just like dicts but provide
+methods like up() and down() which give you the correct value
+for the vertical direction, even if only the "up" or "down" key
+has been defined.
+
+Example application:
+d = Direction(down=5)
+print(d.up()) # prints -5
+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 copy(self):
+		return Direction(**self)
+
+	def _get_bool(self, first, second, fallback=None):
+		try: return self[first]
+		except:
+			try: return not self[second]
+			except: return fallback
+
+	def _get_direction(self, first, second, fallback=0):
+		try: return self[first]
+		except:
+			try: return -self[second]
+			except: return fallback
+
 	def up(self):
-		if self.down is None:
-			return None
-		return -self.down
+		return -Direction.down(self)
 
-	@property
-	def left(self):
-		if self.right is None:
-			return None
-		return -self.right
+	def down(self):
+		return Direction._get_direction(self, 'down', 'up')
 
-	@property
-	def relative(self):
-		return not self.absolute
+	def right(self):
+		return Direction._get_direction(self, 'right', 'left')
 
-	def down_or_default(self, default):
-		if self.has_been_modified:
-			return self.down
-		return default
+	def absolute(self):
+		return Direction._get_bool(self, 'absolute', 'relative')
 
-	def steps_down(self, page_length=10):
-		if self.pages:
-			return self.down * page_length
-		else:
-			return self.down
+	def left(self):
+		return -Direction.right(self)
 
-	def steps_right(self, page_length=10):
-		if self.pages:
-			return self.right * page_length
-		else:
-			return self.right
+	def relative(self):
+		return not Direction.absolute(self)
 
-	def copy(self):
-		new = type(self)()
-		new.__dict__.update(self.__dict__)
-		return new
-
-	def __mul__(self, other):
-		copy = self.copy()
-		if self.absolute:
-			if self.down is not None:
-				copy.down = other
-			if self.right is not None:
-				copy.right = other
-		else:
-			if self.down is not None:
-				copy.down *= other
-			if self.right is not None:
-				copy.right *= other
-		copy.original_down = self.original_down
-		copy.original_right = self.original_right
-		return copy
-	__rmul__ = __mul__
-
-	def __str__(self):
-		s = ['<Direction']
-		if self.down is not None:
-			s.append(" down=" + str(self.down))
-		if self.right is not None:
-			s.append(" right=" + str(self.right))
-		if self.absolute:
-			s.append(" absolute")
-		else:
-			s.append(" relative")
-		if self.pages:
-			s.append(" pages")
-		if self.percent:
-			s.append(" percent")
-		s.append('>')
-		return ''.join(s)
+	def vertical(self):
+		return set(self) & set(['up', 'down'])
+
+	def horizontal(self):
+		return set(self) & set(['left', 'right'])
'oid'>fe179145 ^
c6953a55 ^

fe179145 ^
5822dff7 ^
9506fb8e ^
fae694a0 ^
9506fb8e ^
a4570538 ^
fe179145 ^

fae694a0 ^
fe179145 ^
fae694a0 ^


4c05e43d ^

fae694a0 ^

fe179145 ^

fae694a0 ^




9506fb8e ^
fae694a0 ^
fae694a0 ^
1159f9ec ^



fe179145 ^
5822dff7 ^
9506fb8e ^
a4570538 ^

fae694a0 ^
fe179145 ^

fae694a0 ^
c6953a55 ^

fe179145 ^
fae694a0 ^
9506fb8e ^

f6f26231 ^
4c05e43d ^
f6f26231 ^







9506fb8e ^
fae694a0 ^

f6f26231 ^










4c05e43d ^

fae694a0 ^

fe179145 ^
5822dff7 ^

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