Repository navigation
Expand file tree
/
Copy pathPrettyTable.lua
More file actions
149 lines (115 loc) · 3.02 KB
/
Copy pathPrettyTable.lua
File metadata and controls
149 lines (115 loc) · 3.02 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
local DEFAULT_COLUMN_SIZE = 5 --Count in symbols
local VERTICAL_SYMBOL = "═"
local HORIZONTAL_SYMBOL = "║"
local ANGLE_SYMBOL = "+"
local AllTables = {}
local Table = {}
local function isArray(tab)
return #tab > 0
end
function Table:AddRow( row )
if not isArray(row) then return end
table.insert(self.Rows, row)
end
function Table:AddColumn( name )
table.insert(self.Columns, name)
end
function Table:AddColumns( columns )
if not isArray(columns) then return end
for i, name in pairs(columns) do
table.insert(self.Columns, name)
end
end
function Table:AddRows( rows )
if not isArray(rows) then return end
for i, row in pairs(rows) do
table.insert(self.Rows, row)
end
end
function Table:Remove()
AllTables[self.Name] = nil
self = nil
end
local function GetColumnWeight( prettyTable )
local weight = DEFAULT_COLUMN_SIZE
for i, row in pairs(prettyTable.Rows) do
for i, info in pairs(row) do
if weight < tostring(info):len() then
weight = tostring(info):len()
end
end
end
for i, column in pairs(prettyTable.Columns) do
if weight < tostring(column):len() then
weight = tostring(column):len()
end
end
return weight
end
local function GetCell( name, cellWeight )
local spacebar = " "
return VERTICAL_SYMBOL.." "..name..spacebar:rep(cellWeight - name:len()).." "
end
local function Graf( columnsCount, cellWeight )
local graf = HORIZONTAL_SYMBOL
return graf:rep(columnsCount*(cellWeight+3)+1)
end
local function PrintTable( prettyTable )
local text="\n"
local cellWeight = GetColumnWeight(prettyTable)
text = text..Graf(#prettyTable.Columns,cellWeight)..'\n'
for i, column in pairs(prettyTable.Columns) do
local cell = GetCell( tostring(column), cellWeight )
text = text..cell
end
text= text.."|\n"
for i, row in pairs(prettyTable.Rows) do
text = text..Graf(#prettyTable.Columns,cellWeight)..'\n'
for i, info in pairs(row) do
local cell = GetCell( info, cellWeight )
text = text..cell
end
text= text.."|\n"
end
text = text..Graf(#prettyTable.Columns,cellWeight)..'\n'
return text
end
local PrettyTable = {}
function PrettyTable.new( name )
local self = setmetatable(
{
Name = name,
Rows = {},
Columns = {}
},
{
__index = Table,
__tostring = function()
local tab = AllTables[name]
return PrintTable(tab)
end,
})
AllTables[name] = self
return self
end
function PrettyTable:Convert( name, tab )
if isArray(tab) then return end
local columnsNames = {}
local row = {}
for i, info in pairs(tab) do
table.insert(columnsNames,tostring(i))
if typeof(info)=="table" then
local success = pcall(function()
table.insert(row,table.unpack(info))
end)
if not success then table.insert(row,"{...}") end
else
table.insert(row,tostring(info))
end
end
local tab = PrettyTable.new(name)
tab:AddColumns(columnsNames)
tab:AddRow(row)
return tab
end
return PrettyTable