lune/.lune/csv_printer.luau

64 lines
1.8 KiB
Lua
Raw Normal View History

--> A utility script that prints out a CSV
--> file in a prettified format to stdout
local LINE_SEPARATOR = "\n"
local COMMA_SEPARATOR = ","
local fs = require("@lune/fs")
local process = require("@lune/process")
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
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
end
2023-02-04 04:47:22 +00:00
-- Gather the maximum widths of strings
-- for alignment & spacing in advance
2023-02-04 04:47:22 +00:00
local maxWidths = {}
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)
end
end
local totalWidth = 0
2023-02-04 04:47:22 +00:00
local totalColumns = 0
for _, width in maxWidths do
totalWidth += width
2023-02-04 04:47:22 +00:00
totalColumns += 1
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:47:22 +00:00
local thiccLine = string.rep("", totalWidth + totalColumns * 3 - 1)
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
end
print(string.format("┗%s┛", thiccLine))