set_sensitive() on Back and Forward when needed
[pysize.git] / pysize / core / history.py
blob3ea84055503620e467d30fb6acafd84770d7ddf7
1 from pysize.core.pysize_fs_tree import pysize_tree
2 from pysize.core.observable import observable
4 # [(fullpaths, name)]
5 history = []
6 next_insertion_index = 0
7 history_observable = observable()
9 def add_entry(tree):
10 global next_insertion_index
11 fp = tree.fullpaths
12 same = next_insertion_index and fp == history[next_insertion_index - 1][0]
13 if not same:
14 if next_insertion_index == len(history):
15 # History is being written
16 history.append(None)
17 else:
18 (paths, name) = history[next_insertion_index]
19 if fp != paths:
20 # History took another path, clear the remaining forward history
21 del history[next_insertion_index + 1:]
22 history[next_insertion_index] = (fp, tree.root.get_name())
23 next_insertion_index += 1
24 history_observable.fire_observers(next_insertion_index, history)
26 def get_entry(index):
27 if 0 <= index and index < len(history):
28 return history[index][0]
30 def move_history(delta):
31 global next_insertion_index
32 entry = get_entry(next_insertion_index - 1 + delta)
33 next_insertion_index += delta
34 history_observable.fire_observers(next_insertion_index, history)
35 return entry
37 def forward():
38 if len(history) > next_insertion_index:
39 return move_history(1)
41 def back():
42 if next_insertion_index > 1:
43 return move_history(-1)