summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-05 16:53:12 +0100
committerhut <hut@lavabit.com>2009-12-05 16:53:12 +0100
commit22bb7e358ccd6efc58c38b8703b6818131905a75 (patch)
treea597a8c9302bbf19e08cf37dd2938264fad2c0f9 /ranger
parentffec67de733cfa2ee1c2dab6b87f933565180330 (diff)
downloadranger-22bb7e358ccd6efc58c38b8703b6818131905a75.tar.gz
History Object + Test Case
Diffstat (limited to 'ranger')
-rw-r--r--ranger/history.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/ranger/history.py b/ranger/history.py
new file mode 100644
index 00000000..bd9a575f
--- /dev/null
+++ b/ranger/history.py
@@ -0,0 +1,50 @@
+class HistoryEmptyException(Exception):
+	pass
+
+class History(object):
+	def __init__(self, maxlen = None):
+		from collections import deque
+		self.history = deque(maxlen = maxlen)
+		self.history_forward = deque(maxlen = maxlen)
+	
+	def add(self, item):
+		if len(self.history) == 0 or self.history[-1] != item:
+			self.history.append(item)
+			self.history_forward.clear()
+
+	def __len__(self):
+		return len(self.history)
+
+	def top(self):
+		try:
+			return self.history[-1]
+		except IndexError:
+			raise HistoryEmptyException()
+
+	def bottom(self):
+		try:
+			return self.history[0]
+		except IndexError:
+			raise HistoryEmptyException()
+
+	def back(self):
+		if len(self.history) > 1:
+			self.history_forward.append( self.history.pop() )
+		return self.top()
+
+	def move(self, n):
+		if n > 0:
+			return self.forward()
+		if n < 0:
+			return self.back()
+
+	def __iter__(self):
+		return self.history.__iter__()
+
+	def next(self):
+		return self.history.next()
+
+	def forward(self):
+		if len(self.history_forward) > 0:
+			self.history.append( self.history_forward.pop() )
+		return self.top()