2023-02-04 04:40:27 +00:00
|
|
|
--> A utility script that prints out a CSV
|
|
|
|
--> file in a prettified format to stdout
|
|
|
|
|
|
|
|
local LINE_SEPARATOR = "\n"
|
|
|
|
local COMMA_SEPARATOR = ","
|
|
|
|
|
2023-05-14 21:16:58 +01:00
|
|
|
local fs = require("@lune/fs")
|
|
|
|
local process = require("@lune/process")
|
|
|
|
|
2023-02-04 04:40:27 +00:00
|
|
|
local path = process.args[1] or ".lune/data/test.csv"
|
|
|
|
|
|
|
|
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(fs.isFile(path), "Input file path does not exist")
|
|
|
|
|
2023-02-04 04:47:22 +00:00
|
|
|
-- Read all the lines of the wanted file, and then split
|
|
|
|
-- out the raw lines containing comma-separated values
|
2023-02-04 04:40:27 +00:00
|
|
|
|
|
|
|
local csvTable = {}
|
2023-02-04 04:47:22 +00:00
|
|
|
for index, rawLine in string.split(fs.readFile(path), LINE_SEPARATOR) do
|
2023-02-24 16:44:09 +00:00
|
|
|
if #rawLine > 0 then
|
|
|
|
csvTable[index] = string.split(rawLine, COMMA_SEPARATOR)
|
|
|
|
end
|
2023-02-04 04:40:27 +00:00
|
|
|
end
|
|
|
|
|
2023-02-04 04:47:22 +00:00
|
|
|
-- Gather the maximum widths of strings
|
|
|
|
-- for alignment & spacing in advance
|
2023-02-04 04:40:27 +00:00
|
|
|
|
2023-02-04 04:47:22 +00:00
|
|
|
local maxWidths = {}
|
2023-02-04 04:40:27 +00:00
|
|
|
for _, row in csvTable do
|
|
|
|
for index, value in row do
|
2023-02-04 04:47:22 +00:00
|
|
|
maxWidths[index] = math.max(maxWidths[index] or 0, #value)
|
2023-02-04 04:40:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local totalWidth = 0
|
2023-02-04 04:47:22 +00:00
|
|
|
local totalColumns = 0
|
2023-02-04 04:40:27 +00:00
|
|
|
for _, width in maxWidths do
|
|
|
|
totalWidth += width
|
2023-02-04 04:47:22 +00:00
|
|
|
totalColumns += 1
|
2023-02-04 04:40:27 +00:00
|
|
|
end
|
|
|
|
|
2023-02-04 04:47:22 +00:00
|
|
|
-- We have everything we need, print it out with
|
|
|
|
-- the help of some unicode box drawing characters
|
2023-02-04 04:40:27 +00:00
|
|
|
|
2023-02-04 04:47:22 +00:00
|
|
|
local thiccLine = string.rep("━", totalWidth + totalColumns * 3 - 1)
|
2023-02-04 04:40:27 +00:00
|
|
|
|
|
|
|
print(string.format("┏%s┓", thiccLine))
|
2023-02-04 04:47:22 +00:00
|
|
|
|
|
|
|
for rowIndex, row in csvTable do
|
|
|
|
local paddedValues = {}
|
|
|
|
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
|
2023-02-04 04:40:27 +00:00
|
|
|
end
|
|
|
|
print(string.format("┗%s┛", thiccLine))
|