This repository was archived by the owner on Nov 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty-csv.lua
More file actions
240 lines (199 loc) · 4.83 KB
/
Copy pathpretty-csv.lua
File metadata and controls
240 lines (199 loc) · 4.83 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
--------------------------------
-- CSV Parser/Render
--
-- Copyright (c) 2023 uriid1
-- License MIT
--------------------------------
local utf8 = require('utf8')
local COLOR = {
reset ="\27[0m",
gray = "\27[38;5;240m",
gray_number = "\27[38;5;245m",
}
local function cprint(text, color, nocolor)
if nocolor then
io.write(text)
end
io.write(color..text..COLOR.reset)
end
local special = {
['\a'] = true,
['\b'] = true,
['\t'] = true,
['\n'] = true,
['\v'] = true,
['\f'] = true,
['\r'] = true,
}
-- http://lua-users.org/lists/lua-l/2014-04/msg00590.html
local function utf8_sub(s,i,j)
i = i or 1
j = j or -1
if (i < 1) or (j < 1) then
local n = utf8.len(s)
if not n then
return nil
end
if i < 0 then i = n + 1 + i end
if j < 0 then j = n + 1 + j end
if i < 0 then i = 1 elseif i > n then i = n end
if j < 0 then j = 1 elseif j > n then j = n end
end
if j < i then return '' end
i = utf8.offset(s, i)
j = utf8.offset(s, j + 1)
if i and j then return s:sub(i, j - 1)
elseif i then return s:sub(i)
else return ''
end
end
local function parse(filepath, param)
local csv = {}
local rowLen = {}
local csvIndex = 0
local sep = param.sep or ','
local limit = param.limit or -1
local max_word_len = param.word_len
local rowCount = 0
for column in io.lines(filepath, "*l") do
local inQuotes = false
local resRowStr = ''
local curLen = 0
local columnLen = #column
for i = 1, columnLen do
local char = utf8_sub(column, i, i)
if char == '"' then
inQuotes = not inQuotes
end
if char == sep and not inQuotes then
csvIndex = csvIndex + 1
if max_word_len and curLen > max_word_len then
resRowStr = utf8_sub(resRowStr, 1, max_word_len - 3)..'...'
curLen = max_word_len
end
if not csv[csvIndex] then
csv[csvIndex] = {}
rowLen[csvIndex] = {}
end
table.insert(csv[csvIndex], resRowStr)
table.insert(rowLen[csvIndex], curLen)
curLen = 0
resRowStr = ''
else
if not special[char] then
resRowStr = resRowStr .. char
curLen = curLen + 1
end
end
end
csvIndex = csvIndex + 1
local strLen = utf8.len(resRowStr)
if max_word_len and strLen > max_word_len then
resRowStr = utf8_sub(resRowStr, 1, max_word_len - 3)..'...'
end
if not csv[csvIndex] then
csv[csvIndex] = {}
rowLen[csvIndex] = {}
end
table.insert(csv[csvIndex], resRowStr)
table.insert(rowLen[csvIndex], strLen)
if limit ~= -1 and limit == rowCount then
break
end
rowCount = rowCount + 1
csvIndex = 0
end
local result = {
csv = csv,
row_count = rowCount,
row_len = rowLen,
param = param,
}
return result
end
local function getMaxRowLen(csv, column)
local maxLen = 0
for i = 1, #csv[column] do
local len = csv[column][i]
if len > maxLen then
maxLen = len
end
end
return maxLen
end
local function render(csv_table)
local csvTableLen = #csv_table.csv
local rowCount = csv_table.row_count
local csv = csv_table.csv
--
-- Header
--
cprint('┌', COLOR.gray)
local maxLen = {}
for i = 1, #csv_table.row_len do
local len = getMaxRowLen(csv_table.row_len, i)
table.insert(maxLen, len)
local pos = ' '..i ..' '
cprint(pos, COLOR.gray_number)
cprint(string.rep('─', len - #pos + 2), COLOR.gray)
if i == #csv_table.row_len then
cprint('┐', COLOR.gray)
else
cprint('┐', COLOR.gray)
end
end
io.write('\n')
--
-- Body
--
for item = 1, rowCount do
local lenRows = {}
for column = 1, csvTableLen do
local maxLen = maxLen[column]
local curItem = csv[column][item]
if not curItem then
goto continue
end
cprint('│ ', COLOR.gray)
local len = maxLen - utf8.len(curItem) + 1
table.insert(lenRows, maxLen)
io.write(curItem..string.rep(' ', len))
if column == csvTableLen then
cprint('│', COLOR.gray)
cprint(' '..item, COLOR.gray_number)
end
::continue::
end
io.write('\n')
if item ~= rowCount then
cprint('├', COLOR.gray)
for i = 1, #lenRows do
local len = lenRows[i]
cprint(string.rep('─', len + 2), COLOR.gray)
if i == #lenRows then
cprint('┤', COLOR.gray)
else
cprint('┼', COLOR.gray)
end
end
io.write('\n')
end
end
--
-- End
--
cprint('└', COLOR.gray)
for i = 1, #maxLen do
cprint(string.rep('─', maxLen[i] + 2), COLOR.gray)
if i == #csv_table.row_len then
cprint('┘', COLOR.gray)
else
cprint('┴', COLOR.gray)
end
end
io.write('\n')
end
return {
parse = parse,
render = render,
}