AUTO_LT_SYNC
[tore.git] / torc / list.py
blobbd0cef5089b1e5dfdc30e0ed3366fd71c65e21f1
1 from tord.common import test, log, timer
2 import pytx
4 class Column:
5 def __init__(self, title, func, width, align):
6 self.func = func
7 self.title = title
8 self.width = width
9 self.align = align
11 @test
12 def get_width(self, show_title):
13 if show_title and len(self.title) > self.width: return len(self.title)
14 return self.width
16 @test
17 def format_data(self, data, width):
18 return self.align(self.func(data), width)
20 @test
21 def format_title(self, width):
22 return self.align(self.title, width)
24 class Columns:
25 def __init__(self):
26 self.selected = None
27 self.cols = {}
29 @test
30 def add_column(self, key, column):
31 self.cols[key] = column
33 def swap_columns(self, a, b):
34 self.cols[a], self.cols[b] = self.cols[b], self.cols[a]
36 @test
37 def get_widths(self, show_title):
38 return [self.cols[k].get_width(show_title) for k in self.keys]
40 @test
41 def get_titles(self, widths):
42 return [self.cols[k].format_title(w) for k, w in zip(self.keys, widths)]
44 @test
45 def format_data(self, data, widths):
46 return [self.cols[k].format_data(d,w) for d, w, k in zip(data, widths, self.keys)]
48 class Views(pytx.List):
49 def __init__(self, flags, chop):
50 pytx.List.__init__(self, flags)
51 self.chop = chop
53 self.views = {}
54 self.view = self.root = None
56 @test
57 def view_add(self, view):
58 self.views[view.name] = view
59 if len(self.views) == 1:
60 self.view = self.root = self.views[view.name]
62 @test
63 def change_view(self, key, selected, store = False):
64 if store: self.view.restall = self.view.get_id(store), self.sort_get()
66 if key: self.view = self.views[key]
67 else: self.view = self.root
69 if selected: self.view.selected = selected
70 self.refresh(reset=True, clear=True, update=True, restall=self.view.restall)
72 @test
73 def refresh(self, remove=[], update=False, restall = False, clear=False, reset=False, sync=False, sort=True, args=None):
74 for h in remove: self.remove(self.view.get_id(h))
75 if clear: self.clear()
76 if reset:
77 self.reset()
78 self.sort_set(0,0)
79 if sync or reset: self.sync()
80 if update: self.update(self.view.get_data(args))
81 if restall:
82 selected, sort = restall
83 self.selected(selected)
84 self.sort_set(*sort)
85 if sort: self.sort()
86 self.draw()
88 @test
89 def update(self, data):
90 for id, row in data:
91 self.add(self.view.get_id(id), row)
93 @test
94 def sorting(self, by, max):
95 col, asc = self.sort_get()
96 if 0 < by < max: col = by-1
97 if not by: asc = not asc
98 self.sort_set(col, asc)
99 self.refresh()
101 @test
102 def text_cb(self, data):
103 return self.view.format_data(data, self.widths)
105 @test
106 def bind_cb(self, ch, id):
107 try: self.view.bindings[chr(ch)](self.view.get_hash(id), self)
108 except: self.sorting(ch-48, len(self.view.keys) + 1)
110 @test
111 def idle_cb(self):
112 self.refresh(update=True)
114 @test
115 def rsze_cb(self):
116 widths = self.view.get_widths(self.flag_get(pytx.TITLES))
117 self.widths_set(widths)
118 self.titles_set(self.view.get_titles(widths))
120 @test
121 def size_cb(self, s, d):
122 return d - self.chop