summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/history.py50
-rw-r--r--test/tc_history.py44
2 files changed, 94 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()
diff --git a/test/tc_history.py b/test/tc_history.py
new file mode 100644
index 00000000..3e5adcff
--- /dev/null
+++ b/test/tc_history.py
@@ -0,0 +1,44 @@
+if __name__ == '__main__':
+	from os.path import abspath, join
+	import sys
+	sys.path.append(abspath(join(sys.path[0], '..')))
+
+from ranger.history import History
+from unittest import TestCase, main
+import unittest
+
+class Test(TestCase):
+	def test_everything(self):
+		hist = History(3)
+		for i in range(6):
+			hist.add(i)
+		self.assertEqual([3,4,5], list(hist))
+
+		hist.back()
+
+		self.assertEqual(4, hist.top())
+		self.assertEqual([3,4], list(hist))
+
+		hist.back()
+		self.assertEqual(3, hist.top())
+		self.assertEqual([3], list(hist))
+
+		# no change if top == bottom
+		self.assertEqual(hist.top(), hist.bottom())
+		last_top = hist.top()
+		hist.back()
+		self.assertEqual(hist.top(), last_top)
+
+
+		hist.forward()
+		hist.forward()
+		self.assertEqual(5, hist.top())
+		self.assertEqual([3,4,5], list(hist))
+
+
+		self.assertEqual(3, hist.bottom())
+		hist.add(6)
+		self.assertEqual(4, hist.bottom())
+		self.assertEqual([4,5,6], list(hist))
+
+if __name__ == '__main__': main()