Improve csv printer example

This commit is contained in:
Filip Tibell 2023-02-03 23:47:22 -05:00
parent 28d6817c49
commit 23b570b609
No known key found for this signature in database

View file

@ -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)
print(string.format("┏%s┓", thiccLine))
for rowIndex, row in csvTable do
local paddedValues = {} local paddedValues = {}
for index, value in row do for valueIndex, value in row do
local spacing = string.rep(" ", maxWidths[index] - #value) local spacing = string.rep(" ", maxWidths[valueIndex] - #value)
table.insert(paddedValues, string.format(" %s%s ", value, spacing)) table.insert(paddedValues, value .. spacing)
end end
print(string.format("┃ %s ┃", table.concat(paddedValues, ""))) print(string.format("┃ %s ┃", table.concat(paddedValues, "")))
end -- The first line is the header, we should
-- print out an extra separator below it
local thiccLine = string.rep("", totalWidth + #csvHeader * 3 + 1) if rowIndex == 1 then
print(string.format("┏%s┓", thiccLine))
printRow(csvHeader)
print(string.format("┣%s┫", thiccLine)) print(string.format("┣%s┫", thiccLine))
for _, row in csvTable do end
printRow(row)
end end
print(string.format("┗%s┛", thiccLine)) print(string.format("┗%s┛", thiccLine))