1 from tord
.common
import test
, log
, timer
5 def __init__(self
, title
, func
, width
, align
):
12 def get_width(self
, show_title
):
13 if show_title
and len(self
.title
) > self
.width
: return len(self
.title
)
17 def format_data(self
, data
, width
):
18 return self
.align(self
.func(data
), width
)
21 def format_title(self
, width
):
22 return self
.align(self
.title
, width
)
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
]
37 def get_widths(self
, show_title
):
38 return [self
.cols
[k
].get_width(show_title
) for k
in self
.keys
]
41 def get_titles(self
, widths
):
42 return [self
.cols
[k
].format_title(w
) for k
, w
in zip(self
.keys
, widths
)]
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
)
54 self
.view
= self
.root
= None
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
]
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
)
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()
79 if sync
or reset
: self
.sync()
80 if update
: self
.update(self
.view
.get_data(args
))
82 selected
, sort
= restall
83 self
.selected(selected
)
89 def update(self
, data
):
91 self
.add(self
.view
.get_id(id), row
)
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
)
102 def text_cb(self
, data
):
103 return self
.view
.format_data(data
, self
.widths
)
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)
112 self
.refresh(update
=True)
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
))
121 def size_cb(self
, s
, d
):