mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Improve csv printer example
This commit is contained in:
parent
28d6817c49
commit
23b570b609
1 changed files with 26 additions and 27 deletions
|
@ -10,50 +10,49 @@ assert(path ~= nil and #path > 0, "No input file path was given")
|
||||||
assert(not fs.isDir(path), "Input file path was a dir, not a file")
|
assert(not fs.isDir(path), "Input file path was a dir, not a file")
|
||||||
assert(fs.isFile(path), "Input file path does not exist")
|
assert(fs.isFile(path), "Input file path does not exist")
|
||||||
|
|
||||||
-- Read all the lines of the wanted file
|
-- Read all the lines of the wanted file, and then split
|
||||||
local rawLines = string.split(fs.readFile(path), LINE_SEPARATOR)
|
-- out the raw lines containing comma-separated values
|
||||||
|
|
||||||
-- Split the raw lines into header and table of data
|
|
||||||
local csvHeader = string.split(rawLines[1], COMMA_SEPARATOR)
|
|
||||||
local csvTable = {}
|
local csvTable = {}
|
||||||
for index = 2, #rawLines, 1 do -- NOTE: We skip the first line here, that's the header
|
for index, rawLine in string.split(fs.readFile(path), LINE_SEPARATOR) do
|
||||||
csvTable[index - 1] = string.split(rawLines[index], COMMA_SEPARATOR)
|
csvTable[index] = string.split(rawLine, COMMA_SEPARATOR)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Gather the maximum widths of strings for alignment
|
-- Gather the maximum widths of strings
|
||||||
|
-- for alignment & spacing in advance
|
||||||
|
|
||||||
local maxWidths = {}
|
local maxWidths = {}
|
||||||
|
|
||||||
for index, header in csvHeader do
|
|
||||||
maxWidths[index] = #header
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, row in csvTable do
|
for _, row in csvTable do
|
||||||
for index, value in row do
|
for index, value in row do
|
||||||
maxWidths[index] = math.max(maxWidths[index], #value)
|
maxWidths[index] = math.max(maxWidths[index] or 0, #value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local totalWidth = 0
|
local totalWidth = 0
|
||||||
|
local totalColumns = 0
|
||||||
for _, width in maxWidths do
|
for _, width in maxWidths do
|
||||||
totalWidth += width
|
totalWidth += width
|
||||||
|
totalColumns += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Print it all out
|
-- We have everything we need, print it out with
|
||||||
|
-- the help of some unicode box drawing characters
|
||||||
|
|
||||||
local function printRow(row: { string })
|
local thiccLine = string.rep("━", totalWidth + totalColumns * 3 - 1)
|
||||||
local paddedValues = {}
|
|
||||||
for index, value in row do
|
|
||||||
local spacing = string.rep(" ", maxWidths[index] - #value)
|
|
||||||
table.insert(paddedValues, string.format(" %s%s ", value, spacing))
|
|
||||||
end
|
|
||||||
print(string.format("┃ %s ┃", table.concat(paddedValues, "┃")))
|
|
||||||
end
|
|
||||||
|
|
||||||
local thiccLine = string.rep("━", totalWidth + #csvHeader * 3 + 1)
|
|
||||||
print(string.format("┏%s┓", thiccLine))
|
print(string.format("┏%s┓", thiccLine))
|
||||||
printRow(csvHeader)
|
|
||||||
print(string.format("┣%s┫", thiccLine))
|
for rowIndex, row in csvTable do
|
||||||
for _, row in csvTable do
|
local paddedValues = {}
|
||||||
printRow(row)
|
for valueIndex, value in row do
|
||||||
|
local spacing = string.rep(" ", maxWidths[valueIndex] - #value)
|
||||||
|
table.insert(paddedValues, value .. spacing)
|
||||||
|
end
|
||||||
|
print(string.format("┃ %s ┃", table.concat(paddedValues, " ┃ ")))
|
||||||
|
-- The first line is the header, we should
|
||||||
|
-- print out an extra separator below it
|
||||||
|
if rowIndex == 1 then
|
||||||
|
print(string.format("┣%s┫", thiccLine))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
print(string.format("┗%s┛", thiccLine))
|
print(string.format("┗%s┛", thiccLine))
|
||||||
|
|
Loading…
Reference in a new issue