-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpletable.py
79 lines (72 loc) · 2.28 KB
/
simpletable.py
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
from enum import Enum
class Color(Enum):
red = '\033[31m'
green = '\033[32m'
blue = '\033[34m'
yellow='\033[33m'
white='\033[37m'
grey='\033[30m'
default='\033[0m'
class Table:
rows=[[]]
rowColor=[[]]
titleCount=0
title=[]
def __init__(self,title):
self.titleCount=len(title)
self.title=title
self.rows[0]=title
self.rowColor[0]=([Color.default for i in range(len(title))])
def AddRow(self,row,color=None):
for i in range(len(row)):
row[i]=str(row[i])
if not len(row)==self.titleCount:
raise RuntimeError("The length of the row not match!")
self.rows.append(row)
if not color==None:
self.rowColor.append(color)
else:
self.rowColor.append([Color.default for i in range(len(row))])
def PrintLine(self,width):
for i in range(width):
print("-",end='')
print()
def PrintTable(self):
itemCount=len(self.rows[0])
size=[0 for i in range(itemCount)]
for i in range(itemCount):
for row in self.rows:
stringLen=len(row[i])
if stringLen>size[i]:
size[i]=stringLen
for row in self.rows:
for i in range(itemCount):
spaceCount=int((size[i]-len(row[i]))/2)
space=''
for i2 in range(spaceCount):
space+=' '
row[i]=space+row[i]+space
if (size[i]-len(row[i]))%2>0:
row[i]+=' '
width=sum(size)+itemCount+1
self.PrintLine(width)
for i in range(len(self.rows)):
for i2 in range(len(self.rows[i])):
print("|",end='')
try:
print(self.rowColor[i][i2].value,end='')
except:
print("",end='')
print(self.rows[i][i2],end='')
print(Color.default.value,end='')
print("|")
self.PrintLine(width)
def Clear(self):
self.rows=[[]]
self.rowColor=[[]]
self.rows[0]=self.title
self.rowColor[0]=([Color.default for i in range(len(self.title))])
'''t=Table(['a','b'])
t.AddRow(['c','d'])
t.PrintTable()
#print(t.rows)'''