-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.py
More file actions
178 lines (158 loc) · 5.82 KB
/
table.py
File metadata and controls
178 lines (158 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import string_util as su
import colors
import component
import scrollbar
import math
class TableRow:
def __init__(self,
data: dict[str,str],
justify: dict[str,su.TextJustify],
column_separator: str = " "
):
self.data=data
self.justify=justify
self.column_separator=column_separator
def view(self, sizes: dict[str,int], order: list[str]) -> str:
s = ""
for col in order:
text = self.data[col]
width = sizes[col]
if self.justify[col] == su.TextJustify.Left:
s += su.ljust(text,width)
elif self.justify[col] == su.TextJustify.Center:
s += su.cjust(text,width)
elif self.justify[col] == su.TextJustify.Right:
s += su.rjust(text,width)
if order[-1] != col: # dont print the separator on the last column
s += self.column_separator
return s
class Table(component.Component):
def __init__(self,
parent: component.Component,
title: str,
height: int,
width: int,
columns: list[str],
column_order: list[str],
column_justifies: dict[str,su.TextJustify],
column_separator: str = " ",
scroll: int = 0,
show_title: bool = True,
name:str="Table"
):
super().__init__(width,height,name,parent)
self.title=title
self.columns=columns
self.column_order=column_order
self.column_sizes: dict[str,int]={}
self.column_justifies: dict[str, su.TextJustify] = column_justifies
self.rows: list[TableRow]=[]
self.column_separator = column_separator
self.scroll = scroll
self.selected = 0
self.show_title=show_title
self.filter=lambda s : True
self.disp_rows: list[TableRow]=[]
self.scrollbar = scrollbar.Scrollbar(self, 1, self.height, 0, 0, 0 )
self.update_scrollbar()
def add_row(self, data: dict[str,str], dont_recalc_sizes: bool = False):
row = TableRow(data,self.column_justifies,self.column_separator)
self.rows.append(row)
if not dont_recalc_sizes:
self.calc_column_sizes()
def set_filter(self,func):
self.filter=func
self.update_disp_rows()
def update_disp_rows(self):
self.disp_rows=[]
for row in self.rows:
if self.filter(row):
self.disp_rows.append(row)
def update_scrollbar(self):
self.scrollbar.height = self.height
self.scrollbar.view_height = self.get_shown_rows()
self.scrollbar.view_scroll = self.scroll
self.scrollbar.total_height = len(self.disp_rows)
def clear(self):
self.rows = []
self.update_disp_rows()
def calc_column_sizes(self):
for col in self.columns:
self.column_sizes[col] = su.text_width(col)
for row in self.rows:
for col in self.columns:
width = su.text_width(row.data[col])
if width > self.column_sizes[col]:
self.column_sizes[col] = width
def get_total_width(self) -> int:
width = 0
for col in self.column_order:
width += self.column_sizes[col]
width += su.text_width(self.column_separator)
width -= su.text_width(self.column_separator) # remove the last column separator
return width
def get_field(self, field: str) -> list[str]:
l = []
for row in self.rows:
l.append(su.strip_ansi(row.data[field]))
return l
def update_view(self):
# Clamp selection
self.selected=max(0,min(len(self.disp_rows)-1, self.selected)) # min: 0 max: # of files - 1
# Update scrolling
# center the view around the selected item, but clamp it to not go out of bounds
self.scroll = int(max(
0,
min(
len(self.disp_rows)-self.get_shown_rows(),
# 100000000000000,
self.selected-math.ceil(self.get_shown_rows()/2)
)
))
def get_shown_rows(self):
return (self.height
- int(self.show_title)*2 # 2 lines for the title
- 2 # column names and horizontal separator
- 2 # "..." lines
)
def view(self) -> str:
show_scrollbar = self.get_shown_rows()<len(self.disp_rows)
self.update_scrollbar()
s = ""
if self.show_title:
s += self.title
s += "\n\n"
for col in self.column_order:
s += su.ljust(col.upper(), self.column_sizes[col])
if self.column_order[-1] != col: # dont print the separator on the last column
s += self.column_separator
s += "\n"
s += "-" * self.width
s += "\n"
if self.scroll>0:
s += "..."
s += "\n"
i = 0
for row in self.disp_rows:
if (
i<self.scroll or
i>self.scroll+self.get_shown_rows()-1
):
i += 1
continue
s += "\x1b[0m"
if i == self.selected:
s += colors.invert.on
s += su.set_width(row.view(self.column_sizes,self.column_order),self.width)
if i == self.selected:
s += colors.invert.off
s += "\n"
i += 1
if self.scroll+self.get_shown_rows() < len(self.disp_rows)-1:
s += "..."
s = su.set_width(s, self.width - 2)
# s = su.set_minwidth(s, self.width - 2)
if show_scrollbar:
s = su.join_horizontal(s, self.scrollbar.view())
s += "\x1b[0m"
return s