The demo folder only had a toy todo list, which did not show that a non-trivial application can be written in Lua and shipped as a single compiled binary. This adds LuaSheet: a tokenizer, a formula parser, a lazy evaluator with cycle detection, range-aware builtin functions, ASCII/CSV rendering and a benchmark, all driven by a CLI entry point. Everything stays plain Lua under Demo/: no C++ change is needed to build or run it.
109 lines
2.8 KiB
Lua
109 lines
2.8 KiB
Lua
-- ASCII rendering of a sheet: grid, formula view, CSV export and bars.
|
|
local format = require("sheet.format")
|
|
|
|
local M = {}
|
|
|
|
local MAX_WIDTH = 24
|
|
|
|
function M.render(sheet, options)
|
|
options = options or {}
|
|
local show_formulas = options.show_formulas
|
|
local max_row, max_col = sheet:dimensions()
|
|
if max_row == 0 or max_col == 0 then
|
|
return "(feuille vide)"
|
|
end
|
|
|
|
local widths = {}
|
|
local cells = {}
|
|
local numeric = {}
|
|
for column = 1, max_col do
|
|
widths[column] = #format.column_label(column)
|
|
end
|
|
|
|
for row = 1, max_row do
|
|
cells[row] = {}
|
|
numeric[row] = {}
|
|
for column = 1, max_col do
|
|
local key = format.column_label(column) .. row
|
|
local text
|
|
if show_formulas then
|
|
text = sheet:raw(key) or ""
|
|
else
|
|
text = sheet:display(key)
|
|
end
|
|
text = format.fit(text, MAX_WIDTH)
|
|
cells[row][column] = text
|
|
if #text > widths[column] then
|
|
widths[column] = #text
|
|
end
|
|
local value = sheet:value(key)
|
|
numeric[row][column] = type(value) == "number"
|
|
end
|
|
end
|
|
|
|
local label_width = #tostring(max_row)
|
|
local lines = {}
|
|
|
|
local header = format.pad("", label_width) .. " |"
|
|
for column = 1, max_col do
|
|
header = header .. " " .. format.pad(format.column_label(column), widths[column]) .. " |"
|
|
end
|
|
lines[#lines + 1] = header
|
|
|
|
local separator = string.rep("-", label_width) .. "-+"
|
|
for column = 1, max_col do
|
|
separator = separator .. string.rep("-", widths[column] + 2) .. "+"
|
|
end
|
|
lines[#lines + 1] = separator
|
|
|
|
for row = 1, max_row do
|
|
local line = format.pad(tostring(row), label_width, true) .. " |"
|
|
for column = 1, max_col do
|
|
local text = cells[row][column]
|
|
line = line .. " " .. format.pad(text, widths[column], numeric[row][column]) .. " |"
|
|
end
|
|
lines[#lines + 1] = line
|
|
end
|
|
|
|
return table.concat(lines, "\n")
|
|
end
|
|
|
|
function M.csv(sheet)
|
|
local max_row, max_col = sheet:dimensions()
|
|
local lines = {}
|
|
for row = 1, max_row do
|
|
local fields = {}
|
|
for column = 1, max_col do
|
|
local text = sheet:display(format.column_label(column) .. row)
|
|
if string.find(text, '[,"\n]') then
|
|
local escaped = string.gsub(text, '"', '""')
|
|
text = '"' .. escaped .. '"'
|
|
end
|
|
fields[#fields + 1] = text
|
|
end
|
|
lines[#lines + 1] = table.concat(fields, ",")
|
|
end
|
|
return table.concat(lines, "\n")
|
|
end
|
|
|
|
-- Proportional bar, e.g. bar(820, 1923, 24) = "##########.............."
|
|
function M.bar(value, maximum, width)
|
|
if maximum <= 0 or width <= 0 then
|
|
return ""
|
|
end
|
|
local filled = math.floor(value / maximum * width + 0.5)
|
|
if value > 0 and filled < 1 then
|
|
filled = 1
|
|
end
|
|
if filled > width then
|
|
filled = width
|
|
end
|
|
return string.rep("#", filled) .. string.rep(".", width - filled)
|
|
end
|
|
|
|
function M.fit(text, width)
|
|
return format.fit(text, width)
|
|
end
|
|
|
|
return M
|