diff options
author | hut <hut@lavabit.com> | 2010-08-28 08:01:22 +0200 |
---|---|---|
committer | hut <hut@lavabit.com> | 2010-08-28 08:01:22 +0200 |
commit | a02baa3311a469a021ed0f801d3a743ecb1ace41 (patch) | |
tree | f85ee42b596502123dc4e30ded3d098d38f7a4f7 | |
parent | 0a8001b2a141b141e6e34b1ebcce316288ef7ab7 (diff) | |
download | ranger-a02baa3311a469a021ed0f801d3a743ecb1ace41.tar.gz |
Changed implementation of container.history
-rw-r--r-- | ranger/container/history.py | 131 | ||||
-rw-r--r-- | test/tc_history.py | 16 |
2 files changed, 80 insertions, 67 deletions
diff --git a/ranger/container/history.py b/ranger/container/history.py index 5d4da07a..9a3d6c4a 100644 --- a/ranger/container/history.py +++ b/ranger/container/history.py @@ -13,93 +13,106 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from collections import deque - class HistoryEmptyException(Exception): pass class History(object): - def __init__(self, maxlen = None): - self.history = deque(maxlen = maxlen) - self.history_forward = deque(maxlen = maxlen) + def __init__(self, maxlen=None): + self._history = [] + self._index = 0 + self.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 modify(self, item): + # Remove Duplicates + try: + self._history.remove(item) + except: + pass + # Remove first if list is too long + if len(self._history) > self.maxlen - 1: + del self._history[0] + # Append the item and fast forward + self._history.append(item) + self._index = len(self._history) - 1 + + def modify(self, item, unique=False): + if self._history and unique: + try: + self._history.remove(item) + self._index -= 1 + except: + pass try: - self.history[-1] = item + self._history[self._index] = item except IndexError: - raise HistoryEmptyException + self.add(item) def __len__(self): - return len(self.history) + return len(self._history) def current(self): - try: - return self.history[-1] - except IndexError: - raise HistoryEmptyException() + if self._history: + return self._history[self._index] + else: + raise HistoryEmptyException def top(self): try: - return self.history_forward[-1] + return self._history[-1] except IndexError: - try: - return self.history[-1] - except IndexError: - raise HistoryEmptyException() + raise HistoryEmptyException() def bottom(self): try: - return self.history[0] + return self._history[0] except IndexError: raise HistoryEmptyException() def back(self): - if len(self.history) > 1: - self.history_forward.appendleft( self.history.pop() ) - return self.current() - - def move(self, n, pattern=None): - if n > 0: - return self.forward() - if n < 0: - return self.back() + self._index -= 1 + if self._index < 0: + self._index = 0 + + def move(self, n): + self._index += n + if self._index > len(self._history) - 1: + self._index = len(self._history) - 1 + if self._index < 0: + self._index = 0 + + def search(self, string, n): + if n != 0 and string: + step = n > 0 and 1 or -1 + i = self._index + steps_left = steps_left_at_start = int(abs(n)) + while steps_left: + i += step + if i >= len(self._history) or i < 0: + break + if self._history[i].startswith(string): + steps_left -= 1 + if steps_left != steps_left_at_start: + self._index = i def __iter__(self): - return self.history.__iter__() + return self._history.__iter__() def next(self): - return self.history.next() + return self._history.next() def forward(self): - if len(self.history_forward) > 0: - self.history.append( self.history_forward.popleft() ) - return self.current() + if self._history: + self._index += 1 + if self._index > len(self._history) - 1: + self._index = len(self._history) - 1 + else: + self._index = 0 def fast_forward(self): - if self.history_forward: - self.history.extend(self.history_forward) - self.history_forward.clear() - - def unique(self): - found = [] - i = len(self.history) - while i: - i -= 1 - item = self.history[i] - if item in found: - del self.history[i] - else: - found.append(item) - i = len(self.history_forward) - while i: - i -= 1 - item = self.history_forward[i] - if item in found: - del self.history_forward[i] - else: - found.append(item) + if self._history: + self._index = len(self._history) - 1 + else: + self._index = 0 + + def _left(self): # used for unit test + return self._history[0:self._index+1] diff --git a/test/tc_history.py b/test/tc_history.py index b1161f2a..301ebedd 100644 --- a/test/tc_history.py +++ b/test/tc_history.py @@ -27,13 +27,13 @@ class Test(TestCase): hist.back() self.assertEqual(4, hist.current()) - self.assertEqual([3,4], list(hist)) + self.assertEqual([3,4], list(hist._left())) self.assertEqual(5, hist.top()) hist.back() self.assertEqual(3, hist.current()) - self.assertEqual([3], list(hist)) + self.assertEqual([3], list(hist._left())) # no change if current == bottom self.assertEqual(hist.current(), hist.bottom()) @@ -46,31 +46,31 @@ class Test(TestCase): hist.forward() hist.forward() self.assertEqual(5, hist.current()) - self.assertEqual([3,4,5], list(hist)) + self.assertEqual([3,4,5], list(hist._left())) self.assertEqual(3, hist.bottom()) hist.add(6) self.assertEqual(4, hist.bottom()) - self.assertEqual([4,5,6], list(hist)) + self.assertEqual([4,5,6], list(hist._left())) hist.back() hist.fast_forward() - self.assertEqual([4,5,6], list(hist)) + self.assertEqual([4,5,6], list(hist._left())) hist.back() hist.back() hist.fast_forward() - self.assertEqual([4,5,6], list(hist)) + self.assertEqual([4,5,6], list(hist._left())) hist.back() hist.back() hist.back() hist.fast_forward() - self.assertEqual([4,5,6], list(hist)) + self.assertEqual([4,5,6], list(hist._left())) hist.back() hist.back() hist.back() hist.back() hist.fast_forward() - self.assertEqual([4,5,6], list(hist)) + self.assertEqual([4,5,6], list(hist._left())) if __name__ == '__main__': main() |